最近看到在Android上进行OPENCV开发的文章,OpenCV4Android开发之旅(一)----OpenCV2.4简介及 app通过Java接口调用OpenCV的示例(http://blog.csdn.net/yanzi1225627/article/details/16917961),写的很好,但是例程中有些小问题,给博主提出后,可能博主太忙了,没有时间修改,特此将改进的例子贴出,以供参考。代码中的BUG为,在界面切换,旋转屏幕或锁屏后,由于没有重新加载资源,导致无法继续显示。因此还是建议使用OPENCV官方例子中的方法,在OPENCV管理器加载前使界面无法使用,加载后,将所有资源加载,在界面切换后onPause,将界面响应删除。
package com.example.grayimg; import org.opencv.android.BaseLoaderCallback; import org.opencv.android.OpenCVLoader; import org.opencv.android.Utils; import org.opencv.core.Mat; import org.opencv.imgproc.Imgproc; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.graphics.BitmapFactory; import android.os.Bundle; import android.os.Handler; 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; public class MainActivity extends Activity { Button btnProcess; Bitmap srcBitmap; Bitmap grayBitmap; ImageView imgHuaishi; private static boolean flag = true; //private static boolean isFirst = true; private static final String TAG = "MainActivity"; //OpenCV库加载并初始化成功后的回调函数 private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) { @Override public void onManagerConnected(int status) { // TODO Auto-generated method stub switch (status){ case BaseLoaderCallback.SUCCESS: Log.i(TAG, "成功加载"); //isFirst = true; procSrc2Gray(); btnProcess.setOnClickListener(new ProcessClickListener()); break; default: super.onManagerConnected(status); Log.i(TAG, "加载失败"); break; } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initUI(); } @Override public void onPause() { super.onPause(); btnProcess.setOnClickListener(null); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } public void initUI(){ btnProcess = (Button)findViewById(R.id.btn_gray_process); imgHuaishi = (ImageView)findViewById(R.id.img_huaishi); Log.i(TAG, "initUI sucess..."); } public void procSrc2Gray(){ Mat rgbMat = new Mat(); Mat grayMat = new Mat(); srcBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.peashooter); System.out.println(srcBitmap.getWidth() + "," + srcBitmap.getHeight()); grayBitmap = Bitmap.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(), Config.RGB_565); System.out.println(grayBitmap.getWidth() + "," + grayBitmap.getHeight()); Utils.bitmapToMat(srcBitmap, rgbMat);//convert original bitmap to Mat, R G B. Imgproc.cvtColor(rgbMat, grayMat, Imgproc.COLOR_RGB2GRAY);//rgbMat to gray grayMat Utils.matToBitmap(grayMat, grayBitmap); //convert mat to bitmap Log.i(TAG, "procSrc2Gray sucess..."); if(flag) { imgHuaishi.setImageBitmap(srcBitmap); btnProcess.setText("灰度化"); } else { imgHuaishi.setImageBitmap(grayBitmap); btnProcess.setText("查看原图"); } imgHuaishi.invalidate(); } private class ProcessClickListener implements OnClickListener{ @Override public void onClick(View v) { if(flag){ imgHuaishi.setImageBitmap(grayBitmap); btnProcess.setText("查看原图"); flag = false; } else{ imgHuaishi.setImageBitmap(srcBitmap); btnProcess.setText("灰度化"); flag = true; } } } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); //load OpenCV engine and init OpenCV library OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_7, getApplicationContext(), mLoaderCallback); Log.i(TAG, "onResume sucess load OpenCV..."); } }