在 Android 上旋转 YUV 字节数组 [英] Rotate an YUV byte array on Android

查看:36
本文介绍了在 Android 上旋转 YUV 字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望旋转从 Preview Callblack 接收到的 YUV 帧预览,到目前为止我已经创建了这篇文章,其中包含一种旋转帧预览的算法,但正在弄乱预览图像旋转的相机像素

另一种旋转图像的方法是从YUV图像中创建一个jpg,创建一个位图,旋转一个位图并获取位图的字节数组,但我确实需要YUV(NV21)中的格式.

仅供参考.我问这个的原因是因为我有一个支持旋转的相机应用程序,但帧预览仅在横向模式下返回.

解决方案

以下方法可以将一个YUV420字节数组旋转90度.

private byte[] rotateYUV420Degree90(byte[] data, int imageWidth, int imageHeight){字节[] yuv = 新字节[图像宽度*图像高度*3/2];//旋转 Y 亮度int i = 0;for(int x = 0;x < imageWidth;x++){for(int y = imageHeight-1;y >= 0;y--){yuv[i] = 数据[y*imageWidth+x];我++;}}//旋转 U 和 V 颜色分量i = 图像宽度*图像高度*3/2-1;for(int x = imageWidth-1;x > 0;x=x-2){for(int y = 0;y < imageHeight/2;y++){yuv[i] = 数据[(图像宽度*图像高度)+(y*图像宽度)+x];一世 - ;yuv[i] = 数据[(图像宽度*图像高度)+(y*图像宽度)+(x-1)];一世 - ;}}返回 yuv;}

(请注意,这可能仅在宽度和高度为 4 的因数时才有效)

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

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).

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.

解决方案

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)

这篇关于在 Android 上旋转 YUV 字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