我目前正在 Android Studio 中开发一个项目,我需要使用一些 OpenCV 库。
我知道 Android Studio 目前不支持 NDK 开发,但在进行研究时我发现有一些方法可以解决问题。
我按照本教程将 opencv 库添加到我的项目中:
How to use opencv in android studio using gradle build tool?
一切正常,但是当我尝试通过这样做来测试像“FaceDetection”这样的 OpenCV 示例时:
Go to OpenCV Android SDK, pick a sample project that you’d like to try out.
First, delete the ‘res’ folder inside your own project app/src/main, then place the res folder from the samples inside your app/src/main folder.
First, delete the ‘java’ folder from app/src/main, then copy the ‘src’ folder from the samples in there (note, the src has to be renamed to java).
If you building example with native C++ files, you need to have NDK installed. Download it from Google’s developers portal, and add this line to your local.properties in the top-level of your project, below the sdk.dir line:
ndk.dir=/path/to/your/android-ndk
Build and run the example!
我遇到了这些错误:
java.lang.UnsatisfiedLinkError: Couldn't load detection_based_tracker from loader dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.example.ricardonascimento.opencvexamples-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.ricardonascimento.opencvexamples-2, /vendor/lib, /system/lib]]]: findLibrary returned null
at java.lang.Runtime.loadLibrary(Runtime.java:358)
at java.lang.System.loadLibrary(System.java:526)
问题是加载库的时候
// Load native library after(!) OpenCV initialization
System.loadLibrary("detection_based_tracker");
你有什么建议来解决这个问题?我在网上搜索过但没有找到任何东西。
最佳答案
您能展示一下您应用的 build.gradle 吗?您应该检查“sourceSets”设置,默认情况下 native 模块的源是“jniLibs”。因此,“jniLibs”目录应该包含必要的模块,例如。 armeabi-v7a、mips、x86 等
这是一个示例构建:(注意:我将“jniLibs”自定义为“libs”,这只是个人喜好...)
~/AndroidStudioProjects/OpenCV3-FaceDetection/app/jni$ ndk-build
[armeabi-v7a] Compile++ thumb: detection_based_tracker <= DetectionBasedTracker_jni.cpp
[armeabi-v7a] Prebuilt : libopencv_java3.so <= /home/cobalt/Android/OpenCV-android-sdk/sdk/native/jni/../libs/armeabi-v7a/
[armeabi-v7a] SharedLibrary : libdetection_based_tracker.so
/home/cobalt/Android/adt-bundle-linux-x86-20131030/android-ndk-r10d/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86/bin/../lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin/ld: warning: hidden symbol '__aeabi_atexit' in /home/cobalt/Android/adt-bundle-linux-x86-20131030/android-ndk-r10d/sources/cxx-stl/gnu-libstdc++/4.8/libs/armeabi-v7a/thumb/libgnustl_static.a(atexit_arm.o) is referenced by DSO /home/cobalt/AndroidStudioProjects/OpenCV3-FaceDetection/app/obj/local/armeabi-v7a/libopencv_java3.so
[armeabi-v7a] Install : libdetection_based_tracker.so => libs/armeabi-v7a/libdetection_based_tracker.so
[armeabi-v7a] Install : libopencv_java3.so => libs/armeabi-v7a/libopencv_java3.so
您还可以从应用程序目录中将“jniLibs”自定义为“libs”,如下所示:
例如
sourceSets {
main {
jni.srcDirs = []
jniLibs.srcDirs=['libs']
}
}
这个可以在app模块中找到,完整的app build.gradle如下(例):
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "org.opencv.samples.opencv3_facedetection"
minSdkVersion 15
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main {
jni.srcDirs = []
jniLibs.srcDirs=['libs']
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:22.2.1'
compile project(':libraries:opencv')
}
这是一个在 Android Studio 中导入 OpenCV 3.0 人脸检测示例的示例项目:
https://github.com/DeLaSalleUniversity-Manila/opencvfacedetection-melvincabatuan
程序可以在 README.md 中找到.
此外,如果您想尝试使用 OpenCV 进行原生 cpp 开发的 Android Studio 的 Gradle 实验插件,那么您可能会对以下示例项目感兴趣:
- 使用 Gradle 实验插件在 OpenCV 中进行人脸检测(https://github.com/DeLaSalleUniversity-Manila/facedetectionopencvwithgradleexperimentalplugin-melvincabatuan)
- 带有 Android 实验性 Gradle 插件的 Canny Edge OpenCV (https://github.com/DeLaSalleUniversity-Manila/cannyedgeopencvwithandroidexperimentalgradle-melvincabatuan)
- 颜色减少(https://github.com/DeLaSalleUniversity-Manila/colorreduction-melvincabatuan)
- ORB 具有 OpenCV 和 Android 实验性 Gradle 插件 (https://github.com/DeLaSalleUniversity-Manila/orbfeaturesopencvwithexperimentalgradleplugin-melvincabatuan)
- BRISK 功能 OpenCV 与 Android 实验性 Gradle 插件 (https://github.com/DeLaSalleUniversity-Manila/briskfeaturesopencvwithexperimentalgradleplugin-melvincabatuan)
- AKAZE 具有 OpenCV 和 Android 实验性 Gradle 插件 (https://github.com/DeLaSalleUniversity-Manila/akazefeaturesopencvwithexperimentalgradleplugin-melvincabatuan)
关于java - Android Studio OpenCV 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26282608/