Android Studio OpenCV

I am currently developing a project in Android Studio and I need to use some OpenCV libraries.

I know Android Studio does not currently support NDK development, but while doing my research, I found there are ways to work around the situation.

I followed this tutorial to add opencv libraries to my project:

How to use opencv in android studio using gradle build tool?

Everything worked fine, but when I tried to test an OpenCV example like "FaceDetection" by doing this:

Go to OpenCV Android SDK, select the sample project you would like to try from.

First, delete the res folder in your own project app / src / main, then put the res folder from samples inside your app / src / main folder.

First remove the java folder from the application / src / main, then copy the src file to the folder from the samples (note that src needs to be renamed to Java).

If you are creating an example with native C ++ files, you need to have the NDK installed. Download it from google developer portal and add it to your local.properties at the top level of your project, below is the sdk.dir line: ndk.dir=/path/to/your/android-ndk

Build and run example!

I got the following errors:

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)

      

the problem is loading the library

// Load native library after(!) OpenCV initialization

System.loadLibrary("detection_based_tracker");

      

What do you propose to solve this problem? I searched all over the net but couldn't find anything.

+3


source to share


2 answers


I ran into a similar issue while installing and configuring OPENCV libraries in AndroidStudio. If this is not a problem, please ignore this answer (I know this was asked a year ago). Hopefully others may find this helpful for quick troubleshooting. The github simple sample uses the OPENCV libraries. Everything is already set up to make it work very quickly. My AndroidStudio 2.0 Preview 2 (released 5 days ago).



Greetings.

0


source


Can you show your app build.gradle? You should check the "sourceSets" settings, the default source for the native module is "jniLibs". So the jniLibs directory should contain the required module, eg. armeabi-v7a, mips, x86, etc.

Here is an example assembly: (Note: I configured "jniLibs" in "libs", just personal configuration ...)

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

      

You can also configure "jniLibs" in "libs" from the application directory as follows:

Ref.

sourceSets {
    main {
        jni.srcDirs = []
        jniLibs.srcDirs=['libs']
    }
}

      

This can be found in the app module, the complete build.gradle app looks like this (example):



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')
}

      

Here's a sample project importing the OpenCV 3.0 Face Detection Sample into Android Studio:

https://github.com/DeLaSalleUniversity-Manila/opencvfacedetection-melvincabatuan

The procedure can be found in README.md .

Also, if you want to try Android Studio Gradle Experimental Plugin for native cpp development with OpenCV, you might be interested in the following sample projects:

0


source







All Articles