简体   繁体   English

Opencv将Mat从Android传递到JNI错误

[英]Opencv passing Mat from Android to JNI error

I looked up everything but I don't understand why I receive a Fatal Signal 11. 我抬头看了所有东西,但不明白为什么会收到致命信号11。

Java side: Java方面:

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: C ++方面:

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. 我正在尝试计算直方图,但是对帧的任何访问都会导致SegFault。 What am I doing wrong? 我究竟做错了什么?

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. 这里最可能的问题是您在Java中的本机方法声明(尚未列出)与JNI库中的签名不匹配。

You are calling it: 您正在调用它:

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

So you probably have (otherwise javac would not compile): 因此,您可能拥有了(否则javac将无法编译):

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

But in your JNI library you have: 但是在您的JNI库中,您可以:

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. 因此,您mNativeObj传递给image ,将其强制转换为Mat并在实际尝试遵循指针时获取了SIGSEGV。

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). 例如,如果您不需要访问实例,则将静态方法设置为nativeDetect(long image) (并且不要将mNativeObj传递给它)。

Note 注意

You are responsible for making sure that the method signatures match between your Java and C/C++ source files. 您有责任确保Java和C / C ++源文件之间的方法签名匹配。 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. 除非您重载了本机方法(两个签名具有相同的名称),否则动态库加载器仅在寻找Java_{packageName}_{className}_{methodName} ,并且不能说出参数或类型的数量不匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM