Android Studio 导入OpenCV 并调试运行face-detection例子

将相本无种,男儿当自强。这篇文章主要讲述Android Studio 导入OpenCV 并调试运行face-detection例子相关的知识,希望能为你提供帮助。
p { margin-bottom: 0.1in; direction: ltr; color: rgb(0, 0, 10); line-height: 120%; text-align: justify } p.western { font-family: "Calibri", serif; font-size: 10pt } p.cjk { font-family: "Droid Sans Fallback"; font-size: 10pt } p.ctl { font-size: 12pt } a:link { color: rgb(0, 0, 255) }android Studio 导入OpenCV 并调试运行face-detection例子


系统:Ubuntu 14.04
Studio版本:2.3.3
OpenCV版本:2.4.11
 
 
p { margin-bottom: 0.1in; direction: ltr; color: rgb(0, 0, 10); line-height: 120%; text-align: justify } p.western { font-family: "Calibri", serif; font-size: 10pt } p.cjk { font-family: "Droid Sans Fallback"; font-size: 10pt } p.ctl { font-size: 12pt } a:link { color: rgb(0, 0, 255) }第一部分 将OpenCV导入到项目中
 
p { margin-bottom: 0.1in; direction: ltr; color: rgb(0, 0, 10); line-height: 120%; text-align: justify } p.western { font-family: "Calibri", serif; font-size: 10pt } p.cjk { font-family: "Droid Sans Fallback"; font-size: 10pt } p.ctl { font-size: 12pt } code.cjk { font-family: "Droid Sans Fallback", monospace } a:link { color: rgb(0, 0, 255) } 
1.从Google Android 开发中文网站上下载 Android Studio,下载完成后配置SDK,NDK
 
下载地址:https://developer.android.google.cn/studio/index.html
 
 
2.下载OpenCV android开发SDK,并解压
 
文件名:OpenCV-2.4.11-android-sdk.zip
 
下载地址:http://sourceforge.net/projects/opencvlibrary/files/opencv-android/2.4.11/OpenCV-2.4.11-android-sdk.zip/download
 


 
3.新创建一个的项目
 
Name :FaceDetection
 
Minimum SDK : API22
 
Empty Activity : MainActivity
 


 
4.将OpenCV导入项目中
 
File-> New-> import Module
 
Source-directory:{unzip-dir}/adk/java
 
Module name:默认不用修改

Android Studio 导入OpenCV 并调试运行face-detection例子

文章图片

p { margin-bottom: 0.1in; direction: ltr; color: rgb(0, 0, 10); line-height: 120%; text-align: justify } p.western { font-family: "Calibri", serif; font-size: 10pt } p.cjk { font-family: "Droid Sans Fallback"; font-size: 10pt } p.ctl { font-size: 12pt } a:link { color: rgb(0, 0, 255) }NEXT
这一页全部默认,然后Finish
Android Studio 导入OpenCV 并调试运行face-detection例子

文章图片

p { margin-bottom: 0.1in; direction: ltr; color: rgb(0, 0, 10); line-height: 120%; text-align: justify } p.western { font-family: "Calibri", serif; font-size: 10pt } p.cjk { font-family: "Droid Sans Fallback"; font-size: 10pt } p.ctl { font-size: 12pt } code.cjk { font-family: "Droid Sans Fallback", monospace } a:link { color: rgb(0, 0, 255) }可能会报如下错误
原因是OpenCV的build.gradle里指定的Android SDK 版本跟 本项目的build.gradle里设置的版本不一致
Android Studio 导入OpenCV 并调试运行face-detection例子

文章图片

p { margin-bottom: 0.1in; direction: ltr; color: rgb(0, 0, 10); line-height: 120%; text-align: justify } p.western { font-family: "Calibri", serif; font-size: 10pt } p.cjk { font-family: "Droid Sans Fallback"; font-size: 10pt } p.ctl { font-size: 12pt } code.cjk { font-family: "Droid Sans Fallback", monospace } a:link { color: rgb(0, 0, 255) }将两个文件里面下面这几个配置改成一样:
compileSDKVersion / buildToolsVersion / minSdkVersion / targetSdkVersion
然后在OpenCV的build.gradle点 Try Again,错误解决。
Android Studio 导入OpenCV 并调试运行face-detection例子

