1.下載安裝Android Studio(過程略)。html
2.下載Android OpenCV:https://opencv.org/releases.html,找到Android pack點擊下載。android
下載後解壓獲得以下文件,其中sdk文件夾就是Android studio集成OpenCV所需的文件。app
1. 在Android studio中建立項目,並在file -> new -> import module中導入OpenCV模塊,如圖ide
在source directory中選擇剛纔解壓OpenCV文件路徑-> sdk -> Java,以下,點擊finish按鈕便可。測試
可能出現以下錯誤:點擊藍色字體,安裝相關文件便可。字體
模塊導入成功後,項目文件夾會出現以下目錄:gradle
2.添加依賴,點擊file -> project structure,點擊moudle下app,選擇dependencies,ui
選擇剛纔下載的OpenCV庫,spa
添加成功後以下。3d
添加依賴後,咱們就能夠在項目中使用OpenCV API了,可是若是如今運行APP,會提示安裝OpenCV manager,代表此時OpenCV集成還未成功,由於本身的APP沒法獨立安裝必須使用OpenCV manager這個APP才能運行,爲了免去這個繁瑣條件,咱們繼續執行下面的步驟。
1. 將OpenCVLibrary的build.gradle文件中的一些參數修改成與app的build.gradle文件中相同(紅框標出的部分爲須要修改的部分):
點擊Sync按鈕,出現以下提示:
按提示註釋掉相應內容,
點擊Sync則顯示build成功。
2. 將解壓的OpenCV文件夾目錄下的sdk -> native -> libs中的文件所有拷到項目的lib目錄下,
3.在app的gradle文件中添加以下代碼:
build.gradle文件的所有代碼以下:
apply plugin: 'com.android.application' android { compileSdkVersion 28 defaultConfig { applicationId "com.example.white.opencvmyapplication" minSdkVersion 15 targetSdkVersion 28 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" externalNativeBuild { cmake { cppFlags "" } } } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') implementation 'com.android.support:appcompat-v7:28.0.0-rc01' implementation 'com.android.support.constraint:constraint-layout:1.1.2' compile fileTree(dir:"$buildDir/native-libs",include:'native-libs.jar') //testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' implementation project(':openCVLibrary342') } task nativeLibsToJar(type: Jar, description: 'create a jar archive of the native libs') { destinationDir file("$buildDir/native-libs") baseName 'native-libs' from fileTree(dir: 'libs', include: '**/*.so') into 'lib/' } tasks.withType(JavaCompile) { compileTask -> compileTask.dependsOn(nativeLibsToJar) } } }
至此,OpenCV的配置就完成了,避免了NDK的繁瑣和依賴第三方APP OpenCV manager,但此配置方式OpenCV的加載必須經過靜態加載的方式。
在mainactivity中添加以下代碼,運行APP,
package com.example.white.opencvmyapplication; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import org.opencv.android.OpenCVLoader; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.activity_main); System.out.println("loading"); initLoadOpenCVLibs(); } private void initLoadOpenCVLibs() { boolean success = OpenCVLoader.initDebug(); if (success) { System.out.println("loading success"); Log.d("test", "initLoadOpenCVLibs:OpenCV加載成功!"); } else { System.out.println("loading failed"); Log.d("test", "initLoadOpenCVLibs:OpenCV加載失敗!"); } } }
在控制檯打印以下,則說明OpenCV集成成功。
轉載請註明出處:https://www.cnblogs.com/White-xzx/p/9563479.html