Android 集成OpenCV

2023-10-07  本文已影响0人  tantanxiqi

1 下载OpenCv

opencv官网下载响应的版本,我以 4.7.0版本为例,选择响应版本下载并解压

2 新建AndroidC++项目

image.png

2.1 导入opencv

image.png
找到解压的文件下sdk目录
image.png
重命名Moudule name 将sdk 重命名opencv并点击Finish
image.png
如果报错If you've specified the package attribute in the source AndroidManifest.xml
image.png
只需要在opencv目录下的build.gradle中 配置
namespace 'org.opencv'
image.png

2.2 配置C++相关

1 在gradle.properties文件中配置opencvsdk 绝对路径
我的电脑是winds,使用的绝对路径,根据实际情况替换你项目的绝对路径

opencvsdk=D\:\\project\\android\\opencv\\OpenCvTest

2 在setting.properties文件中配置opencv的projectDir

project(':opencv').projectDir = new File(opencvsdk + '/opencv')
image.png
3 在配置app下的build.gradle文件
    externalNativeBuild {
            cmake {
                cppFlags "-frtti -fexceptions"
                abiFilters 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a'
                arguments "-DOpenCV_DIR=" + opencvsdk + "/opencv/native"
                
            }
        }
image.png

导入opencvModule

 implementation project(path: ':opencv')
image.png

4 配置 cmake文件

include_directories(${OpenCV_DIR}/jni/include)
add_library( lib_opencv SHARED IMPORTED )
set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${OpenCV_DIR}/libs/${ANDROID_ABI}/libopencv_java4.so)
lib_opencv
image.png

3 代码测试

   private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.sampleText.text = stringFromJNI()




        binding.btnConvert.setOnClickListener {
            val bitmap = BitmapFactory.decodeResource(resources, R.drawable.gril)
            val mat = Mat()
            Utils.bitmapToMat(bitmap, mat)
            processImage(mat.nativeObjAddr)
            // 将处理后的图像显示在ImageView中
            val resultBitmap = Bitmap.createBitmap(mat.cols(), mat.rows(), Bitmap.Config.ARGB_8888)
            Utils.matToBitmap(mat, resultBitmap)
            binding.ivGirl.setImageBitmap(resultBitmap)

        }


        binding.btnConvert2.setOnClickListener {

            val bitmap = BitmapFactory.decodeResource(resources, R.drawable.gril)
            val matSrc = Mat()
            val matDst = Mat()
            Utils.bitmapToMat(bitmap, matSrc)
            processImage2(matSrc.nativeObjAddr,matDst.nativeObjAddr)
            // 将处理后的图像显示在ImageView中
            val resultBitmap = Bitmap.createBitmap(matDst.cols(), matDst.rows(), Bitmap.Config.ARGB_8888)
            Utils.matToBitmap(matDst, resultBitmap)
            binding.ivGirl2.setImageBitmap(resultBitmap)


        }


        
    }



    external fun stringFromJNI(): String

    external fun processImage(nativeObjAddr: Long)

    external fun processImage2(nativeObjAddr: Long, dstObjAddr: Long)

    companion object {

        init {
            System.loadLibrary("open")
        }
    }
#include <jni.h>
#include <string>
#include <opencv2/opencv.hpp>

extern "C" JNIEXPORT jstring JNICALL
Java_com_mind_open_MainActivity_stringFromJNI(
        JNIEnv *env,
        jobject /* this */) {
    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
}
extern "C"
JNIEXPORT void JNICALL
Java_com_mind_open_MainActivity_processImage(JNIEnv *env, jobject thiz, jlong matAddr) {
    cv::Mat &image = *(cv::Mat *) matAddr;
    // 灰度处理
    cv::cvtColor(image, image, cv::COLOR_RGB2GRAY);
}
extern "C"
JNIEXPORT void JNICALL
Java_com_mind_open_MainActivity_processImage2(JNIEnv *env, jobject thiz, jlong matAddr,
                                              jlong matDst) {
    cv::Mat &image = *(cv::Mat *) matAddr;
    cv::Mat &dst = *(cv::Mat *) matDst;
    // 灰度处理
    cv::cvtColor(image, dst, cv::COLOR_RGB2GRAY);
}

4 可能遇到的问题

注意java版本的问题

5 代码地址

ruirui1128/android-opencv (github.com)

上一篇下一篇

猜你喜欢

热点阅读