图像处理之原始帧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 

猜你喜欢

转载自blog.csdn.net/tklwj/article/details/87975528