android開發直播將yvu420順時針逆時針各旋轉90度
做android手機直播的時候遇到在Camera的回到onPreviewFrame獲得的資料幀傳輸到jni層,但是視訊方向是有問題的,所以在網上找了許多yuv分量旋轉的程式碼,最後適用的這兩個還是比較不錯的,
/**
* 視訊順時針旋轉90
* */
private byte[] rotateYUV420Degree90(byte[] data, int imageWidth,
int imageHeight) {
byte[] yuv = new byte[imageWidth * imageHeight * 3 / 2];
int i = 0;
for (int x = 0; x < imageWidth; x++) {
for (int y = imageHeight - 1; y >= 0; y--) {
yuv[i] = data[y * imageWidth + x];
i++;
}
}
i = imageWidth * imageHeight * 3 / 2 - 1;
for (int x = imageWidth - 1; x > 0; x = x - 2) {
for (int y = 0; y < imageHeight / 2; y++) {
yuv[i] = data[(imageWidth * imageHeight) + (y * imageWidth) + x];
i--;
yuv[i] = data[(imageWidth * imageHeight) + (y * imageWidth)
+ (x - 1)];
i--;
}
}
return yuv;
}
/**
* 視訊逆時針旋轉90
* */
public void YUV420spRotateNegative90(byte[] dst, byte[] src, int srcWidth,
int height) {
int nWidth = 0, nHeight = 0;
int wh = 0;
int uvHeight = 0;
if (srcWidth != nWidth || height != nHeight) {
nWidth = srcWidth;
nHeight = height;
wh = srcWidth * height;
uvHeight = height / 2;
}
// 旋轉Y
int k = 0;
for (int i = 0; i < srcWidth; i++) {
int nPos = srcWidth - 1;
for (int j = 0; j < height; j++) {
dst[k] = src[nPos - i];
k++;
nPos += srcWidth;
}
}
for (int i = 0; i < srcWidth; i += 2) {
int nPos = wh + srcWidth - 1;
for (int j = 0; j < uvHeight; j++) {
dst[k] = src[nPos - i - 1];
dst[k + 1] = src[nPos - i];
k += 2;
nPos += srcWidth;
}
}
return;
}