我试图修改一个字节数组,然后通过JNI

时间:2018-04-05 11:57:18

标签: android c++ arrays java-native-interface jbytearray

这是我的功能:

void FrameReceived(int width, int height, const char *rawImageBytes, int size, jboolean remote)
{

if(size == 0)
    return;

jboolean isAttached;
JNIEnv *env;
jint jParticipant;
jint jWidth;
jint jHeight;
jbyte *jRawImageBytes;
jbyte *modifiedRawImageBytes;
jbyteArray Array;

env = getJniEnv(&isAttached);

if (env == NULL)
    goto FAIL0;
//LOGE(".... **** ....TRYING TO FIND CALLBACK");
if(remote)
{
    if(frameReceivedRemoteMethod == NULL)
        frameReceivedRemoteMethod = getApplicationJniMethodId(env, applicationJniObj, "vidyoConferenceFrameReceivedRemoteCallback", "(III[B)V");

    if (frameReceivedRemoteMethod == NULL) {
        //LOGE(".... **** ....CALLBACK NOT FOUND");
        goto FAIL1;
    }
}
else
{
    if(frameReceivedMethod == NULL)
        frameReceivedMethod = getApplicationJniMethodId(env, applicationJniObj, "vidyoConferenceFrameReceivedCallback", "(III[B)V");

    if (frameReceivedMethod == NULL) {
        //LOGE(".... **** ....CALLBACK NOT FOUND");
        goto FAIL1;
    }
}

jWidth = width;
jHeight = height;
LOGI("FrameReceived will reach here 1");

jRawImageBytes = (*env)->NewByteArray(env, size);
LOGI("FrameReceived will reach here 2");
(*env)->SetByteArrayRegion(env, jRawImageBytes, 0, size, rawImageBytes);
LOGI("FrameReceived will reach here 3");
//TODO will transform to NV21 in ndk (faster)
modifiedRawImageBytes = (*env)->NewByteArray(env, size);
LOGI("FrameReceived will reach here 4");
jint sizeWH = width * height;
jint quarter = sizeWH/4;
jint v0 = sizeWH + quarter;
LOGI("FrameReceived will reach here 5");
for (int u = sizeWH, v = v0, o = sizeWH; u < v0; u++, v++, o += 2) {
    modifiedRawImageBytes[o] = jRawImageBytes[v]; // For NV21, V first
    modifiedRawImageBytes[o + 1] = jRawImageBytes[u]; // For NV21, U second
}
LOGI("FrameReceived will reach here 6");
(*env)->SetByteArrayRegion(env, Array, 0, size, modifiedRawImageBytes);
LOGI("FrameReceived will reach here 7");

//LOGE(".... **** ....CALLBACK BEING CALLED");
if(remote)
{
    (*env)->CallVoidMethod(env, applicationJniObj, frameReceivedRemoteMethod, 0, jWidth, jHeight, Array);
}
else
{
    (*env)->CallVoidMethod(env, applicationJniObj, frameReceivedMethod, 0, jWidth, jHeight, Array);
}
//LOGE(".... **** ....CALLBACK CALLED");

(*env)->DeleteLocalRef(env, jRawImageBytes);

if (isAttached)
{
    (*global_vm)->DetachCurrentThread(global_vm);
}
//LOGE("FrameReceived End");
return;
FAIL1:
if (isAttached)
{
    (*global_vm)->DetachCurrentThread(global_vm);
}
FAIL0:
//LOGE("FrameReceived FAILED");
return;
}

在FOR循环中,这个LOG:LOGI("FrameReceived will reach here 5");之后总会崩溃。 我在这做错了什么? 这就是我想在NDK中实现的目标:

public byte [] I420toNV21(final byte [] input,byte [] output,final int width,final int height){         if(output == null){             output = new byte [input.length];         }         final int size = width * height;         final int quarter = size / 4;         final int v0 = size + quarter;

    System.arraycopy(input, 0, output, 0, size); // Y is same

    for (int u = size, v = v0, o = size; u < v0; u++, v++, o += 2) {
        output[o] = input[v]; // For NV21, V first
        output[o + 1] = input[u]; // For NV21, U second
    }
    return output;
}

所以我可以将corect格式的字节数组发送给java。而不必在我的java代码中执行此方法。为什么不能通过字节数组在for循环中添加值?

1 个答案:

答案 0 :(得分:0)

所以为了做到这一点,我改变了一点逻辑。 拥有char * rawImageBytes及其大小。而不是直接创建一个jbyteArray。我将为另一个char数组分配空间。我将从第一个复制内存,在第二个中复制内存。 然后我调用for循环,以便修改我需要的东西,只有在那之后,我才会创建jbyteArray:

char *modifiedRawImageBytes = malloc(size);
memcpy(modifiedRawImageBytes, rawImageBytes, size);
jint sizeWH = width * height;
jint quarter = sizeWH/4;
jint v0 = sizeWH + quarter;
for (int u = sizeWH, v = v0, o = sizeWH; u < v0; u++, v++, o += 2) {
        modifiedRawImageBytes[o] = rawImageBytes[v]; // For NV21, V first
        modifiedRawImageBytes[o + 1] = rawImageBytes[u]; // For NV21, U second
}
jWidth = width;
jHeight = height;

jRawImageBytes = (*env)->NewByteArray(env, size);
(*env)->SetByteArrayRegion(env, jRawImageBytes, 0, size, modifiedRawImageBytes);