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

系统:Ubuntu 14.04

Studio版本:2.3.3

OpenCV版本:2.4.11

 

 

第一部分 将OpenCV导入到项目中

 

 

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:默认不用修改

NEXT

这一页全部默认,然后Finish

可能会报如下错误

原因是OpenCVbuild.gradle里指定的Android SDK 版本跟 本项目的build.gradle里设置的版本不一致

将两个文件里面下面这几个配置改成一样:

compileSDKVersion / buildToolsVersion / minSdkVersion / targetSdkVersion

然后在OpenCVbuild.gradleTry Again,错误解决。

 

5.复制OpenCV Native libs到项目中

复制{unzip-dir}/sdk/native/libs到项目{project_name}/openCVLibrary2441/src/main/目录下,重命名为jniLibs

6.设置Module依赖

File->Project Structure->app->Dependencies

+->3.Module dependency->选:openCVLibrary2411

7.如果APP5.0以上的话,需要修改

FaceDetection/openCVLibrary2411/src/main/java/org/opencv/android/AsyncServiceHelper.java中的initOpenCV方法.

新方法代码如下:

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

 

 

第二部分 调试运行OpenCV SDK中的 face-detection例子

 

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

 

2. 编译C++代码

face-detectionC++代码目录复制到app/main

{unzip-dir}/OpenCV-android-sdk/samples/face-detection/jni

 

将编译用的文件复制到项目根目录

{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里,对应的包信息根据项目的实际情况修改一下,内容如下红色部分:(会有很多处,建议整体替换)

原:JNIEXPORT jlong JNICALL Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeCreateObject

现:JNIEXPORT jlong JNICALL Java_cn_com_grufield_facedetection_DetectionBasedTracker_nativeCreateObject

 

app中的build.gradle的android标签中加入如下内容:

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

 

log如下则编译成功

 

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

 

 

 到此为止导入完成

 

4.运行程序

 

运行结果如下:

 

 

 

OK完成了

 

参考文章:

http://www.jianshu.com/p/e90da53c4bb5

http://blog.csdn.net/zengxyuyu/article/details/52303659

posted @ 2017-06-26 19:38  grufield  阅读(1799)  评论(2编辑  收藏  举报