文章图片

 
p { margin-bottom: 0.1in; direction: ltr; color: rgb(0, 0, 10); line-height: 120%; text-align: justify } p.western { font-family: "Calibri", serif; font-size: 10pt } p.cjk { font-family: "Droid Sans Fallback"; font-size: 10pt } p.ctl { font-size: 12pt } code.cjk { font-family: "Droid Sans Fallback", monospace } a:link { color: rgb(0, 0, 255) }5.复制OpenCV Native lib到项目中
复制{unzip-dir}/sdk/native/libs到项目{project_name}/openCVLibrary2441/src/main/目录下,重命名为jniLibs
Android Studio 导入OpenCV 并调试运行face-detection例子

文章图片

p { margin-bottom: 0.1in; direction: ltr; color: rgb(0, 0, 10); line-height: 120%; text-align: justify } p.western { font-family: "Calibri", serif; font-size: 10pt } p.cjk { font-family: "Droid Sans Fallback"; font-size: 10pt } p.ctl { font-size: 12pt } a:link { color: rgb(0, 0, 255) }6.设置Module依赖
p { margin-bottom: 0.1in; direction: ltr; color: rgb(0, 0, 10); line-height: 120%; text-align: justify } p.western { font-family: "Calibri", serif; font-size: 10pt } p.cjk { font-family: "Droid Sans Fallback"; font-size: 10pt } p.ctl { font-size: 12pt } a:link { color: rgb(0, 0, 255) }File-> Project Structure-> app-> Dependencies
Android Studio 导入OpenCV 并调试运行face-detection例子

文章图片

p { margin-bottom: 0.1in; direction: ltr; color: rgb(0, 0, 10); line-height: 120%; text-align: justify } p.western { font-family: "Calibri", serif; font-size: 10pt } p.cjk { font-family: "Droid Sans Fallback"; font-size: 10pt } p.ctl { font-size: 12pt } a:link { color: rgb(0, 0, 255) }点+号-> 3.Module dependency-> 选:openCVLibrary2411
Android Studio 导入OpenCV 并调试运行face-detection例子

文章图片

p { margin-bottom: 0.1in; direction: ltr; color: rgb(0, 0, 10); line-height: 120%; text-align: justify } p.western { font-family: "Calibri", serif; font-size: 10pt } p.cjk { font-family: "Droid Sans Fallback"; font-size: 10pt } p.ctl { font-size: 12pt } a:link { color: rgb(0, 0, 255) }7.如果APP是5.0以上的话,需要修改FaceDetection/openCVLibrary2411/src/main/java/org/opencv/android/AsyncServiceHelper.java中的initOpenCV方法.
p { margin-bottom: 0.1in; direction: ltr; color: rgb(0, 0, 10); line-height: 120%; text-align: justify } p.western { font-family: "Calibri", serif; font-size: 10pt } p.cjk { font-family: "Droid Sans Fallback"; font-size: 10pt } p.ctl { font-size: 12pt } a:link { color: rgb(0, 0, 255) }新方法代码如下:
public static boolean initOpenCV(String Version, final Context AppContext, final LoaderCallbackInterface Callback) { AsyncServiceHelper helper = new AsyncServiceHelper(Version, AppContext, Callback); Intent intent = new Intent("org.opencv.engine.BIND"); intent.setPackage("org.opencv.engine"); if (AppContext.bindService(intent, helper.mServiceConnection, Context.BIND_AUTO_CREATE)) { return true; } else { AppContext.unbindService(helper.mServiceConnection); InstallService(AppContext, Callback); return false; } }

