How to package and link OpenCV binares and OpenCV

2019-05-09 06:34发布

问题:

Trying to get started with OpenCV Java SDK on Android Studio creating an app targetting 14 - 19.

I'm following steps defined by @Paito here: https://stackoverflow.com/a/20600674/1291489

1) I package libs in a .jar

2) Add dependencies: ... dependencies {

    //OpenCV
    compile files('libs/opencv-2.4.8.jar')
    compile project(':app:libs:opencv')
}

3) Copy the OpenCV java folder

4) Create and fix build.gradle for OpenCV (same as referenced source)

5) Try to use the library

import org.opencv.android.OpenCVLoader;
...
if (!OpenCVLoader.initDebug()) {}

Results: App builds and runs but I get the following exception:

03-19 19:54:46.144  D/OpenCV/StaticHelper﹕ Trying to get library list
03-19 19:54:46.144  E/OpenCV/StaticHelper﹕ OpenCV error: Cannot load info library for OpenCV
03-19 19:54:46.144  D/OpenCV/StaticHelper﹕ Library list: ""
03-19 19:54:46.144  D/OpenCV/StaticHelper﹕ First attempt to load libs
03-19 19:54:46.144  D/OpenCV/StaticHelper﹕ Trying to init OpenCV libs
03-19 19:54:46.144  D/OpenCV/StaticHelper﹕ Trying to load library opencv_java
03-19 19:54:46.144  D/OpenCV/StaticHelper﹕ Cannot load library "opencv_java"
03-19 19:54:46.154  W/System.err java.lang.UnsatisfiedLinkError: Couldn't load opencv_java: findLibrary returned null

Not able to see what's wrong here. I tried every possible way of packaging the .jar file and repeated the steps at least 10 times with tweaks.

I also tried structure:

-opencv.jar
  -libs
    -armeabi-v7a
      -(.so files)

I am testing on a Samsung GS3Mini and Genymotion 4.1.1, and getting same results on both. Both are running OpenCV Manager and samples.

回答1:

You should add the *.so files in /main/jniLibs folder if your gradle plugin is 0.7.2+ and add the opencv java as library, follow the steps which I covered here.

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



回答2:

So close, try this structure:

-opencv.jar -lib -armeabi-v7a -(.so files)

Android Studio:

Update: Looks like @TGMCians has a better solution for you with the new Gradle.