Rotate an YUV byte array on Android

前端 未结 3 2220
春和景丽
春和景丽 2020-11-28 08:22

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

3条回答
  •  误落风尘
    2020-11-28 08:37

    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;
    }
    

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

提交回复
热议问题