Reputation: 18276
How do I convert a unsigned char buffer to a jbyteArray? I need get a C++ buffer and returns to Java through JNI.
This is my current code to do that.
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;
};
I got the following error when compiling.
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.
Upvotes: 4
Views: 8782
Reputation: 86419
See these JNI array operations, particularly:
If you know a maximum size for the array beforehand, you can avoid the NewByteArray call.
If you call this infrequently, you might find it easier to use JNA.
Upvotes: 6