使用renderscript将相机YUV数据转换为ARGB

use*_*833 14 android renderscript android-camera

我的问题是:我在Android中设置了一个摄像头并通过使用onPreviewFrame监听器接收预览数据,该监听器向我传递一个包含默认android YUV格式的图像数据的byte []数组(设备不支持R5G6B5-格式).每个像素由12位组成,这使得事情有点棘手.现在我想要做的是将YUV数据转换为ARGB数据,以便用它进行图像处理.这必须使用renderscript来完成,以保持高性能.

我的想法是在一个元素中传递两个像素(这将是24位= 3个字节),然后返回两个ARGB像素.问题是,在Renderscript中,u8_3(三维8位向量)存储在32位,这意味着最后8位未使用.但是当将图像数据复制到分配中时,使用了所有32位,因此最后8位丢失.即使我使用32位输入数据,最后8位也没用,因为它们只有2/3像素.在定义包含3字节数组的元素时,它实际上具有3个字节的实际大小.但是然后Allocation.copyFrom() - 方法没有用数据填充in-Allocation,认为它没有用byte []填充正确的数据类型.

renderscript文档指出,有一个ScriptIntrinsicYuvToRGB应该在API级别17中完全执行.但实际上该类不存在.我已经下载了API Level 17,即使它似乎不再可以下载了.有没有人有任何关于它的信息?有没有人尝试过ScriptIntrinsic?

总而言之,我的问题是:如何快速,快速地将摄像机数据转换为ARGB数据?

这是在Dalvik VM中如何做到这一点(在网上找到代码,它的工作原理):

@SuppressWarnings("unused")
private void decodeYUV420SP(int[] rgb, byte[] yuv420sp, int width, int height) {  
    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);  
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Her*_*hal 12

我相信你会发现LivePreview测试应用程序很有趣......它是最新Jelly Bean(MR1)中Android源代码的一部分.它实现了摄像头预览,并使用ScriptIntrinsicYuvToRgb通过Renderscript转换预览数据.您可以在此浏览在线源:

LivePreview


小智 5

我无法运行ScriptInstrinsicYuvToRgb,因此我决定编写自己的RS解决方案.

这是现成的脚本(名为yuv.rs):

#pragma version(1) 
#pragma rs java_package_name(com.package.name)

rs_allocation gIn;

int width;
int height;
int frameSize;

void yuvToRgb(const uchar *v_in, uchar4 *v_out, const void *usrData, uint32_t x, uint32_t y) {

    uchar yp = rsGetElementAtYuv_uchar_Y(gIn, x, y) & 0xFF;

    int index = frameSize + (x & (~1)) + (( y>>1) * width );
    int v = (int)( rsGetElementAt_uchar(gIn, index) & 0xFF ) -128;
    int u = (int)( rsGetElementAt_uchar(gIn, index+1) & 0xFF ) -128;

    int r = (int) (1.164f * yp  + 1.596f * v );
    int g = (int) (1.164f * yp  - 0.813f * v  - 0.391f * u);
    int b = (int) (1.164f * yp  + 2.018f * u );

    r = r>255? 255 : r<0 ? 0 : r;
    g = g>255? 255 : g<0 ? 0 : g;
    b = b>255? 255 : b<0 ? 0 : b;

    uchar4 res4;
    res4.r = (uchar)r;
    res4.g = (uchar)g;
    res4.b = (uchar)b;
    res4.a = 0xFF;

    *v_out = res4;
}
Run Code Online (Sandbox Code Playgroud)

不要忘记将相机预览格式设置为NV21:

Parameters cameraParameters = camera.getParameters();
cameraParameters.setPreviewFormat(ImageFormat.NV21);
// Other camera init stuff: preview size, framerate, etc.
camera.setParameters(cameraParameters);
Run Code Online (Sandbox Code Playgroud)

分配初始化和脚本使用:

// Somewhere in initialization section 
// w and h are variables for selected camera preview size
rs = RenderScript.create(this); 

Type.Builder tbIn = new Type.Builder(rs, Element.U8(rs));
tbIn.setX(w);
tbIn.setY(h);
tbIn.setYuvFormat(ImageFormat.NV21);

Type.Builder tbOut = new Type.Builder(rs, Element.RGBA_8888(rs));
tbOut.setX(w); 
tbOut.setY(h);

inData = Allocation.createTyped(rs, tbIn.create(), Allocation.MipmapControl.MIPMAP_NONE,  Allocation.USAGE_SCRIPT & Allocation.USAGE_SHARED);
outData = Allocation.createTyped(rs, tbOut.create(), Allocation.MipmapControl.MIPMAP_NONE,  Allocation.USAGE_SCRIPT & Allocation.USAGE_SHARED);

outputBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);


yuvScript = new ScriptC_yuv(rs); 
yuvScript.set_gIn(inData);
yuvScript.set_width(w);
yuvScript.set_height(h);
yuvScript.set_frameSize(previewSize);
//.....
Run Code Online (Sandbox Code Playgroud)

相机回调方法:

public void onPreviewFrame(byte[] data, Camera camera) {
    // In your camera callback, data 
    inData.copyFrom(data);
    yuvScript.forEach_yuvToRgb(inData, outData);
    outData.copyTo(outputBitmap);
    // draw your bitmap where you want to 
    // .....
}
Run Code Online (Sandbox Code Playgroud)