旋转Android上的YUV字节数组数组、字节、Android、YUV

2023-09-13 00:24:20 作者:他心难入i

我期待旋转YUV框架preVIEW从preVIEW Callblack:收到,到目前为止,我已经创建了这个帖子里面cointains算法旋转框preVIEW但搞乱了preVIEW图像 摄像头像素旋转

I'm looking to rotate a YUV frame preview recieved from a Preview Callblack, so far I've founded this post which cointains an algorithm to rotate the frame preview but is messing the preview image camera pixels rotated

另一种方式来旋转图像将创建一个JPG出来的YUV形象,创建一个位图,位图旋转和获取位图的字节数组,但我真的需要在YUV(NV21)的格式。

another way to rotate the image will be creating a jpg out of the YUV image, create a bitmap, rotate a bitmap and obtaining the byte array of the bitmap, but I really need the format in YUV (NV21).

仅供参考。我问这个的原因是因为我有一个摄像头应用程序,支持旋转,但框架previews都回来了仅在横向模式。

FYI. the reason I'm asking this is because I have a camera app that supports rotation, but the frame previews are coming back in landscape mode only.

推荐答案

下面的方法可以90度旋转YUV420字节数组。

The following method can rotate a YUV420 byte array by 90 degree.

private byte[] rotateYUV420Degree90(byte[] data, int imageWidth, int imageHeight) 
{
    byte [] yuv = new byte[imageWidth*imageHeight*3/2];
    // Rotate the Y luma
    int i = 0;
    for(int x = 0;x < imageWidth;x++)
    {
        for(int y = imageHeight-1;y >= 0;y--)                               
        {
            yuv[i] = data[y*imageWidth+x];
            i++;
        }
    }
    // Rotate the U and V color components 
    i = imageWidth*imageHeight*3/2-1;
    for(int x = imageWidth-1;x > 0;x=x-2)
    {
        for(int y = 0;y < imageHeight/2;y++)                                
        {
            yuv[i] = data[(imageWidth*imageHeight)+(y*imageWidth)+x];
            i--;
            yuv[i] = data[(imageWidth*imageHeight)+(y*imageWidth)+(x-1)];
            i--;
        }
    }
    return yuv;
}

(注意,这可能仅工作,如果该宽度和高度是一个因子4)

(Note that this might only work if the width and height is a factor of 4)