在android中旋转YUV420/NV21图像

Kir*_*tan 6 android image-processing rotation yuv

在PreviewCall背面的表面,我们在相机预览中得到YUV420SP格式,但是由于该图像的错误旋转,我想要执行YUV图像的正确旋转,因为我需要通过网络发送它.因此需要应用正确的旋转.

我发现这个链接它确实正确旋转但图像松散了颜色.

http://www.wordsaretoys.com/2013/10/25/roll-that-c​​amera-zombie-rotation-and-coversion-from-yv12-to-yuv420planar/

还检查了在Android上旋转YUV字节数组,但它没有正确显示图像.

我确实检查了stckoverflow上的链接,但没有一个在android环境中正确使用代码有令人满意的答案.

任何人都知道如何正确旋转NV21图像字节[]并正确保留其颜色信息.

小智 7

如果您只是想旋转NV21,以下代码将会有所帮助.(我从这里修改了代码)

public static void rotateNV21(byte[] input, byte[] output, int width, int height, int rotation) {
        boolean swap = (rotation == 90 || rotation == 270);
        boolean yflip = (rotation == 90 || rotation == 180);
        boolean xflip = (rotation == 270 || rotation == 180);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                int xo = x, yo = y;
                int w = width, h = height;
                int xi = xo, yi = yo;
                if (swap) {
                    xi = w * yo / h;
                    yi = h * xo / w;
                }
                if (yflip) {
                    yi = h - yi - 1;
                }
                if (xflip) {
                    xi = w - xi - 1;
                }
                output[w * yo + xo] = input[w * yi + xi];
                int fs = w * h;
                int qs = (fs >> 2);
                xi = (xi >> 1);
                yi = (yi >> 1);
                xo = (xo >> 1);
                yo = (yo >> 1);
                w = (w >> 1);
                h = (h >> 1);
                // adjust for interleave here
                int ui = fs + (w * yi + xi) * 2;
                int uo = fs + (w * yo + xo) * 2;
                // and here
                int vi = ui + 1;
                int vo = uo + 1;
                output[uo] = input[ui]; 
                output[vo] = input[vi]; 
            }
        }
    }   
Run Code Online (Sandbox Code Playgroud)

  • 这似乎适用于 180 度,但 90 度和 270 度旋转会使图像失真。 (2认同)