以前的文章中《OpenCV簡(jiǎn)介與Android SDK環(huán)境》列出過OpenCV4Android的開發(fā),不過當(dāng)時(shí)是用的Java實(shí)現(xiàn),做了一些Demo后慢慢發(fā)現(xiàn),在圖像處理中用JAVA調(diào)OpenCV的開發(fā)處理速度是個(gè)瓶頸,所以才激起了學(xué)習(xí)NDK開發(fā)的想法,具體NDK開發(fā)的配置可以看我前面的文章《Android NDK編程(一)---NDK介紹及環(huán)境搭建》,本章主要說后面的OpenCV在Android NDK開發(fā)的環(huán)境搭建。
開發(fā)環(huán)境
NDK的搭建 詳見《Android NDK編程(一)---NDK介紹及環(huán)境搭建》 OpenCV下載及準(zhǔn)備 下載地址:https:///releases/ 找到最新版本4.1.0的下載地址,點(diǎn)擊Android進(jìn)入下載 下載完后就是我們下圖紅框中的ZIP文件,然后我們解壓一下后即生成了OpenCV-android-sdk的文件夾(下圖藍(lán)框)
1.創(chuàng)建項(xiàng)目 我們打開Android Studio新建一個(gè)項(xiàng)目,選擇Native C++ 將程序名稱改為OpenCVDemo C++Stand選擇為C++11 點(diǎn)擊Finish后我們完成了項(xiàng)目的創(chuàng)建 2.修改build.gradle文件 雙擊build.gradle文件 修改要我們要支持的CPU架構(gòu)下 調(diào)用OpenCV4Android中的so動(dòng)態(tài)庫用于打包進(jìn)APK,下圖中紅框內(nèi)的路徑就是我們上面下載的OpenCV4.1.0中的動(dòng)態(tài)庫路徑 我們看一下那個(gè)路徑 完整的build.gradle代碼 apply plugin: 'com.android.application' android { compileSdkVersion 28 defaultConfig { applicationId "dem.vac.opencvdemo" minSdkVersion 14 targetSdkVersion 28 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" externalNativeBuild { cmake { cppFlags "-std=c++11" //我們的APP要支持的CPU架構(gòu) abiFilters'x86','armeabi-v7a' } } } //加上 sourceSets{ main{ //當(dāng)前這個(gè)目錄下的庫文件會(huì)被調(diào)用并且被打包進(jìn)apk中 jniLibs.srcDirs = ['D:/PersonalStudio/OpenCV-android-sdk/sdk/native/libs'] } } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } externalNativeBuild { cmake { path "src/main/cpp/CMakeLists.txt" } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' 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' } 3.修改CMakeLists.txt文件 下圖中標(biāo)紅框的地方就是我在原來的CMakeLists.txt中修改的地方 ![]() ![]() 完整的CMakeList.txt代碼 # For more information about using CMake with Android Studio, read the # documentation: https://d./studio/projects/add-native-code.html # Sets the minimum version of CMake required to build the native library. cmake_minimum_required(VERSION 3.4.1) #該變量為真時(shí)會(huì)創(chuàng)建完整版本的Makefile set(CMAKE_VERBOSE_MAKEFILE on) #定義變量ocvlibs使后面的命令可以使用定位具體的庫文件 set(opencvlibs "D:/PersonalStudio/OpenCV-android-sdk/sdk/native/libs") #調(diào)用頭文件的具體路徑 include_directories(D:/PersonalStudio/OpenCV-android-sdk/sdk/native/jni/include) #增加我們的動(dòng)態(tài)庫 add_library(libopencv_java4 SHARED IMPORTED) #建立鏈接 set_target_properties(libopencv_java4 PROPERTIES IMPORTED_LOCATION "${opencvlibs}/${ANDROID_ABI}/libopencv_java4.so") # Creates and names a library, sets it as either STATIC # or SHARED, and provides the relative paths to its source code. # You can define multiple libraries, and CMake builds them for you. # Gradle automatically packages shared libraries with your APK. add_library( # Sets the name of the library. native-lib # Sets the library as a shared library. SHARED # Provides a relative path to your source file(s). native-lib.cpp) # Searches for a specified prebuilt library and stores the path as a # variable. Because CMake includes system libraries in the search path by # default, you only need to specify the name of the public NDK library # you want to add. CMake verifies that the library exists before # completing its build. find_library( # Sets the name of the path variable. log-lib # Specifies the name of the NDK library that # you want CMake to locate. log) # Specifies libraries CMake should link to your target library. You # can link multiple libraries, such as libraries you define in this # build script, prebuilt third-party libraries, or system libraries. target_link_libraries( # Specifies the target library. native-lib libopencv_java4 # Links the target library to the log library # included in the NDK. ${log-lib}) 劃重點(diǎn) 修改完后我們需要點(diǎn)擊選項(xiàng)上的Build-Refresh Linked C++ Projects ![]() 完成后我們展開native-lib下面的includes后里面有個(gè)opencv2已經(jīng)添加進(jìn)來了,如下圖: ![]() ![]() 上面我們的Android Studio通過NDK配置OpenCV已經(jīng)完下了,下面開始我就可以進(jìn)行代碼的編寫了。 -END- |
|