执行程序时出现Java异常错误。虽然java捕获了异常,但它继续执行本机代码而不会给出预期的结果。
这里是本机代码我觉得它有问题: -
System::Collections::Generic::List<cli::array<unsigned char>^>^ processImages(unsigned char* frontImage, unsigned char* backImage);
jbyteArray convertUnsignedCharIntoJByteArray(JNIEnv *env,array<unsigned char>^ inputArray);
unsigned char* convertJByteArrayIntoUnsignedChar(JNIEnv *env, jbyteArray inImage);
JNIEXPORT jobject JNICALL Java_com_me_NativeCaller_processImages
(JNIEnv *env, jclass c, jbyteArray front, jbyteArray back){
unsigned char* frontBuffer = convertJByteArrayIntoUnsignedChar(env, front);
unsigned char* backBuffer = convertJByteArrayIntoUnsignedChar(env, back);
jbyteArray intermediateArray1 = convertUnsignedCharIntoJByteArray(env,(array<unsigned char>^)returnedValue[0]);
jbyteArray intermediateArray2 = convertUnsignedCharIntoJByteArray(env,(array<unsigned char>^)returnedValue[1]);
int aLen1 = strlen(reinterpret_cast<const char*>(&intermediateArray1));
int aLen2 = strlen(reinterpret_cast<const char*>(&intermediateArray2));
jbyteArray finalArray = env->NewByteArray(2);
env->SetByteArrayRegion(finalArray,0,1,(jbyte*)&intermediateArray1);
env->SetByteArrayRegion(finalArray,1,2,(jbyte*)&intermediateArray2);
return static_cast<jobject>(finalArray);
}
/*takes an array<unsigned char>^ as input and convert it into a jbyteArray*/
jbyteArray convertUnsignedCharIntoJByteArray(JNIEnv *env,array<unsigned char>^ inputArray){
int aLen = strlen(reinterpret_cast<const char*>(&inputArray));
jbyteArray intermediateArray = env->NewByteArray(aLen);
env->SetByteArrayRegion(intermediateArray,0,aLen,(jbyte*)&inputArray);
return intermediateArray;
}
准确地说,我认为将manged unsigned char转换为jbyteArray的方式是不正确的。
有人可以告诉我哪里出错了,并且有可能解决这个问题。
答案 0 :(得分:0)
这看起来不正确:
jbyteArray intermediateArray1 = convertUnsignedCharIntoJByteArray(env,(array<unsigned char>^)returnedValue[0]);
jbyteArray intermediateArray2 = convertUnsignedCharIntoJByteArray(env,(array<unsigned char>^)returnedValue[1]);
//...
env->SetByteArrayRegion(finalArray,0,1,(jbyte*)&intermediateArray1);
env->SetByteArrayRegion(finalArray,1,2,(jbyte*)&intermediateArray2);
不熟悉使用“^”内容的C ++ / CLI语法,我专注于jbyteArray和SetByteArrayRegion()调用。 jbyteArray是指针的别名。鉴于此,对SetByteArrayRegion()的调用不正确。它应该是:
env->SetByteArrayRegion(finalArray,0,1,(jbyte*)intermediateArray1);
env->SetByteArrayRegion(finalArray,1,2,(jbyte*)intermediateArray2);
您还可以在代码的其他部分检查&array
的其他用法。同样,我不熟悉“^”语法的细微差别,但使用传统的C ++来实现JNI代码。