最近在做android视频实时采集,由于android摄像头默认是旋转90度的,所以采集到的视频会有一些问题,因此在采集到数据帧之后,首先需要在onPreviewFrame方法中对yuv格式的原始帧进行一次旋转。
参考了http://stackoverflow.com/questions/14167976/rotate-an-yuv-byte-array-on-android
以下为旋转90度的代码
<span style="white-space: pre;"> </span>private byte[] rotateYUV420Degree90(byte[] data, int imageWidth, <span style="white-space: pre;"> </span>int imageHeight) { <span style="white-space: pre;"> </span>byte[] yuv = new byte[imageWidth * imageHeight * 3 / 2]; <span style="white-space: pre;"> </span>// 旋转Y int i = 0; <span style="white-space: pre;"> </span>for (int x = 0; x < imageWidth; x++) { <span style="white-space: pre;"> </span>for (int y = imageHeight - 1; y >= 0; y--) { <span style="white-space: pre;"> </span>yuv[i] = data[y * imageWidth + x]; <span style="white-space: pre;"> </span>i++; <span style="white-space: pre;"> </span>} <span style="white-space: pre;"> </span>} <span style="white-space: pre;"> </span>// 旋转U和V <span style="white-space: pre;"> </span>i = imageWidth * imageHeight * 3 / 2 - 1; <span style="white-space: pre;"> </span>for (int x = imageWidth - 1; x > 0; x = x - 2) { <span style="white-space: pre;"> </span>for (int y = 0; y < imageHeight / 2; y++) { <span style="white-space: pre;"> </span>yuv[i] = data[(imageWidth * imageHeight) + (y * imageWidth) + x]; <span style="white-space: pre;"> </span>i--; <span style="white-space: pre;"> </span>yuv[i] = data[(imageWidth * imageHeight) + (y * imageWidth) <span style="white-space: pre;"> </span>+ (x - 1)]; <span style="white-space: pre;"> </span>i--; <span style="white-space: pre;"> </span>} <span style="white-space: pre;"> </span>} <span style="white-space: pre;"> </span>return yuv; <span style="white-space: pre;"> </span>}