我需要帮助将二进制数据传递到Java。我试图使用jbytearray,但当数据进入Java时,它似乎是损坏的。有人能帮我吗?
这是一段示例代码的片段。首先是本地C++端:
printf("Building audio array copy\n");
jbyteArray rawAudioCopy = env->NewByteArray(10);
jbyte toCopy[10];
printf("Filling audio array copy\n");
char theBytes[10] = {0,1,2,3,4,5,6,7,8,9};
for (int i = 0; i < sizeof(theBytes); i++) {
toCopy[i] = theBytes[i];
}
env->SetByteArrayRegion(rawAudioCopy,0,10,toCopy);
printf("Finding object callback\n");
jmethodID aMethodId = env->GetMethodID(env->GetObjectClass(obj),"handleAudio","([B)V");
if(0==aMethodId) throw MyRuntimeException("Method not found error",99);
printf("Invoking the callback\n");
env->CallVoidMethod(obj,aMethodId, &rawAudioCopy);
接下来是Java回调方法:
public void handleAudio(byte[] audio){
System.out.println("Audio supplied to Java [" + audio.length + "] bytes");
byte[] expectedAudio = {0,1,2,3,4,5,6,7,8,9};
for (int i = 0; i < audio.length; i++) {
if(audio[i]!= expectedAudio[i])
System.err.println("Expected byte " + expectedAudio[i]
+ " at byte " + i + " but got byte " + audio[i]);
else System.out.print('.');
}
System.out.println("Audio passed back accordingly!");
}
当回调被调用时,我得到以下输出:
library loaded!
Audio supplied to Java [-2019659176] bytes
Audio passed back accordingly!