How to pass and receive objects using JNI

≡放荡痞女 提交于 2019-12-07 17:28:11

问题


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

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.


回答1:


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.



来源:https://stackoverflow.com/questions/18381076/how-to-pass-and-receive-objects-using-jni

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!