JNI Android jbytearray到无符号字符*和viceversa
我有两个单独的pthread和一个静态结构数组。其中一个pthread写入解码对象,包括字节、大小、宽度和高度。另一个pthread实际上是读取堆栈并进行一些图像处理,然后将结果发布到java函数中 问题是,在pthread1上,我将jbytearray转换为unsigned char*,并存储到静态数组上的位置0 但当pthread2将其转换回jbytearray时,总会发生一些事情,我总是收到致命的信号 这是我的cpp班的第一名JNI Android jbytearray到无符号字符*和viceversa,android,c++,android-ndk,java-native-interface,bytearray,Android,C++,Android Ndk,Java Native Interface,Bytearray,我有两个单独的pthread和一个静态结构数组。其中一个pthread写入解码对象,包括字节、大小、宽度和高度。另一个pthread实际上是读取堆栈并进行一些图像处理,然后将结果发布到java函数中 问题是,在pthread1上,我将jbytearray转换为unsigned char*,并存储到静态数组上的位置0 但当pthread2将其转换回jbytearray时,总会发生一些事情,我总是收到致命的信号 这是我的cpp班的第一名 struct DecodeObject { unsigned
struct DecodeObject {
unsigned char* data;
int data_size;
int width;
int height;
int orientation;
};
static int decodeLimit = 200 ;
static DecodeObject decodeList[200] ;
static int decodeSize = -1 ;
这是我的pthread1的一部分
//Values
jbyteArray imageData = (jbyteArray) env->CallObjectMethod(decodeObject,getData);
jint width = (jint) env->CallIntMethod(decodeObject,getWidth);
jint height = (jint) env->CallIntMethod(decodeObject,getHeight);
jint orientation = (jint) env->CallIntMethod(decodeObject,getOrientation);
if(decodeSize<decodeLimit-1){
DecodeObject object;
object.data_size = env->GetArrayLength (imageData);
object.data = as_unsigned_char_array(env,imageData);
object.width = width;
object.height = height;
object.orientation = orientation;
decodeSize++;
decodeList[decodeSize] = object;
}
else {
LOGD("ERROR => BUFFER IS FULL");
}
最后是我用来转换的函数
unsigned char* as_unsigned_char_array(JNIEnv* &env,jbyteArray array) {
int len = env->GetArrayLength (array);
unsigned char* buf = new unsigned char[len];
env->GetByteArrayRegion (array, 0, len, reinterpret_cast<jbyte*>(buf));
return buf;
}
jbyteArray as_byte_array(JNIEnv* &env,unsigned char* buf, jsize len) {
jbyteArray array = env->NewByteArray(len);
//HERE I GET THE ERROR, I HAVE BEEN TRYING WITH len/2 and WORKS , PROBABLY SOME BYTS ARE GETTING LOST.
env->SetByteArrayRegion (array, 0, len, (jbyte*)(buf));
return array;
}
unsigned char*as_unsigned_char_数组(JNIEnv*&env,jbyteArray数组){
int len=env->GetArrayLength(数组);
无符号字符*buf=新的无符号字符[len];
env->GetByteArrayRegion(数组、0、len、重新解释(buf));
返回buf;
}
jbyteArray作为字节数组(JNIEnv*&env,unsigned char*buf,jsize len){
jbyterarray=env->newyterarray(len);
//在这里我得到了错误,我一直在尝试使用len/2和WORKS,可能一些byt正在丢失。
env->SetByteArrayRegion(数组,0,len,(jbyte*)(buf));
返回数组;
}
可能的重复:
unsigned char* as_unsigned_char_array(JNIEnv* &env,jbyteArray array) {
int len = env->GetArrayLength (array);
unsigned char* buf = new unsigned char[len];
env->GetByteArrayRegion (array, 0, len, reinterpret_cast<jbyte*>(buf));
return buf;
}
jbyteArray as_byte_array(JNIEnv* &env,unsigned char* buf, jsize len) {
jbyteArray array = env->NewByteArray(len);
//HERE I GET THE ERROR, I HAVE BEEN TRYING WITH len/2 and WORKS , PROBABLY SOME BYTS ARE GETTING LOST.
env->SetByteArrayRegion (array, 0, len, (jbyte*)(buf));
return array;
}