图像处理之原始帧YUV数据格式旋转处理

yuv编码简介


在YUV420中,一个像素点对应一个Y,一个2X2的小方块对应一个U和V。对于所有YUV420图像,它们的Y值排列是完全相同的,因为只有Y的图像就是灰度图像。YUV420sp与YUV420p的数据格式它们的UV排列在原理上是完全不同的。420p它是先把U存放完后,再存放V,也就是说UV它们是连续的。而420sp它是UV、UV这样交替存放的。

yuv流的各种操作。


代码:

public static byte[] flip(byte[] src, int width, int height) {
        for (int i = 0; i < height; i++) {
            int left = 0;
            int right = width - 1;
            while (left < right) {
                byte b = src[i * width + right];
                src[i * width + right] = src[i * width + left];
                src[i * width + left] = b;
                left++;
                right--;
            }
        }
        int uHeader = width * height;
        int uHeight = height / 2;
        for (int i = 0; i < uHeight; i++) {
            int left = 0;
            int right = width - 2;
            while (left < right) {
                byte b = src[uHeader + i * width + right];
                src[uHeader + i * width + right] = src[uHeader + i * width + left];
                src[uHeader + i * width + left] = b;
                b = src[uHeader + i * width + right + 1];
                src[uHeader + i * width + right + 1] = src[uHeader + i * width + left + 1];
                src[uHeader + i * width + left + 1] = b;
                left += 2;
                right -= 2;
            }
        }
        return src;
 
    }
 
    public static byte[] rotate270(byte[] src, int width, int height) {
        byte[] dest = new byte[src.length];
        int index = 0;
        for (int i = width - 1; i >= 0; i--) {
            for (int j = 0; j < height; j++) {
                dest[index++] = src[j * width + i];
            }
        }
        int uHeader = index;
        int uHeight = height / 2;
        for (int i = width - 2; i >= 0; i -= 2) {
            for (int j = 0; j < uHeight; j++) {
                dest[index++] = src[uHeader + j * width + i];
                dest[index++] = src[uHeader + j * width + i + 1];
            }
        }
        return dest;
    }
 
 
    public static byte[] rotate90(byte[] src, int width, int height) {
        byte[] dest = new byte[src.length];
        int index = 0;
        for (int i = 0; i < width; i++) {
            for (int j = height - 1; j >= 0; j--) {
                dest[index++] = src[j * width + i];
            }
        }
        int uHeader = index;
        int uHeight = height / 2;
        for (int i = 0; i < width; i += 2) {
            for (int j = uHeight - 1; j >= 0; j--) {
                dest[index++] = src[uHeader + j * width + i];
                dest[index++] = src[uHeader + j * width + i + 1];
            }
        }
        return dest;
    }
 
    public static byte[] rotate180(byte[] src, int width, int height) {
        int top = 0;
        int bottom = height - 1;
        while (top < bottom) {
            for (int i = 0; i < width; i++) {
                byte b = src[bottom * width + width - 1 - i];
                src[bottom * width + width - 1 - i] = src[top * width + i];
                src[top * width + i] = b;
            }
            top++;
            bottom--;
        }
        int uHeader = width * height;
        top = 0;
        bottom = height / 2 - 1;
        while (top < bottom) {
            for (int i = 0; i < width; i += 2) {
                byte b = src[uHeader + bottom * width + width - 2 - i];
                src[uHeader + bottom * width + width - 2 - i] = src[uHeader + top * width + i];
                src[uHeader + top * width + i] = b;
 
                b = src[uHeader + bottom * width + width - 1 - i];
                src[uHeader + bottom * width + width - 1 - i] = src[uHeader + top * width + i + 1];
                src[uHeader + top * width + i + 1] = b;
            }
            top++;
            bottom--;
        }
        return src;
    }

转载自:https://blog.csdn.net/weixin_33943347/article/details/87200308 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值