How to convert unsigned char to jbyteArray

How to convert unsigned char buffer to jbyteArray? I need to get a C ++ buffer and return to Java via JNI.

This is my current code for this.

JNIEXPORT jbyteArray JNICALL Java_com_rmsdk_wrapper_RMServices_getImageBuffer(JNIEnv *env, jobject thiso,
    jint w, jint h) {
    emh::PNGSurface * surface = services->getImageBuffer(w,h);
jbyteArray * buffer = (jbyteArray*)malloc(sizeof(jbyteArray)*surface->getBufferSize());
    unsigned char * imgBuff = surface->getBuffer();

    for(int i = 0; i < surface->getBufferSize();i++){
        buffer = imgBuff;
        buffer++;
        imgBuff++;
    }
    return *buffer;
};

When compiling, I got the following error.

Compile++ thumb  : rmsdk <= RMSDK_Wrapper_JNI.cpp
/home/marcos/dev/workspace/rmsdk.native.wraper/jni/RMSDK_Wrapper_JNI.cpp: In function '_jbyteArray* Java_com_rmsdk_wrapper_RMServices_getImageBuffer(JNIEnv*, _jobject*, jint, jint)':
/home/marcos/dev/workspace/rmsdk.native.wraper/jni/RMSDK_Wrapper_JNI.cpp:37: error: cannot convert 'unsigned char*' to '_jbyteArray**' in assignment
make: *** [/home/marcos/dev/workspace/rmsdk.native.wraper/obj/local/armeabi/objs/rmsdk/RMSDK_Wrapper_JNI.o] Error 1

Ty.

+3
source share
1 answer

See these JNI array operations , in particular:

  • NewByteArray
  • GetArrayElements
  • ReleaseArrayElements

If you know the maximum size of the array in advance, you can avoid calling NewByteArray.

If you call this infrequently, it may be easier for you to use JNA.

+5
source

Source: https://habr.com/ru/post/1785059/


All Articles