Skip to content
Advertisement

Android, opencv – No implementation found for long org.opencv.core.Mat.n_Mat() when trying to initialize Mat

I want to do some things using OpenCV and I’m trying to create Mat object:

import org.opencv.core.CvType;
import org.opencv.core.Mat;
...
Mat imageMat = new Mat(CvType.CV_8U);

On this line I get the error:

2021-04-28 15:13:44.657 23613-23613/xxxx.yyyy E/art: No implementation found for long org.opencv.core.Mat.n_Mat() (tried Java_org_opencv_core_Mat_n_1Mat and Java_org_opencv_core_Mat_n_1Mat__)
2021-04-28 15:13:44.676 23613-23613/xxxx.yyyy D/AndroidRuntime: Shutting down VM
2021-04-28 15:13:44.686 23613-23613/xxxx.yyyy E/AndroidRuntime: FATAL EXCEPTION: main
    Process: xxxx.yyyy, PID: 23613
    java.lang.UnsatisfiedLinkError: No implementation found for long org.opencv.core.Mat.n_Mat() (tried Java_org_opencv_core_Mat_n_1Mat and Java_org_opencv_core_Mat_n_1Mat__)
        at org.opencv.core.Mat.n_Mat(Native Method)
        at org.opencv.core.Mat.<init>(Mat.java:26)
        at org.opencv.core.MatOfPoint2f.<init>(MatOfPoint2f.java:12)
        at xxxx.yyyy.Utils.quadrilateralToRect(Utils.java:77)
        at xxxx.yyyy.activities.CropActivity$1.onClick(CropActivity.java:74)
        at android.view.View.performClick(View.java:5619)
        at android.view.View$PerformClick.run(View.java:22298)
        at android.os.Handler.handleCallback(Handler.java:754)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:165)
        at android.app.ActivityThread.main(ActivityThread.java:6375)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:802)

I have OpenCV as separate module in my android project and have imported it into my main module by adding implementation project(path: ':OpenCV') in its build.gradle in dependencies section.

I have seen one simillar question but it is answered with using OpenCvManager which I wouldn’t like to use in my project. If I’m getting right, my users will be forced to install this manager separately before using my app, right? It is disgousting.

UPD 1

I’ve added

static {
        Log.d(TAG, "OPENCV INITIALIZATION " + (OpenCVLoader.initDebug() ? "SUCCEEDED" : "FAILED"));
    }

in my MainActivity.java. It prints “SUCCEEDED” but the error hasn’t gone. However, it changed:

2021-04-28 21:09:47.798 22656-22656/xxxx.yyyy E/AndroidRuntime: FATAL EXCEPTION: main
    Process: xxxx.yyyy, PID: 22656
    java.lang.UnsupportedOperationException: Native object address is NULL
        at org.opencv.core.Mat.<init>(Mat.java:14)
        at xxxx.yyyy.Utils.quadrilateralToRect(Utils.java:94)
        at xxxx.yyyy.activities.CropActivity$1.onClick(CropActivity.java:74)
        at android.view.View.performClick(View.java:5619)
        at android.view.View$PerformClick.run(View.java:22298)
        at android.os.Handler.handleCallback(Handler.java:754)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:165)
        at android.app.ActivityThread.main(ActivityThread.java:6375)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:802)

UPD 2

Oops error in UPD 1 was because of argument in new Mat(). With no args there is no errors yet. Investigation continues.

Advertisement

Answer

Seems like I have found the solution (worked for OpenCV 3). It was needed to copy folder sdknativelibs from downloaded SDK into the project’s root and name it jniLibs. Each of its forders has single file named libopencv_java3.so, as follows:

jniLibs in project structure

Also, probably, it is useful to add this code to your main activity:

static {
    if (OpenCVLoader.initDebug()) {
        Log.d(TAG, "OPENCV INITIALIZATION SUCCEEDED");
    } else {
        Log.d(TAG, "OPENCV INITIALIZATION FAILED BUT TRYING TO RELOAD");
        System.loadLibrary("opencv_java3");
    }
}

Bad side: these files add 80 Mb to your APK after build. Better solutions are welcome.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement