Android和JNI之间的参数传递

3

我正在处理在Android应用程序与JNI之间传递参数的问题,使用Java中的OpenCV库,我在Android应用程序代码中编写了以下内容。

Android OpenCV Java 代码:

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

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

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;
}

但是当我试图在C代码中从(Mat)转换为(IplImage *)时,我的应用程序失败了。因此,我的问题是如何将CvRect和Mat对象从我的Android Java代码传递到JNI。有更好的方法吗?
非常感谢。
1个回答

1
似乎Java的Mat对象和C的Mat对象之间存在差异,但是你可以传递你的Java Mat对象存储的本地Mat对象的地址。将你的代码更改为以下内容:

Android OpenCV Java 代码:

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

C 代码:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接