1. 程式人生 > >android camera YV12資料旋轉方法

android camera YV12資料旋轉方法

以下是YV12資料順時針和逆時針旋轉90度的方法:

private void rotateYv12Degree90(byte[] src, int width, int height, byte[] dst, boolean clockwise) {
        int area = width * height;

        if (clockwise) {
            rotateRectClockwiseDegree90(src, 0, width, height, dst, 0);
            rotateRectClockwiseDegree90(src, area, width / 2, height / 2, dst, area);
            rotateRectClockwiseDegree90(src, area * 5 / 4, width / 2, height / 2, dst, area * 5 / 4);
        } else {
            rotateRectAnticlockwiseDegree90(src, 0, width, height, dst, 0);
            rotateRectAnticlockwiseDegree90(src, area, width / 2, height / 2, dst, area);
            rotateRectAnticlockwiseDegree90(src, area * 5 / 4, width / 2, height / 2, dst, area * 5 / 4);
        }
    }

    private void rotateRectClockwiseDegree90(byte[] src, int srcOffset, int width, int height, byte dst[], int dstOffset) {
        int i, j;
        int index = dstOffset;
        for (i = 0; i < width; i++) {
            for (j = height - 1; j >= 0; j--) {
                dst[index] = src[srcOffset + j * width + i];
                index++;
            }
        }
    }

    private void rotateRectAnticlockwiseDegree90(byte[] src, int srcOffset, int width, int height, byte dst[],
            int dstOffset) {
        int i, j;
        int index = dstOffset;
        for (i = width - 1; i >= 0; i--) {
            for (j = 0; j < height; j++) {
                dst[index] = src[srcOffset + j * width + i];
                index++;
            }
        }
    }