将Java代码转换为C - > JNI致电 - >程序停止运行

时间:2013-05-12 19:34:35

标签: java c java-native-interface converter

我尝试转换此Java代码:

// http://www.stanford.edu/class/ee368/Android/index.html  
// Source: http://www.stanford.edu/class/ee368/Android/HelloViewfinder/Project.zip
private void decodeYUV420RGB(int[] rgb, byte[] yuv420sp, int width, int height) {
     Convert YUV to RGB
    final int frameSize = width * height;
    for (int j = 0, yp = 0; j < height; j++) {
        int uvp = frameSize + (j >> 1) * width, u = 0, v = 0;
        for (int i = 0; i < width; i++, yp++) {
            int y = (0xff & ((int) yuv420sp[yp])) - 16;
            if (y < 0) y = 0;
            if ((i & 1) == 0) {
                v = (0xff & yuv420sp[uvp++]) - 128;
                u = (0xff & yuv420sp[uvp++]) - 128;
            }

            int y1192 = 1192 * y;
            int r = (y1192 + 1634 * v);
            int g = (y1192 - 833 * v - 400 * u);
            int b = (y1192 + 2066 * u);

            if (r < 0) r = 0; else if (r > 262143) r = 262143;
            if (g < 0) g = 0; else if (g > 262143) g = 262143;
            if (b < 0) b = 0; else if (b > 262143) b = 262143;

            rgb[yp] = 0xff000000 | ((r << 6) & 0xff0000) | ((g >> 2) & 0xff00) | ((b >> 10) & 0xff);
        }
    }
}

以这种方式调用:

//byte[] mYUVData; int[] mRGBData;
decodeYUV420RGB(mRGBData, mYUVData, mImageWidth, mImageHeight);

到这个C代码:

#include <string.h>
#include <jni.h>

jint*
Java_com_camera_DrawOnTop_decodeYUV420RGB565(JNIEnv* env, jobject  thiz, jintArray rgb, jbyteArray yuv420sp, jint width, jint height)
{
 jbyte* yuv420spc = (*env)->GetByteArrayElements(env, yuv420sp, NULL);
 jint*  rgbc      = (*env)->GetIntArrayElements(env, rgb, NULL);

    int frameSize = width * height;
    int j;
    int i;
    int yp;
    for (j = 0, yp = 0; j < height; j++) {
        int uvp = frameSize + (j >> 1) * width, u = 0, v = 0;
        for (i = 0; i < width; i++, yp++) {
            int y = (0xff & ((int) yuv420spc[yp])) - 16;
            if (y < 0) y = 0;
            if ((i & 1) == 0) {
                v = (0xff & yuv420spc[uvp++]) - 128;
                u = (0xff & yuv420spc[uvp++]) - 128;
            }

            int y1192 = 1192 * y;
            int r = (y1192 + 1634 * v);
            int g = (y1192 - 833 * v - 400 * u);
            int b = (y1192 + 2066 * u);

            if (r < 0) r = 0; else if (r > 262143) r = 262143;
            if (g < 0) g = 0; else if (g > 262143) g = 262143;
            if (b < 0) b = 0; else if (b > 262143) b = 262143;

            rgbc[yp] = 0xff000000 | ((r << 6) & 0xff0000) | ((g >> 2) & 0xff00) | ((b >> 10) & 0xff);
        }
    }

(*env)->ReleaseByteArrayElements(env, yuv420sp, yuv420spc, 0 );
(*env)->ReleaseIntArrayElements(env, rgb, rgbc, 0 );
return rgbc;

}

并通过JNI调用它:

//int[] mRGBData; int [] tmpData = {1,2,3};
mRGBData = decodeYUV420RGB565(tmpData, mYUVData, mImageWidth, mImageHeight);

但程序在上述调用后中断运行。 我现在不知道如何使用JNI进行调用,所以我使用了tmpData 只有数据但通过等号将实际数据返回到mRGBData。

我的C代码有什么问题,所以它在运行时断开了?

我要改变什么,以便它可以像参考一样工作 原始代码(没有等号)?

3 个答案:

答案 0 :(得分:1)

您应该为您的JNI函数提供正确大小的RGB数组(即 w * h ),以及正确大小和结构的YUV数组( w * h * 3 / 2 w * h luma 字节( Y ),然后是(w / 2)*( h / 2) chroma U V )字节对。如果你提供 rgb,调用将会崩溃大小为3的数组,与您的代码段一样。

另请注意,您正在构建rgb565阵列。它的元素可能是短类型(16位)而不是int(32位)。

答案 1 :(得分:0)

GetByteArrayElements()在C端创建一个数组,然后填充,然后释放,然后返回到Java,它获取指向已释放内存的指针。如果要将数据实际传送到Java端,则需要创建一个新的Java数组对象并返回该对象。或者,在Java端创建新对象并将其传递给C代码进行修改。我发现后者通常更容易。我通常在Java端创建一个DirectByteBuffer并将其传递给本机函数,让本机函数调用GetDirectBufferAddress并写入。

有关示例,请参阅我的PD ojrandlib JNI代码:https://github.com/lcrocker/ojrandlib/tree/master/source/java/com/onejoker/randlib

答案 2 :(得分:0)

我将代码更改为:

爪哇:

native void decodeYUV420RGB565(ByteBuffer rgb, byte[] yuv420sp, int width, int height); 
//There's no allocateDirect for IntBuffer so I have to use ByteBuffer
//although mRGBData is an int array
//allocateDirect(mRGBData.length * 4) because mRGBData is an int array
ByteBuffer mTempData = ByteBuffer.allocateDirect(mRGBData.length*4);
decodeYUV420RGB565(mTempData, mYUVData, mImageWidth, mImageHeight);
mRGBData = mTempData.asIntBuffer().array();
mTempData.clear();

C:

void Java_com_camera_DrawOnTop_decodeYUV420RGB565(JNIEnv* env, jobject thiz, jintArray rgb, jbyteArray yuv420sp, jint width, jint height)
{
 jbyte* yuv420spc = (*env)->GetByteArrayElements(env, yuv420sp, NULL);
 jint*  rgbc      = (*env)->GetDirectBufferAddress(env, thiz);

 //...conversion code as above...

 (*env)->ReleaseByteArrayElements(env, yuv420sp, yuv420spc, 0 );
 (*env)->ReleaseIntArrayElements(env, rgb, rgbc, 0 );

但我有同样的问题:当我调用decodeYUV420RGB565(mTempData,mYUVData,mImageWidth,mImageHeight)时代码崩溃。我在代码中要改变什么?