开发者

JNI unsigned char to byte array

开发者 https://www.devze.com 2023-02-02 11:54 出处:网络
I\'m working with a C++ library that stores image byte data in an array of unsigned characters. My jni function returns a jByteArray (which then gets converted to a BufferedImage on the java side), bu

I'm working with a C++ library that stores image byte data in an array of unsigned characters. My jni function returns a jByteArray (which then gets converted to a BufferedImage on the java side), but I'm not sure how to fill the jByteArray from the unsigned character array (if it is possible). Can anyone provide a snippet for this last part to basically do this:

// size is the size of the unsigned char array
const int size = 100;
unsigned char* buf = new unsigned char[size];
// buf gets passed to another library here to be populated

开发者_StackOverflow社区jbyteArray bArray = env->NewByteArray(size);
// now how do I get the data from buf to bArray?

Thanks, Jeff


Here's a snippet that should point you in the right direction.

jboolean isCopy;
void *data = env->GetPrimitiveArrayCritical((jarray)bArray, &isCopy);

memcpy(data, buf, bytecount);

// and don't forget the 'release'


User jbyte* instead of unsigned char*

In JNI A jbyte is defined to be a signed char. JNI offers a few functions for that purpose: you can create a new jbyteArray and set a specified region of it given a jbyte* buffer.

Please Read the documentation.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号