Unable to link native library in OpenCV Android sample

后端 未结 3 902
猫巷女王i
猫巷女王i 2020-12-05 11:43

I have OpenCV code (c++), which I want to use in Android. To do this I have to use Android NDK. I downloaded OpenCV package for Android development (ver. 2.4.0) and did all

相关标签:
3条回答
  • 2020-12-05 12:21

    Instead of loading your native library as

    static{
        System.loadLibrary("YOUR_LIBRARY");
    }
    

    load your library after opencv manager is connected in "onManagerConnected" method in you "BaseLoaderCallBack". Following is my code snippet working for me

    public void onManagerConnected(int status) {
                    switch(status){
                    case LoaderCallbackInterface.SUCCESS:
                        Toast.makeText(getApplicationContext(), "manager connected", Toast.LENGTH_LONG).show();
                        System.loadLibrary("MYNATIVELIB");
                        break;
                    default:
                        super.onManagerConnected(status);
                        break;
                    }
                }
    
    0 讨论(0)
  • 2020-12-05 12:24

    You're at a higher level than the actual problem. See "Getting started with the NDK", when you run

        cd <project>
        <ndk>/ndk-build
    

    ... what does it say? (Remember to use the cygwin window and not a dos prompt).

    0 讨论(0)
  • 2020-12-05 12:34

    Whoohoo!

    Finally I found solution for this problem by myself!

    I decided to debug line:

    System.loadLibrary("native_sample");
    

    To do this I downloaded android source code from Android-SDK and then attached source folder (/opt/android-sdk-linux/sources/android-15) to my project. After this I found that error was:

    Cannot load library: link_image[1936]:    37 could not load needed library 'libopencv_java.so' for 'libhello-jni.so' (load_library[1091]: Library 'libopencv_java.so' not found)
    

    And really this library is not in lib directory. I don't know why but ndk-build ignored it. So i decided to copy and load it manualy. For this I copied libopencv_java.so from /opt/OpenCV-2.4.0/libs/armeabi-v7a and also edited java code:

    static {
        System.loadLibrary("opencv_java"); //load opencv_java lib
        System.loadLibrary("native_sample");
    }
    

    Actually similar problems are:

    1. Can not load Opencv libraries in necessitas
    2. Android OpenCV: cannot dlopen camera wrapper library

    From second solution I found that I can load libraries using dlopen, but I haven't tried it yet.

    So I will write simple bash-script that will do it (just copy) for myself.

    Thanks to all.

    0 讨论(0)
提交回复
热议问题