p { margin-bottom: 0.1in; direction: ltr; color: rgb(0, 0, 10); line-height: 120%; text-align: justify } p.western { font-family: "Calibri", serif; font-size: 10pt } p.cjk { font-family: "Droid Sans Fallback"; font-size: 10pt } p.ctl { font-size: 12pt } code.cjk { font-family: "Droid Sans Fallback", monospace } a:link { color: rgb(0, 0, 255) }到此为止就把OpenCV SDK加载到项目中了,可以在项目使用OpenCV了。
 
 
p { margin-bottom: 0.1in; direction: ltr; color: rgb(0, 0, 10); line-height: 120%; text-align: justify } p.western { font-family: "Calibri", serif; font-size: 10pt } p.cjk { font-family: "Droid Sans Fallback"; font-size: 10pt } p.ctl { font-size: 12pt } code.cjk { font-family: "Droid Sans Fallback", monospace } a:link { color: rgb(0, 0, 255) } 
 
p { margin-bottom: 0.1in; direction: ltr; color: rgb(0, 0, 10); line-height: 120%; text-align: justify } p.western { font-family: "Calibri", serif; font-size: 10pt } p.cjk { font-family: "Droid Sans Fallback"; font-size: 10pt } p.ctl { font-size: 12pt } code.cjk { font-family: "Droid Sans Fallback", monospace } a:link { color: rgb(0, 0, 255) } 
第二部分 调试运行OpenCV SDK中的 face-detection例子
 
p { margin-bottom: 0.1in; direction: ltr; color: rgb(0, 0, 10); line-height: 120%; text-align: justify } p.western { font-family: "Calibri", serif; font-size: 10pt } p.cjk { font-family: "Droid Sans Fallback"; font-size: 10pt } p.ctl { font-size: 12pt } code.cjk { font-family: "Droid Sans Fallback", monospace } a:link { color: rgb(0, 0, 255) } 
1.face-detectionjava代码和res资源复制到项目 app下对应的目录中
 
{unzip-dir}/OpenCV-android-sdk/samples/face-detection/src/org/opencv/samples/facedetect
 
{unzip-dir}/OpenCV-android-sdk/samples/face-detection/res
Android Studio 导入OpenCV 并调试运行face-detection例子

文章图片

 
 
p { margin-bottom: 0.1in; direction: ltr; color: rgb(0, 0, 10); line-height: 120%; text-align: justify } p.western { font-family: "Calibri", serif; font-size: 10pt } p.cjk { font-family: "Droid Sans Fallback"; font-size: 10pt } p.ctl { font-size: 12pt } code.cjk { font-family: "Droid Sans Fallback", monospace } a:link { color: rgb(0, 0, 255) }2. 编译C++代码
face-detectionC++代码目录复制到app/main
{unzip-dir}/OpenCV-android-sdk/samples/face-detection/jni
Android Studio 导入OpenCV 并调试运行face-detection例子

文章图片

 
p { margin-bottom: 0.1in; direction: ltr; color: rgb(0, 0, 10); line-height: 120%; text-align: justify } p.western { font-family: "Calibri", serif; font-size: 10pt } p.cjk { font-family: "Droid Sans Fallback"; font-size: 10pt } p.ctl { font-size: 12pt } code.cjk { font-family: "Droid Sans Fallback", monospace } a:link { color: rgb(0, 0, 255) }将编译用的文件复制到项目根目录
{unzip-dir}/OpenCV-android-sdk/sdk/native
Android Studio 导入OpenCV 并调试运行face-detection例子

文章图片

p { margin-bottom: 0.1in; direction: ltr; color: rgb(0, 0, 10); line-height: 120%; text-align: justify } p.western { font-family: "Calibri", serif; font-size: 10pt } p.cjk { font-family: "Droid Sans Fallback"; font-size: 10pt } p.ctl { font-size: 12pt } a:link { color: rgb(0, 0, 255) }修改jni下的Android.mk文件,内容如下
注意OpenCV.mk的路径(红字部分)
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) OpenCV_INSTALL_MODULES := on OpenCV_CAMERA_MODULES := off OPENCV_LIB_TYPE :=STATIC ifeq ("$(wildcard $(OPENCV_MK_PATH))","") include ../../../../native/jni/OpenCV.mk else include $(OPENCV_MK_PATH) endif LOCAL_MODULE := detection_based_tracker LOCAL_SRC_FILES :=DetectionBasedTracker_jni.cpp LOCAL_LDLIBS +=-lm -llog include $(BUILD_SHARED_LIBRARY)

