我有一个JAVA
应用程序,我想将对象作为参数传递给C
代码 using JNI
,并且我想再次从C
代码中接收对象JAVA
using JNI
。
在 JAVA 方面,我只是创建了一个应用程序并将其传递给如下所示的方法
JlibFprint.fp_image_data fpimg = new JlibFprint.fp_image_data(); //object to be pass
//fp_image_data is the static inner class of the class JlibFprint
JlibFprint.fp_image_data fpimg1 = new JlibFprint.fp_image_data(); //received object
这个对象被传递给类似的方法
fpimg1 = JlibFprint.binary_image(fpimg);
该方法的 JNI 代码如下所示:
JNIEXPORT jobject JNICALL Java_jlibfprint_JlibFprint_binary_1image(JNIEnv *env, jclass jcls,jobject imgobj)
{
struct fp_img img;
struct fp_img *imgptr;
imgptr = &img;
jfp2cfp(env,imgobj,imgptr);
fp_init();
imgptr = fp_img_binarize(imgptr);
cfp2jfp(env, imgobj, imgptr);
fp_exit();
return imgobj;
}
void jfp2cfp(JNIEnv* env, jobject obj, fp_img *fpd)
{
/* Determines all the fields of the object */
jclass fpClass = env->FindClass("jlibfprint/JlibFprint$fp_image_data");
jfieldID height;
jfieldID width;
jfieldID length;
jfieldID data;
jbyteArray dataArray;
height = env->GetFieldID(fpClass, "height", "I");
width = env->GetFieldID(fpClass, "width", "I");
length = env->GetFieldID(fpClass, "length", "I");
data = env->GetFieldID(fpClass, "data", "[B");
/* Starts to fill fpd */
fpd->height = env->GetIntField(obj, height);
fpd->width = env->GetIntField(obj, width);
fpd->length = env->GetIntField(obj, length);
printf("\n height :%d",fpd->height);
printf("\n width :%d",fpd->width);
printf("\n length :%d",fpd->length);
dataArray = static_cast<jbyteArray>(env->GetObjectField(obj, data));
env->GetByteArrayRegion(dataArray, 0, FP_PRINT_DATA_DATA_SIZE, (jbyte*)fpd->data);
}
void cfp2jfp(JNIEnv* env, jobject obj, fp_img* fpd)
{
/* Determines all the fields of the object */
jclass fpClass = env->FindClass("jlibfprint/JlibFprint$fp_image_data");
jfieldID height;
jfieldID width;
jfieldID length;
jfieldID data;
jbyteArray dataArray;
height = env->GetFieldID(fpClass, "height", "I");
width = env->GetFieldID(fpClass, "width", "I");
length = env->GetFieldID(fpClass, "length", "I");
data = env->GetFieldID(fpClass, "data", "[B");
/* Starts to fill the obj */
env->SetIntField(obj, height, fpd->height);
env->SetIntField(obj, width, fpd->width);
env->SetIntField(obj, length, fpd->length);
dataArray = env->NewByteArray(FP_PRINT_DATA_DATA_SIZE);
env->SetByteArrayRegion(dataArray, 0, FP_PRINT_DATA_DATA_SIZE, (jbyte*)fpd->data);
env->SetObjectField(obj, data, dataArray);
}
但是在从 JAVA 端的 JNI 代码的这些函数返回后,该方法显示异常,如
java.lang.ArrayIndexOutOfBoundsException
at jlibfprint.JlibFprint.binary_image(Native Method)
at jlibfprint.SampleRun.main(SampleRun.java:96)
ie 对象没有被正确处理,它不会从 JNI 层返回任何东西。但是我没有得到我应该在 JNI 代码中更改的内容,以便它返回正确的对象。
请建议我任何解决方案。