第一步:先到openCV官網(wǎng)現(xiàn)在所需的openCV的版本庫(kù)微谓;地址:https://opencv.org/,選擇相應(yīng)的Android版本進(jìn)行下載
第二步挺身,在AndoridStudio中新建一個(gè)有C++庫(kù)的Android項(xiàng)目拐迁,并在main下新建jniLibs的目錄,把openCV-android-sdk/native/libs目錄下的文件copy到新建的jniLibs目錄中,在app下的build.gradle中defaultConfig中添加
ndk{
? ? abiFilters 'armeabi-v7a','arm64-v8a'
}
并在Android標(biāo)簽下添加
sourSets{
?? ??? ?? ? main{
?? ??? ??? ?? ? jni.srcDirs=['src/main/cpp']
?? ??? ??? ??? ?? ? jniLibs.srcDirs=['src/main/jniLibs']
?? ??? ??? ?}
?? ?}
第三步名党,把openCV-android-sdk/native/jni/目錄下的include目錄copy到項(xiàng)目中的libs目錄下坪哄,然后修改cpp目錄下的CMakeLists.txt文件
在里面添加
include_directories(../../../libs/opencv_include)
add_library( lib_opencv SHARED IMPORTED )
set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/../jniLibs/${ANDROID_ABI}/libopencv_java4.so)
其中注意 PROPERTIES IMPORTED_LOCATION 后面是添加的項(xiàng)目的相對(duì)地址
在target_link_libraries標(biāo)簽內(nèi)添加lib_opencv,具體如下:
target_link_libraries( # Specifies the target library.
???????????????????????native-lib
???????????????????????lib_opencv//這里的名字是add_library中的lib_opencv保持一致
???????????????????????# Links the target library to the log library
???????????????????????# included in the NDK.
???????????????????????${log-lib} )
第四步:把openCV-android-simples目錄下的face-detection目錄中的res中的raw目錄copy到項(xiàng)目的res目錄下质蕉,這個(gè)是人臉識(shí)別的模型對(duì)比文件势篡,然后把src中的DetectionBasedTracker和FdActivity copy 到項(xiàng)目目錄中
第五步,在cpp目錄中新建一個(gè)C++類模暗,命名為DetectionBasedTracker_jni禁悠,然后修改CMakeLists.txt文件
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).
?????????????DetectionBasedTracker_jni.cpp //這里之前是native-lib.cpp修改為當(dāng)前新建的cpp文件
)
第六步:把openCV-android/simples/face-detection/jni目錄下的DectectionBaseTracker_jni.cpp和.h文件中的方法copy到項(xiàng)目中新建的DetectionBasedTracker_jni文件中,如果不想修改里面內(nèi)容兑宇,需在項(xiàng)目中新建org.opencv.samples.facedetect包碍侦,把DetectionBasedTracker和FdActivity放到里面,并把FdActivity作為啟動(dòng)頁(yè)隶糕,修改FdActivity中的BaseLoaderCallback監(jiān)聽(tīng)內(nèi)的System.loadLibrary("native-lib")
第七步:在AndroidManifest.xml文件中添加:
????<uses-permission android:name="android.permission.CAMERA"/>
????<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
????<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
????<supports-screens android:resizeable="true"
????????android:smallScreens="true"
????????android:normalScreens="true"
????????android:largeScreens="true"
????????android:anyDensity="true" />
????<uses-feature android:name="android.hardware.camera" android:required="false"/>
????<uses-feature android:name="android.hardware.camera.autofocus" android:required="false"/>
????<uses-feature android:name="android.hardware.camera.front" android:required="false"/>
????<uses-feature android:name="android.hardware.camera.front.autofocus" android:required="false"/>
第八步:將openCV-android/sdk/java復(fù)制粘貼到項(xiàng)目目錄下以第三方庫(kù)的形式引入瓷产,因?yàn)槭莈clipse項(xiàng)目,引用時(shí)AndroidStudio會(huì)自動(dòng)生成gradle枚驻,把build.gradle中apply plugin:'com.android.library',并刪除defaultConfig中的applicationId即可編譯通過(guò)濒旦,
openCV提供的人臉識(shí)別默認(rèn)采用的是后置攝像頭,可以在onCreate方法中設(shè)置如下代碼测秸,即可實(shí)現(xiàn)前置攝像頭的使用疤估,當(dāng)為0是即為后置攝像頭? ? ? ??
?? ??? ?mOpenCvCameraView.setCameraIndex(1);
????????mOpenCvCameraView.enableView();
集成完成后,如需其他功能可根據(jù)openCV提供的api文檔進(jìn)行添加使用霎冯。