最近项目中需要用到opencv,于是就研究了一下怎么在Android studio中配置opencv,记录一下,免得以后还会使用。
一.因为本人Android Studio是4.1的,网上资料大多是3.5版本,配置略有不同,主要参考 Android Studio 中配置 Opencv4.5_hui~奔跑的博客,简单明了
以下标明我的步骤:
点finish即可。
二.配置过程中导入的module不是选Java 而是直接选择sdk!!
三.在执行完前两步后,可以采用以下方式调用openvc的函数
可以在activity里面先打一个Mat ,然后鼠标放上去点击添加依赖,
点完之后,这两个就自动帮你配好了,不用再手动改来改去了。
等待完后,就可以import class了,即可调用openvc函数。
四.测试代码(借鉴于 Android Studio 使用OpenCV配置_pengshuyes的博客)
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="16dp" android:paddingRight="16dp" android:paddingTop="16dp" android:paddingBottom="16dp" tools:context="top.liushupeng.opencv0603.MainActivity"> <ImageView android:id="@+id/img_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" /> <Button android:id="@+id/btn_gray_process" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/img_2" android:layout_centerHorizontal="true" android:text="灰度化"/> </RelativeLayout>
MainActivity.java
ackage top.liushupeng.opencvtest; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ImageView; import org.opencv.android.BaseLoaderCallback; import org.opencv.android.LoaderCallbackInterface; import org.opencv.android.OpenCVLoader; import org.opencv.android.Utils; import org.opencv.core.Mat; import org.opencv.imgproc.Imgproc; public class MainActivity extends AppCompatActivity { 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, "成功加载"); break; default: super.onManagerConnected(status); Log.i(TAG, "加载失败"); break; } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initUI(); btnProcess.setOnClickListener(new ProcessClickListener()); } public void initUI() { btnProcess = (Button) findViewById(R.id.btn_gray_process); imgHuaishi = (ImageView) findViewById(R.id.img_2); Log.i(TAG, "initUI sucess..."); } public void procSrc2Gray() { Mat rgbMat = new Mat(); Mat grayMat = new Mat(); srcBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.img_2); grayBitmap = Bitmap.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(), Bitmap.Config.RGB_565); 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..."); } private class ProcessClickListener implements View.OnClickListener { @Override public void onClick(View v) { // TODO Auto-generated method stub procSrc2Gray(); if (flag) { imgHuaishi.setImageBitmap(grayBitmap); btnProcess.setText("查看原图"); flag = false; } else { imgHuaishi.setImageBitmap(srcBitmap); btnProcess.setText("灰度化"); flag = true; } } } @Override public void onResume() { super.onResume(); if (!OpenCVLoader.initDebug()) { Log.d(TAG, "Internal OpenCV library not found. Using OpenCV Manager for initialization"); OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_13, this, mLoaderCallback); } else{ Log.d(TAG, "OpenCV library found inside package. Using it!"); mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS); } } }
效果图:
五.总结:
本次配置未出现 opencv manager package was not found 这个错误,之前导入module时导入的是java这次是sdk,不知道是不是这个原因。