我是 Java 和 Android 的新手,所以请多多包涵。我的活动中有 3 个按钮和 imageView。每个按钮都会生成新版本的 ImageView 中显示的图片。当我单击它时,我希望 imageView 将当前图片放大到全屏。我的问题是:实现这一目标的最佳方法是什么?
- 我应该创建 onClickListener 来放大图片吗?
我应该为 ImageView 定义一个方法来调用
activity_start.xml
吗android:onClick="myMethodToCall
我的 activity_start.xml 代码:
<Button android:id="@+id/button1" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:onClick="template1match" android:text="image1" /> <Button android:id="@+id/button2" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/button1" android:layout_centerHorizontal="true" android:onClick="template2match" android:text="image2" /> <Button android:id="@+id/button3" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignTop="@+id/button2" android:layout_marginRight="16dp" android:onClick="template3match" android:text="image3" /> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:clickable="true" android:src="@drawable/wall" />
我的 Start.java 代码:
package com.example.matchtemplate;
import java.io.File;
import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.Core;
import org.opencv.core.Core.MinMaxLocResult;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class Start extends Activity {
Button button1;
Button button2;
Button button3;
ImageView imageView1;
File cacheDir;
protected static final String TAG = "OpenCV";
private BaseLoaderCallback mLoaderCallBack = new BaseLoaderCallback(this) {
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS:
Log.i(TAG, "Open CV loaded successfully");
break;
default:
super.onManagerConnected(status);
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
//addListenerOnButton();
initDir();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.start, menu);
return true;
}
@Override
public void onResume() {
super.onResume();
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_6, this,
mLoaderCallBack);
}
// ADDED THIS: to create/initialize external file
// Be sure write permissions is enabled in the manifest file
// ie add: <uses-permission android:name= "android.permission.WRITE_EXTERNAL_STORAGE"/>
public void initDir() {
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
cacheDir = new File(
android.os.Environment.getExternalStorageDirectory(),
"FileList");
if (!cacheDir.exists()) {
cacheDir.mkdirs();
}
}
}
// added this to simplify creating full file path
public String getFileAbsPath(String fileName) {
File f = new File(cacheDir, fileName);
return f.getAbsolutePath();
}
public void template1match(View v) {
imageView1 = (ImageView) findViewById(R.id.imageView1);
button1 = (Button) findViewById(R.id.button1);
String infile = getFileAbsPath("wall.jpg");
String tp = getFileAbsPath("template1.jpg");
String outFile = getFileAbsPath("result.jpg");
try {
matchTemplate(infile, tp, outFile, Imgproc.TM_CCOEFF);
Bitmap bm = BitmapFactory.decodeFile(outFile);
imageView1.setImageBitmap(bm);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public void template2match(View v) {
imageView1 = (ImageView) findViewById(R.id.imageView1);
button2 = (Button) findViewById(R.id.button2);
String infile = getFileAbsPath("wall.jpg");
String tp = getFileAbsPath("template2.jpg");
String outFile = getFileAbsPath("result.jpg");
try {
matchTemplate(infile, tp, outFile, Imgproc.TM_CCOEFF);
Bitmap bm = BitmapFactory.decodeFile(outFile);
imageView1.setImageBitmap(bm);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public void template3match(View v) {
imageView1 = (ImageView) findViewById(R.id.imageView1);
button3 = (Button) findViewById(R.id.button3);
String infile = getFileAbsPath("wall.jpg");
String tp = getFileAbsPath("template3.jpg");
String outFile = getFileAbsPath("result.jpg");
try {
matchTemplate(infile, tp, outFile, Imgproc.TM_CCOEFF);
Bitmap bm = BitmapFactory.decodeFile(outFile);
imageView1.setImageBitmap(bm);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public void matchTemplate(String inFile, String templateFile,
String outFile, int match_method) {
Log.i(TAG, "Running Template Matching");
Mat img = Highgui.imread(inFile);
Mat templ = Highgui.imread(templateFile);
// / Create the result matrix
int result_cols = img.cols() - templ.cols() + 1;
int result_rows = img.rows() - templ.rows() + 1;
Mat result = new Mat(result_rows, result_cols, CvType.CV_32FC1);
// / Do the Matching and Normalize
Imgproc.matchTemplate(img, templ, result, match_method);
Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new Mat());
// / Localizing the best match with minMaxLoc
MinMaxLocResult mmr = Core.minMaxLoc(result);
Point matchLoc;
if (match_method == Imgproc.TM_SQDIFF
|| match_method == Imgproc.TM_SQDIFF_NORMED) {
matchLoc = mmr.minLoc;
} else {
matchLoc = mmr.maxLoc;
}
// / Show me what you got
Core.rectangle(img, matchLoc, new Point(matchLoc.x + templ.cols(),
matchLoc.y + templ.rows()), new Scalar(0, 255, 0));
// Save the visualized detection.
Log.i(TAG, "Writing: " + outFile);
Highgui.imwrite(outFile, img);
}
}