環(huán)境搭建
一、創(chuàng)建Android Studio項目
1.打開Android Studio IDE 點擊 File -> New -> New Project

Create New Project.png
2.Empty Activity -> Next -> Finish

Create New Project.png
二、導(dǎo)入 OpenCV Android SDK 依賴項
1.File -> New -> Import Module ->OK
2.選擇OpenCV4的路徑(必須選擇到j(luò)ava目錄上)
例如: D:\OpenCVandroidSDK\sdk\java

Select Source Location.png
3.修改模板名稱
Next -> Finish

Import Module.png
三、修改build.gradle文件
-
apply plugin: 'com.android.application'修改為apply plugin: 'com.android.library'
1.1選擇build.gradle(OpenCV4)
1.2將apply plugin: 'com.android.application'修改為apply plugin: 'com.android.library'
Modify Gradle.png
2.將文件中的自動生成的applicationId "org.opencv"刪除,因為OpenCV4模塊是屬于library而不是application
Modify Gradle.png
3.將compileSdkVersion 、buildToolsVersion、minSdkVersion 、targetSdkVersion 屬性值的設(shè)置與app對應(yīng)的build.gradle文件的配置一致。
OpenCV4.png
app.png
4.添加jniLibs文件夾
1.在 app -> src -> main 文件夾下添加jniLibs
1.1 File -> New -> directory

jniLibs.png
1.2 app目錄如下

Content.png
1.3將OpenCVandroidSDK\sdk\native\libs內(nèi)的架構(gòu)文件復(fù)制到j(luò)niLibs中

copy.png

contentcopy.png
4.配置app中的build.gradle進行配置,增加jniLibs.srcDirs參數(shù)
sourceSets{
main{
jniLibs.srcDirs = ['src/main/jniLibs']
}
}
5.配置支持的ndk類型,把架構(gòu)包寫到app對應(yīng)的build.gradle中
ndk{
abiFilters "arm64-v8a","armeabi-v7a","x86","x86_64"
}
6.OpenCV4 build.gradle
apply plugin: 'com.android.library'
android {
compileSdkVersion 28
buildToolsVersion "28.0.3"
defaultConfig {
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
7.app build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
buildToolsVersion "28.0.3"
defaultConfig {
applicationId "com.example.opencvdemo"
minSdkVersion 28
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
ndk{
abiFilters "arm64-v8a","armeabi-v7a","x86","x86_64"
}
}
sourceSets{
main{
jniLibs.srcDirs = ['src/main/jniLibs']
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
五、添加依賴
1.File -> Project Structure ->Dependencies -> app -> + -> 勾選OpenCV4 -> Ok->OK

select Model.png
六、測試

OpenCVDemo.png



