Android平台上调用OpenCV

时间:2022-09-29 11:20:22

opencv有支持安卓的版本,尝试调用openCv进行图片处理

在新建的工程下面,Projects-Propertities-Add-opencv Library 2.4.9

Android平台上调用OpenCV

代码如下:

public class MainActivity extends Activity {
private Button btn_photo, btn_gray,btn_canny,btn_bw;
private ImageView img;
private static int REQ_1 = 1;
private String mFilePath;
private Bitmap bitmap;
//OpenCV类库加载并初始化成功后的回调函数,在此我们不进行任何操作
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS:{
} break;
default:{
super.onManagerConnected(status);
} break;
}
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_photo = (Button)findViewById(R.id.btn_photo);
btn_gray = (Button)findViewById(R.id.btn_gray);
btn_canny = (Button)findViewById(R.id.btn_canny);
btn_bw = (Button)findViewById(R.id.btn_bw);
img = (ImageView)findViewById(R.id.image_view);
mFilePath = Environment.getExternalStorageDirectory().getPath();
mFilePath = mFilePath + "/"+"temp.jpg";
}
public void takePhoto(View v)
{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri photoUri = Uri.fromFile(new File(mFilePath));
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(intent, REQ_1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK)
{
if(requestCode== REQ_1)
{
FileInputStream fis = null;
try {
fis = new FileInputStream(mFilePath);
bitmap = BitmapFactory.decodeStream(fis);
img.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally
{
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
public void ImgToGray(View v)
{
Mat rgbMat = new Mat();
Mat grayMat = new Mat();
Utils.bitmapToMat(bitmap, rgbMat);
Imgproc.cvtColor(rgbMat, grayMat, Imgproc.COLOR_BGR2GRAY);
Bitmap grapBmp = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.RGB_565);
Utils.matToBitmap(grayMat, grapBmp);
img.setImageBitmap(grapBmp);

}
public void ImgToCanny(View v)
{
Mat rgbMat = new Mat();
Mat grayMat = new Mat();
Utils.bitmapToMat(bitmap, rgbMat);
Imgproc.cvtColor(rgbMat, grayMat, Imgproc.COLOR_BGR2GRAY);
Imgproc.Canny(grayMat, grayMat, 40, 255);
Bitmap grapBmp = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.RGB_565);
Utils.matToBitmap(grayMat, grapBmp);
img.setImageBitmap(grapBmp);

}
public void ImgToBW(View v)
{
Mat rgbMat = new Mat();
Mat grayMat = new Mat();
Utils.bitmapToMat(bitmap, rgbMat);
Imgproc.cvtColor(rgbMat, grayMat, Imgproc.COLOR_BGR2GRAY);
Imgproc.threshold(grayMat, grayMat, 50, 255, Imgproc.THRESH_BINARY);
Bitmap grapBmp = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.RGB_565);
Utils.matToBitmap(grayMat, grapBmp);
img.setImageBitmap(grapBmp);

}

@Override
public void onResume(){
super.onResume();
//通过OpenCV引擎服务加载并初始化OpenCV类库,所谓OpenCV引擎服务即是
//OpenCV_2.4.3.2_Manager_2.4_*.apk程序包,存在于OpenCV安装包的apk目录中
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_9, this, mLoaderCallback);
}

}
结果如下:

Android平台上调用OpenCV  Android平台上调用OpenCV