将I420转换为BGRA

时间:2016-01-08 07:05:33

标签: android

我想将I420转换为BGRA,但我只搜索将I420转换为ARGB的方法:

public static int[] I420toARGB(byte[] yuv, int width, int height)
{

    boolean invertHeight=false;
    if (height<0)
    {
        height=-height;
        invertHeight=true;
    }

    boolean invertWidth=false;
    if (width<0)
    {
        width=-width;
        invertWidth=true;
    }



    int iterations=width*height;

    int[] rgb = new int[iterations];

    for (int i = 0; i<iterations;i++)
    {

        int nearest = (i/width)/2 * (width/2) + (i%width)/2;

        int y = yuv[i] & 0x000000ff;
        int u = yuv[iterations+nearest] & 0x000000ff;


        int v = yuv[iterations + iterations/4 + nearest] & 0x000000ff;


        int b = (int)(y+1.8556*(u-128));

        int g = (int)(y - (0.4681*(v-128) + 0.1872*(u-128)));

        int r = (int)(y+1.5748*(v-128));


        if (b>255){b=255;}
        else if (b<0 ){b = 0;}
        if (g>255){g=255;}
        else if (g<0 ){g = 0;}
        if (r>255){r=255;}
        else if (r<0 ){r = 0;}


        int targetPosition=i;

        if (invertHeight)
        {
            targetPosition=((height-1)-targetPosition/width)*width   +   (targetPosition%width);
        }
        if (invertWidth)
        {
            targetPosition=(targetPosition/width)*width    +    (width-1)-(targetPosition%width);
        }


        rgb[targetPosition] =  (0xff000000) | (0x00ff0000 & r << 16) | (0x0000ff00 & g << 8) | (0x000000ff & b);
    }
    return rgb;

}

所以,如果我只修改最后一行:

rgb [targetPosition] =(0x000000ff&amp; b)| (0x0000ff00&amp; g&lt;&lt; 8)| (0x00ff0000&amp; r&lt;&lt; 16)| (0xff000000);

1 个答案:

答案 0 :(得分:0)

否,仅更改最后一行class UsersConfig(AppConfig): name = 'users' def ready(self): import users.signals 的顺序是不够的。另外,原始代码是错误的,因为逻辑和 r…g…b…a优先于 shift &

<<

但是这种转换非常缓慢。我建议使用使用高度优化的本机代码的 libyuv Out of the box,随附

bgra[targetPosition] =  255 | (r << 8) | (g << 16) | (b < 24);

要转换单个半平面字节数组,您将需要基于Android420ToARGB()的自己的包装器。