jni 传递java对象,如何使用JNI传递和接收对象

I have an JAVA application in that I want to pass the object as a parameter to the C code using JNI and again I want to receive the object from the C code to JAVA using JNI.

In JAVA side i have simply created an application and pass it to the method as shown bellow

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

This object is pass to the method like

fpimg1 = JlibFprint.binary_image(fpimg);

And JNI code for the method is as shown bellow :

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(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);

}

But after returning from these functions of JNI code at the JAVA side the method shows the exception like

java.lang.ArrayIndexOutOfBoundsException

at jlibfprint.JlibFprint.binary_image(Native Method)

at jlibfprint.SampleRun.main(SampleRun.java:96)

i.e objects are not process correctly and it does not return any thing from the JNI layer.

But I am not getting what should I change in the JNI code so that it will return the correct object.

Please suggest me any solution.

解决方案

Both GetByteArrayRegion() and SetByteArrayRegion() can throw ArrayIndexOutOfBoundsException if the specified region goes outside the bounds of the array.

Your call to SetByteArrayRegion() looks correct -- it immediately follows creation of the array of the required size.

Verify that the size of the array on entry, accessed by GetByteArrayRegion(), is at least FP_PRINT_DATA_DATA_SIZE.

By the way, an alternative is to use GetByteArrayElements() and ReleaseByteArrayElements(), which may return a pointer to the same memory Java uses for the Array.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值