首页 > 代码库 > Android Studio 导入OpenCV 并调试运行face-detection例子
Android Studio 导入OpenCV 并调试运行face-detection例子
Android Studio 导入OpenCV 并调试运行face-detection例子
系统:Ubuntu 14.04
Studio版本:2.3.3
OpenCV版本:2.4.11
<style>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) }</style>
第一部分 将OpenCV导入到项目中
<style>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) }</style>
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:默认不用修改
<style>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) }</style>
NEXT
这一页全部默认,然后Finish
<style>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) }</style>可能会报如下错误
原因是OpenCV的build.gradle
里指定的
Android SDK
版本跟 本项目的
build.gradle
里设置的版本不一致
<style>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) }</style>
将两个文件里面下面这几个配置改成一样:
compileSDKVersion / buildToolsVersion / minSdkVersion / targetSdkVersion
然后在OpenCV的build.gradle
里
点 Try Again,错误解决。
<style>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) }</style>
5.复制OpenCV Native lib到项目中
复制{unzip-dir}/sdk/native/libs
到项目{project_name}/openCVLibrary2441/src/main/
目录下,重命名为jniLibs
6.设置Module依赖
<style>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) }</style>File->Project Structure->app->Dependencies
<style>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) }</style>点+号->3.Module dependency->选:openCVLibrary2411
<style>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) }</style>7.如果APP是5.0以上的话,需要修改FaceDetection/openCVLibrary2411/src/main/java/org/opencv/android/AsyncServiceHelper.java中的initOpenCV方法.
<style>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) }</style>新方法代码如下:
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; } }
到此为止就把
OpenCV SDK
加载到项目中了,可以在项目使用
OpenCV
了。
<style>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) }</style>
<style>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) }</style>
第二部分 调试运行
OpenCV SDK
中的
face-detection
例子
<style>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) }</style>
1.
将
face-detection
的
java
代码和
res
资源复制到项目
app
下对应的目录中
{unzip-dir}
/OpenCV-android-sdk/samples/face-detection/src/org/opencv/samples/facedetect
{unzip-dir}
/OpenCV-android-sdk/samples/face-detection/res
<style>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) }</style>
2. 编译C++代码
将face-detection
的
C++
代码目录复制到
app/main
中
{unzip-dir}
/OpenCV-android-sdk/samples/face-detection/jni
<style>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) }</style>
将编译用的文件复制到项目根目录
{unzip-dir}/OpenCV-android-sdk/sdk/native
修改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)
将DetectionBasedTracker_jni.cpp 和DetectionBasedTracker_jni.h里,对应的包信息根据项目的实际情况修改一下,内容如下红色部分:(会有很多处,建议整体替换)
<style>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) }</style>原:JNIEXPORT jlong JNICALL Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeCreateObject
<style>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) }</style>
现:JNIEXPORT jlong JNICALL Java_cn_com_grufield_facedetection_DetectionBasedTracker_nativeCreateObject
<style>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) }</style>
在app中的build.gradle
deandroid标签中加入如下内容:
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‘
在右面Gradle projects中找到ndkBuild,双击ndkBuild,编译C++代码
FaceDetection->:app->Taks->other->ndkBuild
<style>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) }</style>
log如下则编译成功
<style>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) }</style>
3.修改AndroidManifest.xml文件
将{unzip-dir}
/OpenCV-android-sdk/samples/face-detection/AndroidManifest.xml中内容替换项目到项目的AndroidManifest.xml中
内容如下:
<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"/>
到此为止导入完成
<style>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) }</style> <style>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) }</style>
4.运行程序
运行结果如下:
OK完成了
Android Studio 导入OpenCV 并调试运行face-detection例子