Android Studio 导入OpenCV 并调试运行face-detection例子
系统:Ubuntu 14.04
Studio版本:2.3.3
OpenCV版本:2.4.11
第一部分 将OpenCV导入到项目中
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:默认不用修改
NEXT
这一页全部默认,然后Finish
可能会报如下错误
原因是OpenCV的build.gradle
里指定的
Android SDK
版本跟 本项目的
build.gradle
里设置的版本不一致
将两个文件里面下面这几个配置改成一样:
compileSDKVersion / buildToolsVersion / minSdkVersion / targetSdkVersion
然后在OpenCV的build.gradle
里
点 Try 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.如果APP是5.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-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
2. 编译C++代码
将face-detection
的
C++
代码目录复制到
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