p { margin-bottom: 0.1in; direction: ltr; color: rgb(0, 0, 10); line-height: 120%; text-align: justify } p.western { font-family: "Calibri", serif; font-size: 10pt } p.cjk { font-family: "Droid Sans Fallback"; font-size: 10pt } p.ctl { font-size: 12pt } a:link { color: rgb(0, 0, 255) } 
将DetectionBasedTracker_jni.cpp 和DetectionBasedTracker_jni.h里,对应的包信息根据项目的实际情况修改一下,内容如下红色部分:(会有很多处,建议整体替换)
p { margin-bottom: 0.1in; direction: ltr; color: rgb(0, 0, 10); line-height: 120%; text-align: justify } p.western { font-family: "Calibri", serif; font-size: 10pt } p.cjk { font-family: "Droid Sans Fallback"; font-size: 10pt } p.ctl { font-size: 12pt } a:link { color: rgb(0, 0, 255) }原:JNIEXPORT jlong JNICALL Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeCreateObject
 
p { margin-bottom: 0.1in; direction: ltr; color: rgb(0, 0, 10); line-height: 120%; text-align: justify } p.western { font-family: "Calibri", serif; font-size: 10pt } p.cjk { font-family: "Droid Sans Fallback"; font-size: 10pt } p.ctl { font-size: 12pt } a:link { color: rgb(0, 0, 255) } 
现:JNIEXPORT jlong JNICALL Java_cn_com_grufield_facedetection_DetectionBasedTracker_nativeCreateObject
 
Android Studio 导入OpenCV 并调试运行face-detection例子

文章图片

 
p { margin-bottom: 0.1in; direction: ltr; color: rgb(0, 0, 10); line-height: 120%; text-align: justify } p.western { font-family: "Calibri", serif; font-size: 10pt } p.cjk { font-family: "Droid Sans Fallback"; font-size: 10pt } p.ctl { font-size: 12pt } code.cjk { font-family: "Droid Sans Fallback", monospace } a:link { color: rgb(0, 0, 255) }在app中的build.gradledeandroid标签中加入如下内容:
 
sourceSets.main.jni.srcDirs = [] sourceSets.main.jniLibs.srcDirs = [‘src/main/libs‘,‘src/main/jniLibs‘]task ndkBuild(type: Exec, description: ‘Compile JNI source with NDK‘) { Properties properties = new Properties() properties.load(project.rootProject.file(‘local.properties‘).newDataInputStream()) def ndkDir = properties.getProperty(‘ndk.dir‘) if (org.apache.tools.ant.taskdefs.condition.Os.isFamily(org.apache.tools.ant.taskdefs.condition.Os.FAMILY_WINDOWS)) { commandLine "$ndkDir/ndk-build.cmd", ‘-C‘, file(‘src/main/jni‘).absolutePath } else { commandLine "$ndkDir/ndk-build", ‘-C‘, file(‘src/main/jni‘).absolutePath } } tasks.withType(JavaCompile) { compileTask -> compileTask.dependsOn ndkBuild } task ndkClean(type: Exec, description: ‘Clean NDK Binaries‘) { Properties properties = new Properties() properties.load(project.rootProject.file(‘local.properties‘).newDataInputStream()) def ndkDir = properties.getProperty(‘ndk.dir‘) if (org.apache.tools.ant.taskdefs.condition.Os.isFamily(org.apache.tools.ant.taskdefs.condition.Os.FAMILY_WINDOWS)) { commandLine "$ndkDir/ndk-build.cmd",‘clean‘, ‘-C‘, file(‘src/main/jni‘).absolutePath } else { commandLine "$ndkDir/ndk-build",‘clean‘, ‘-C‘, file(‘src/main/jni‘).absolutePath } } clean.dependsOn ‘ndkClean‘

