相信各位再開發(fā)中經(jīng)常會用到人臉識別的技術(shù),opencv是其中的一種實現(xiàn)方案溪食,但是需要精通NDK,不然光是配置都搞死一堆人娜扇,不啰嗦了。
第一步:去官網(wǎng)下載:https://sourceforge.net/projects/opencvlibrary/files/opencv-android/3.2.0/opencv-3.2.0-android-sdk.zip/download
第二步:解壓后創(chuàng)建一個androidProject
接著下一步下一步枢析,記得異常支持的兩個勾勾上
第三步:點(diǎn)擊File->importModule(選擇你下載的Opencv-android-sdk)目錄下sdk/java,點(diǎn)擊Ok
第四步:再把導(dǎo)入的類庫添加依賴,我導(dǎo)入的這里項目叫app
第五步:
5.1醒叁,samples目錄下的face-detection中的src復(fù)制到app這個項目的src下(把系統(tǒng)生成的刪除)
5.2辐益,導(dǎo)入layout,和raw
5.3脱吱,把jni目錄下的DetectionBasedTracker_jni.cpp和DetectionBasedTracker_jni.h拷貝到cpp目錄下,
5.4续捂,在main目錄下新建jniLibs目錄,在jniLibs下新建armeabi,再sdk/native/libs/armeabi目錄下的libopencv_java3.so宦搬,拷貝到新建的jniLibs目下的armeabi,
5.5矾克,將sdk/native/jni/include文件下的所有東西憔足,全部考入到j(luò)niLibs目錄下。
完成后的結(jié)構(gòu)如下圖
第六步:在CMakeList.txt文件中寫入(原來文件中的全部刪除控妻,復(fù)制粘貼下面的即可弓候,這是NDK的配置,在這里就不多解釋他匪,老司機(jī)一看就明白)
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/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)
# 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(
opencv_java3
SHARED
IMPORTED)
set_target_properties(
opencv_java3
PROPERTIES IMPORTED_LOCATION
../../../../src/main/jniLibs/armeabi/libopencv_java3.so)
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
if(CMAKE_COMPILER_IS_GNUCXX)
set(CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}")
message(STATUS "optional:-std=c++11")
endif(CMAKE_COMPILER_IS_GNUCXX)
include_directories(src/main/jniLibs/include)
add_library( # Sets the name of the library.
detection_based_tracker
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/cpp/DetectionBasedTracker_jni.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.
detection_based_tracker opencv_java3
# Links the target library to the log library
# included in the NDK.
${log-lib} )
第七步:在build-gradle文件中加入依鸥,接著同步一下畦徘,檢查是否報錯(記得加相機(jī)權(quán)限喲)
第八步:記得運(yùn)行成功后是需要添加Manager的,但是按照下面做就不需要了(最重要一步)井辆。
8.1在加載類庫的前面加上
8.2并且注釋掉
在點(diǎn)擊運(yùn)行杯缺,這次就不需要Manager了,記得沒有申請運(yùn)行時權(quán)限的請在應(yīng)用管理中打開喲袍榆,至于,相機(jī)全屏的宿崭,請參考它的demo,確實不會就復(fù)制它的AndroidManifest.xml文件到覆蓋你自己的就行才写。
第一次寫文章,老司機(jī)和大神們多多關(guān)照讹堤。