如何快速的将OpenCV集成到自己的Android项目中

一、首先下载官方的OpenCV-Android-SDK,我已经下载下来了,给出链接同志们自己下载OpenCV-android-sdk.zip
二、把官方SDK中的程序文件导入到自己的项目中,具体步骤如下:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
导入官方SDK中的sdk/java文件夹,点击OK就行
在这里插入图片描述
将导入的名字改为OpenCV
在这里插入图片描述
点击Finish导入成功
三、导入库
在自己工程src/main下新建jinLibs,然后将下载的官方SDK中的OpenCV-android-sdk\sdk\native\libs下的文件拷贝进去

如下图所示
在这里插入图片描述
四、修改配置文件
1.打开Gradle Scripts,找到build.gradle(Module.OpenCV),将自己工程的build.gradle(Module.app)中的的内容拷贝到build.gradle(Module.OpenCV)中,然后修改build.gradle(Module.OpenCV)中的apply plugin: ‘com.android.application’ 改为apply plugin: ‘com.android.library’ 然后,把applicationId "com.example.mplog"这一句去掉
在这里插入图片描述
2.打开build.gradle(Module.app)在dependencies中加入这句话implementation project(":openCV")
在android中加入sourceSets { main { jniLibs.srcDirs = ['src/main/jniLibs'] } }
这句话,如下图所示
在这里插入图片描述
在这里插入图片描述
五、新建工程测试
下面是我的工程文件
MainActivity.java

package com.xp.opencvtest;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.android.Utils;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.imgproc.Imgproc;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    public static final String TAG = "OpenCv_compare";
    private Bitmap mBitmap1,mBitmap2;
    private ImageView mIv_ImageView1,mIv_ImageView2;
    private Button mBtn_compare;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }
    public void init(){
        mBitmap1 = BitmapFactory.decodeResource(getResources(), R.drawable.n1);
        mBitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.n2);
        mIv_ImageView1 = (ImageView)findViewById(R.id.iv_img1);
        mIv_ImageView2 = (ImageView)findViewById(R.id.iv_img2);
        mBtn_compare = (Button)findViewById(R.id.btn_compare);
        mIv_ImageView1.setImageBitmap(mBitmap1);
        mIv_ImageView2.setImageBitmap(mBitmap2);
        mBtn_compare.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        Mat mat1 = new Mat();
        Mat mat2 = new Mat();
        Mat mat11 = new Mat();
        Mat mat22 = new Mat();
        Utils.bitmapToMat(mBitmap1, mat1);
        Utils.bitmapToMat(mBitmap2, mat2);
        Imgproc.cvtColor(mat1, mat11, Imgproc.COLOR_BGR2GRAY);
        Imgproc.cvtColor(mat2, mat22, Imgproc.COLOR_BGR2GRAY);
        comPareHist(mat11, mat22);
    }

    public void comPareHist(Mat srcMat,Mat desMat){

        srcMat.convertTo(srcMat, CvType.CV_32F);
        desMat.convertTo(desMat, CvType.CV_32F);
        double target = Imgproc.compareHist(srcMat, desMat, Imgproc.CV_COMP_CORREL);
        Log.e(TAG, "相似度 :   ==" + target);
        Toast.makeText(this, "相似度 :   ==" + target, Toast.LENGTH_SHORT).show();
    }
    @Override
    protected void onResume() {
        super.onResume();
        if(!OpenCVLoader.initDebug()){
            Log.d(getClass().getName(),"Internal OpenCV library not found. Using OpenCV manger for initialization");
            OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_0_0, this, mLoaderCallback);
        }else{
            Log.d(getClass().getName(),"OpenCV library found inside package. Using it!");
            mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
        }
    }
    private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
        @Override
        public void onManagerConnected(int status) {
            switch (status) {
                case LoaderCallbackInterface.SUCCESS: {
                    Log.i("rr", "OpenCV loaded successfully");
                }
                break;
                default: {
                    super.onManagerConnected(status);
                }
                break;
            }
        }
    };
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignParentTop="true">

        <ImageView
            android:id="@+id/iv_img1"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:src="@drawable/n1"
            android:scaleType="fitStart"/>

        <ImageView
            android:id="@+id/iv_img2"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:src="@drawable/n2"
            android:scaleType="fitStart"/>
    </LinearLayout>


    <Button
        android:id="@+id/btn_compare"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="比较"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="20dp"/>

</RelativeLayout>

完整移植成功的工程下载,加我QQ:1124315978

原创文章 81 获赞 48 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_15181569/article/details/103843536