Android下使用Opencv有三种方式:OpenCVManager、OpencvLibrary和JNI方式。第一种方法需要将Opencv的APK加载到系统中,这样的操作方式只能自己测试一下而已。
第三种方法则需要对Opencv比较熟悉。我们这里先来试试第二种方法。
步骤0:默认在windows下已经安装好了Android Studio2.2。
步骤1:下载android版本OpenCV的sdk,我这里下载OpenCV-3.1.0-android-sdk。下载 ,解压即可。
步骤2:使用Android Studio新建一个工程MyOpencv310,不支持NDK的工程。
将OpenCV-3.1.0-android-sdk加载到工程中。
修改MyOpencv310和openCVLibrary310目录下的build.gradle文件,对其中buildToolsVersion、compileSdkVwesion进行必要修改,以便编译过去。
步骤3:将OpenCV-3.1.0-android-sdk下的libs文件夹拷贝到MyOpencv310下的app目录下,并改名为opencv310_libs,将下面的代码添加到MyOpencv310下的build.gradle文件中。
- sourceSets {
- main {
- jniLibs.srcDirs=['F:\MediaTekX20\opencv-project\MyOpencv310\app\src\main\opencv310_libs']
- }
- }
步骤4:添加openCVLibrary310模块
步骤5:准备测试图片happyfish.jpg注意名字里面不能有大写字母
步骤6:layout界面设计
- tiveLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/activity_main"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context="com.example.administrator.myopencv310.MainActivity">
-
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Hello World! =0001=" />
-
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentBottom="true"
- android:layout_alignParentEnd="true"
- android:layout_marginEnd="75dp"
- android:layout_marginBottom="163dp"
- android:id="@+id/imageView217" />
-
- android:text="Button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentBottom="true"
- android:layout_alignParentEnd="true"
- android:layout_marginEnd="44dp"
- android:layout_marginBottom="46dp"
- android:id="@+id/button217" />
步骤7:MainActivity.java代码
- package com.example.administrator.myopencv310;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- 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 {
- private static String TAG = "MyOpencv310";
- static {
- if (OpenCVLoader.initDebug()) {
- Log.i(TAG, "OpenCV310 initialize success");
- } else {
- Log.i(TAG, "OpenCV310 initialize failed");
- }
- }
- Button btnProcess;
- Bitmap srcBitmap;
- Bitmap grayBitmap;
- ImageView imgHuaishi;
- private static boolean flag = true;
- //private static boolean isFirst = true;
- //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.button217);
- imgHuaishi = (ImageView)findViewById(R.id.imageView217);
- Log.i(TAG, "initUI sucess...");
- }
- public void procSrc2Gray(){
- Mat rgbMat = new Mat();
- Mat grayMat = new Mat();
- srcBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.happyfish);
- 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
- // if(isFirst)
- // {
- procSrc2Gray();
- // isFirst = false;
- // }
- 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_3_0_0, this, mLoaderCallback);
- } else {
- Log.d(TAG, "OpenCV library found inside package. Using it!");
- mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
- }
- }
- }
编译运行结果: