欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android Studio导入OpenCV库

程序员文章站 2022-05-16 09:10:30
...

一、 OpenCV 包下载

下载链接 OpenCV 选择Android pack下载后解压

二、 AS导入 OpenCV

1.新建项目选择Import Module导入
Android Studio导入OpenCV库
2.选择导入的路径D:\AndroidMode\opencv-4.1.0-android-sdk\OpenCV-android-sdk\sdk,修改名称 “OpenCVLibrary410”
Android Studio导入OpenCV库
3.选择OpenCVLibrary410导入
Android Studio导入OpenCV库
4.修改OpenCVLibrary410的build.gradle与项目的build.gradle版本保持一致
Android Studio导入OpenCV库

三、初始化OpenCV库

override fun onResume() {
        super.onResume()
        if (!OpenCVLoader.initDebug()) {
            Log.e("TAG", "Internal OpenCV library not found. Using OpenCV Manager for initialization")
            OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_1_0, this, mLoaderCallback)
        } else {
            Log.e("TAG", "OpenCV library found inside package. Using it!")
            mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS)
        }
    }

    //OpenCV库加载并初始化成功后的回调函数
    private val mLoaderCallback = object : BaseLoaderCallback(this) {
        override fun onManagerConnected(status: Int) {
            when (status) {
                SUCCESS -> Log.e("TAG", "成功加载")
                else -> {
                    super.onManagerConnected(status)
                    Log.e("TAG", "加载失败")
                }
            }
        }
    }