Parameter passing between Android and JNI

I am dealing with a parameter passing between an Android app with OpenCV and JNI. Using OpenCV Libraries in Java I have something similar in my android application code.

OpenCV Java Code for Android:

Mat mat; //Mat object with data
Rect rect; //Rect object with data

//call to the native function
int resProc = Native.processImages_native(rect, mat); 

      

Code C:

JNIEXPORT jint JNICALL Java_com_test_Native_processImages_1native
(JNIEnv*, jclass, CvRect, Mat);

...

jint Java_com_test_Native_processImages_1native
(JNIEnv* env, jclass jc, CvRect rect, Mat mat){
    int res = processImages(rect, mat);
    return (jint)res;
}

...

int processImages(CvRect rect, Mat mat)
{               
    IplImage *ipl_Img = &mat.operator IplImage(); // here FAILS
    CvRect rect_value = rect;
}

      

But when I try to do a conversion from (Mat) to (IplImage *) in C code, my application doesn't work. So my question is how to pass CvRect and Mat object from my Java Java code to JNI. Is there a better way to do this?

Many thanks.

+3


source to share


1 answer


There seems to be a difference between Java Mat

and C object Mat

, but you can pass the address of the native object Mat

that is stored in your Java Mat

. Change your code to the following:

OpenCV Java Code for Android:

//call to the native function
int resProc = Native.processImages_native(rect, mat.getNativeObjAddr());

      



C Code:

jint Java_com_test_Native_processImages_1native
(JNIEnv* env, jclass jc, CvRect rect, jlong mat){
    int res = processImages(rect, *((Mat*)mat));
    return (jint)res;
}

      

+1


source







All Articles