0

I'm developing an Android application in java, that calls a function in a native c library. The native function requires a pointer to an Opencv Mat type so I need to be able to create a Opencv Mat type in my Java application to pass to the native library.

I'm using Android Studio and I've added the 'opencv library' .jar file to my project's libs folder. So I can import the required Opencv modules and create an Opencv Mat type/access its functions. The problem is at runtime I'm getting the following error:

java.lang.NoClassDefFoundError: org.opencv.core.Mat

I've read that the Opencv .jar file is just a wrapper around native functionality and the application needs 'the native library'. How do I find/add this? What do I need to add to my project so that the Application can find the Opencv Mat class definition at runtime? I only want to use the OpenCV Mat type and nothing else.

I've read tutorials on setting up OpenCV Android, but these require installing the OpenCV Manager App on the Android device but this could be a problem for me because I won't have access to the devices where my app will be installed and the OpenCV Manager App won't be installed on these devices. Instead, is there just an '.so' library that I can add to my Android Studio project which will get bundled with my project and allow the app to find the required Mat class at runtime, which is all I need?

Thanks in advance.

CSharp
  • 1,396
  • 1
  • 18
  • 41

1 Answers1

1

There are 3 ways to include OpenCV in your app

  1. Use NDK to compile opencv and write your own wrapper to include your opencv c++ code.
  2. Import the OpenCV java wrapper module provided by Android Opencv SDK and connect it to Opencv manager installed on your phone.
  3. Import the OpenCV java wrapper module provided by Android Opencv SDK and copy the native libraries in your project.

For option 1, you'll have to setup the ndk project. For 2, 3 you can simply import the opencv android module and initialize it in your code by calling -

    if(!OpenCVLoader.initDebug()) {
        Log.d(TAG, "Internal OpenCV library not found. Using OpenCV Manager for initialization.");
        OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_0_0, this, mLoaderCallback);
    } else {
        Log.d(TAG, "OpenCV library found inside package using it.");
        mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
    }

Refer to following links to setup opencv android sdk in your android studio project -

http://blog.codeonion.com/2015/11/25/creating-a-new-opencv-project-in-android-studio/

OpenCV in Android Studio

Most of the opencv functionalities can be directly accessed using the java wrapper. So you might not need to pass any Mat to native C functions. If you still want to call native functions, you can refer to Opencv Java wrapper for help and even samples provided with SDK for further understanding.

Community
  • 1
  • 1
Gaurav Raj
  • 699
  • 6
  • 21
  • 1
    Ofcourse you can also use other opencv wrapper libraries like JavaCV if you want which includes some other libraries as well. – Gaurav Raj Jun 13 '16 at 13:27
  • Thanks! I think option 3 is probably the one I'm looking for. Just wasn't sure which native libraries to copy or where but the links helped. – CSharp Jun 14 '16 at 08:10