android studio版本:3.6.4
gradle: 3.6.1 、5.6.4
opencv:4.5.3
下载open cv sdk
opencv sdk 下载,我下载的是最新的4.5.3。
创建项目
这个按正常的创建一个空Activity的项目即可
将下载解压的opencv sdk导入到创建的项目中作为一个moudle
步骤如下:
在上面创建的项目中点击菜单: File > New > Import-Module
导入sdk文件夹
将opencv sdk中的OpenCV-android-sdk/sdk 这个“sdk目录“导入到moudle中,这里我将模块名命名为opencvtest
- 到此映入就完成了
检测是否成功
- 在创建的项目中引入依赖
dependencies {
implementation project(':opencvtest')
}
- 添加测试代码:
package com.pkyingy.mp.opencv
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import kotlinx.android.synthetic.main.activity_main.*
import org.opencv.android.OpenCVLoader
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Log.e("Check", "OpenCv configured ")
if (OpenCVLoader.initDebug()) {
tv_tv.text = "OpenCv configured successfully"
Log.e("Check", "OpenCv configured successfully")
} else {
tv_tv.text = "OpenCv configured faile"
Log.e("Check", "OpenCv configured faile")
}
}
}
可能遇到的错误:
-
kotlin依赖问题
e: /Users/MacBook/Documents/projects/android/opencv/opencvtest/java/src/org/opencv/core/MatAt.kt: (6, 18): Cannot access built-in declaration 'kotlin.Int'. Ensure that you have a dependency on the Kotlin standard library
解决办法:
- 在导入的opencvtest库的build.gradle中找到
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
- 添加 apply plugin: 'kotlin-android-extensions',如下所示
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
-
NDK环境问题
A problem occurred configuring project ':opencv4_5_3'.
> NDK not configured. Download it with SDK manager. Preferred NDK version is '20.0.5594570'. Log: /Users/MacBook/Documents/projects/android/pk-android-libs/opencv4_5_3/.cxx/ndk_locator_record.json
解决办法:
在项目根目录的local.properties文件中添加电脑上的dnk路径
网友评论