安卓上的摄像头,格式是NV21。前置的摄像头,需要镜像。
于是搜索了一下,找到了一个经过验证的算法,并优化如下:
//NV21: YYYY VUVU byte[] NV21_mirror(byte[] nv21_data, int width, int height) { int i; int left, right; byte temp; int startPos = 0; // mirror Y for (i = 0; i < height; i++) { left = startPos; right = startPos + width - 1; while (left < right) { temp = nv21_data[left]; nv21_data[left] = nv21_data[right]; nv21_data[right] = temp; left ++; right--; } startPos += width; } // mirror U and V int offset = width * height; startPos = 0; for (i = 0; i < height / 2; i++) { left = offset + startPos; right = offset + startPos + width - 2; while (left < right) { temp = nv21_data[left ]; nv21_data[left ] = nv21_data[right]; nv21_data[right] = temp; left ++; right--; temp = nv21_data[left ]; nv21_data[left ] = nv21_data[right]; nv21_data[right] = temp; left ++; right--; } startPos += width; } return nv21_data; }