首页 > 代码库 > Android Studio 导入OpenCV 并调试运行face-detection例子

Android Studio 导入OpenCV 并调试运行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 } a:link { color: rgb(0, 0, 255) }</style>

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,下载完成后配置SDKNDK

 

下载地址: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>

可能会报如下错误

原因是OpenCVbuild.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

然后在OpenCVbuild.gradleTry 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

技术分享

<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>

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.如果APP5.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;
    }
}
<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加载到项目中了,可以在项目使用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-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

技术分享

 

 

<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-detectionC++代码目录复制到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

技术分享

<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>

修改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)
<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>

 

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.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‘

技术分享

<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>

在右面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

<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>

内容如下:

 

    <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例子