Android Studio 导入OpenCV 并调试运行face-detection例子

文章图片

p { margin-bottom: 0.1in; direction: ltr; color: rgb(0, 0, 10); line-height: 120%; text-align: justify } p.western { font-family: "Calibri", serif; font-size: 10pt } p.cjk { font-family: "Droid Sans Fallback"; font-size: 10pt } p.ctl { font-size: 12pt } a:link { color: rgb(0, 0, 255) }在右面Gradle projects中找到ndkBuild,双击ndkBuild,编译C++代码
FaceDetection-> :app-> Taks-> other-> ndkBuild
Android Studio 导入OpenCV 并调试运行face-detection例子

文章图片

 
p { margin-bottom: 0.1in; direction: ltr; color: rgb(0, 0, 10); line-height: 120%; text-align: justify } p.western { font-family: "Calibri", serif; font-size: 10pt } p.cjk { font-family: "Droid Sans Fallback"; font-size: 10pt } p.ctl { font-size: 12pt } a:link { color: rgb(0, 0, 255) }log如下则编译成功
Android Studio 导入OpenCV 并调试运行face-detection例子

文章图片

 
p { margin-bottom: 0.1in; direction: ltr; color: rgb(0, 0, 10); line-height: 120%; text-align: justify } p.western { font-family: "Calibri", serif; font-size: 10pt } p.cjk { font-family: "Droid Sans Fallback"; font-size: 10pt } p.ctl { font-size: 12pt } code.cjk { font-family: "Droid Sans Fallback", monospace } a:link { color: rgb(0, 0, 255) }3.修改AndroidManifest.xml文件
{unzip-dir}/OpenCV-android-sdk/samples/face-detection/AndroidManifest.xml中内容替换项目到项目的AndroidManifest.xml中
p { margin-bottom: 0.1in; direction: ltr; color: rgb(0, 0, 10); line-height: 120%; text-align: justify } p.western { font-family: "Calibri", serif; font-size: 10pt } p.cjk { font-family: "Droid Sans Fallback"; font-size: 10pt } p.ctl { font-size: 12pt } a:link { color: rgb(0, 0, 255) }内容如下:
 
< application android:label="@string/app_name" android:icon="@drawable/icon" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" > < activity android:name="FdActivity" android:label="@string/app_name" android:screenOrientation="landscape" android:configChanges="keyboardHidden|orientation"> < intent-filter> < action android:name="android.intent.action.MAIN" /> < category android:name="android.intent.category.LAUNCHER" /> < /intent-filter> < /activity> < /application> < supports-screens android:resizeable="true" android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:anyDensity="true" /> < uses-sdk android:minSdkVersion="8" /> < uses-permission android:name="android.permission.CAMERA"/> < 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"/>

 
Android Studio 导入OpenCV 并调试运行face-detection例子

文章图片

 
  到此为止导入完成
 
p { margin-bottom: 0.1in; direction: ltr; color: rgb(0, 0, 10); line-height: 120%; text-align: justify } p.western { font-family: "Calibri", serif; font-size: 10pt } p.cjk { font-family: "Droid Sans Fallback"; font-size: 10pt } p.ctl { font-size: 12pt } a:link { color: rgb(0, 0, 255) } p { margin-bottom: 0.1in; direction: ltr; color: rgb(0, 0, 10); line-height: 120%; text-align: justify } p.western { font-family: "Calibri", serif; font-size: 10pt } p.cjk { font-family: "Droid Sans Fallback"; font-size: 10pt } p.ctl { font-size: 12pt } a:link { color: rgb(0, 0, 255) }4.运行程序
Android Studio 导入OpenCV 并调试运行face-detection例子

文章图片

 
运行结果如下:
 
 
OK完成了
【Android Studio 导入OpenCV 并调试运行face-detection例子】 




    推荐阅读