Opencv passing Mat from Android to JNI error

BumbleBee

I looked up everything but I don't understand why I receive a Fatal Signal 11.

Java side:

mRgba = inputFrame.rgba();

String nativeTesting = mNativeDetector.getFeatures(mRgba);
Log.e(TAG, nativeTesting);    

// In another class
public String getFeatures(Mat image) {
    Log.e("FRAME", " Rows:" +image.rows());    // This correctly returns the number of rows
    String resultMsg = nativeFeatures(mNativeObj, image.getNativeObjAddr());
    return resultMsg;
}

C++ side:

JNIEXPORT jstring JNICALL Java_com_example_myfacedetection_DetectionBasedTracker_nativeFeatures (JNIEnv* env, jclass, jlong image){

LOGD("NativeFeatures enter");

try {
    Mat* frame = (Mat*) image;
//        if (frame.empty())              // This also results in Fatal Signal
//            LOGD("EMPTY FRAME");


    LOGD("Size: %d", frame->rows);
}
catch(cv::Exception& e)
{
    LOGD("nativeCreateObject caught cv::Exception: %s", e.what());
    jclass je = env->FindClass("org/opencv/core/CvException");
    if(!je)
        je = env->FindClass("java/lang/Exception");
    env->ThrowNew(je, e.what());
}


return (env)->NewStringUTF("Hello from JNI !");
}

I'm trying to calculate histogram, but any access to frame results in SegFault. What am I doing wrong?

szym

Problem

The most likely problem here is that your native method declaration in Java (which you have not listed) does not match the signature in the JNI library.

You are calling it:

String resultMsg = nativeFeatures(mNativeObj, image.getNativeObjAddr())

So you probably have (otherwise javac would not compile):

static native String nativeDetect(long thiz, long image);

But in your JNI library you have:

JNIEXPORT jstring JNICALL Java_{snip}_nativeFeatures (JNIEnv* env, jclass, jlong image)

So you are passing mNativeObj into image, casting it to Mat and getting the SIGSEGV when actually trying to follow pointers.

Solution

To fix this problem update the method signature to match. For example, if you don't need access to the instance, make the static method a nativeDetect(long image) (and don't pass mNativeObj to it).

Note

You are responsible for making sure that the method signatures match between your Java and C/C++ source files. Unless you have overloaded the native method (two signatures for the same name), the dynamic library loader is only looking for Java_{packageName}_{className}_{methodName} and can't tell that the number of arguments or types are mismatched.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Most Efficient way to send Image in YUV_420_888 from Android to JNI in OpenCV Mat

From Java

How to get the Mat object from the Byte[] in openCV android?

From Java

passing string array from java to C with JNI

From Java

Android: Using JNI from NativeActivity

From Java

Problem passing byte[] via jni to C on Android

From Java

Passing a pointer from JNI to Java using a long

From Java

Android OpenCv: Edit ImageView Mat without Reassigning

From Java

JNI: passing integer array from Java to C

From Dev

Android ndk pass OpenCV mat to jni containing double values

From Dev

OpenCV Android - Cannot Resolve Corresponding JNI Function

From Dev

issue passing mat from java to c++ jni android arm64-v8a 32/64 bit

From Dev

Android JNI DETECTED ERROR IN APPLICATION

From Dev

Convert OpenCV mat to Android Bitmap

From Dev

Passing Images to opencv from unity

From Dev

Passing OpenCv Mat Object From Java to C Native

From Dev

Convert Mat to MemoryStream OpenCV for Android

From Dev

Convert Mat to ImageSource OpenCV for Android

From Dev

Android Java Access from JNI

From Dev

error passing arguments from an activity to a fragment in android

From Dev

Save Mat (openCV) to SharedPreferences Android

From Dev

Extract set of mat from another mat with OpenCV

From Dev

Android opencv byte[] to mat to byte[]

From Dev

Fastest approach to send OpenCV Mat from PC to Android through Sockets

From Dev

Getting Mat picture from camera in OpenCV for Android

From Dev

Error after including a 2nd JNI library to my Android project (OpenCV)

From Dev

OpenCV Mat to OpenGL - error on glBindTexture()

From Dev

OpenCV on android, Mat.rows() = 0

From Dev

OpenCV, passing Mat by reference, and change it type

From Dev

Converting cv::Mat to Eigen::Matrix gives compilation error from opencv2/core/eigen.hpp file (OpenCV + Eigen)

Related Related

  1. 1

    Most Efficient way to send Image in YUV_420_888 from Android to JNI in OpenCV Mat

  2. 2

    How to get the Mat object from the Byte[] in openCV android?

  3. 3

    passing string array from java to C with JNI

  4. 4

    Android: Using JNI from NativeActivity

  5. 5

    Problem passing byte[] via jni to C on Android

  6. 6

    Passing a pointer from JNI to Java using a long

  7. 7

    Android OpenCv: Edit ImageView Mat without Reassigning

  8. 8

    JNI: passing integer array from Java to C

  9. 9

    Android ndk pass OpenCV mat to jni containing double values

  10. 10

    OpenCV Android - Cannot Resolve Corresponding JNI Function

  11. 11

    issue passing mat from java to c++ jni android arm64-v8a 32/64 bit

  12. 12

    Android JNI DETECTED ERROR IN APPLICATION

  13. 13

    Convert OpenCV mat to Android Bitmap

  14. 14

    Passing Images to opencv from unity

  15. 15

    Passing OpenCv Mat Object From Java to C Native

  16. 16

    Convert Mat to MemoryStream OpenCV for Android

  17. 17

    Convert Mat to ImageSource OpenCV for Android

  18. 18

    Android Java Access from JNI

  19. 19

    error passing arguments from an activity to a fragment in android

  20. 20

    Save Mat (openCV) to SharedPreferences Android

  21. 21

    Extract set of mat from another mat with OpenCV

  22. 22

    Android opencv byte[] to mat to byte[]

  23. 23

    Fastest approach to send OpenCV Mat from PC to Android through Sockets

  24. 24

    Getting Mat picture from camera in OpenCV for Android

  25. 25

    Error after including a 2nd JNI library to my Android project (OpenCV)

  26. 26

    OpenCV Mat to OpenGL - error on glBindTexture()

  27. 27

    OpenCV on android, Mat.rows() = 0

  28. 28

    OpenCV, passing Mat by reference, and change it type

  29. 29

    Converting cv::Mat to Eigen::Matrix gives compilation error from opencv2/core/eigen.hpp file (OpenCV + Eigen)

HotTag

Archive