NV21 旋转+转为NV12
生活随笔
收集整理的這篇文章主要介紹了
NV21 旋转+转为NV12
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
yuv420sp的分兩種,nv21和nv12。Android 取攝像頭中的數(shù)據(jù) ,當(dāng)使用camera1.0 時,onPreviewFrame返回的數(shù)據(jù)yuv420sp的nv21,并且camera中取出的數(shù)據(jù)顯示時是偏轉(zhuǎn)的,需要將其旋轉(zhuǎn)順時針旋轉(zhuǎn)270或逆時針旋轉(zhuǎn)90,注:旋轉(zhuǎn)后寬高對調(diào)
/*** 此處為順時針旋轉(zhuǎn)270* @param data 旋轉(zhuǎn)前的數(shù)據(jù)* @param imageWidth 旋轉(zhuǎn)前數(shù)據(jù)的寬* @param imageHeight 旋轉(zhuǎn)前數(shù)據(jù)的高* @return 旋轉(zhuǎn)后的數(shù)據(jù)*/ private byte[] rotateYUV420Degree270(byte[] data, int imageWidth, int imageHeight){byte[] yuv =new byte[imageWidth*imageHeight*3/2];// Rotate the Y lumaint i =0;for(int x = imageWidth-1;x >=0;x--){for(int y =0;y < imageHeight;y++){yuv[i]= data[y*imageWidth+x];i++;}}// Rotate the U and V color componentsi = imageWidth*imageHeight;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-1)];i++;yuv[i]= data[(imageWidth*imageHeight)+(y*imageWidth)+x];i++;}}return yuv;} /*** 此處為順時針旋轉(zhuǎn)旋轉(zhuǎn)90度* @param data 旋轉(zhuǎn)前的數(shù)據(jù)* @param imageWidth 旋轉(zhuǎn)前數(shù)據(jù)的寬* @param imageHeight 旋轉(zhuǎn)前數(shù)據(jù)的高* @return 旋轉(zhuǎn)后的數(shù)據(jù)*/ private byte[] rotateYUV420Degree90(byte[] data, int imageWidth, int imageHeight) {byte [] yuv = new byte[imageWidth*imageHeight*3/2];// Rotate the Y lumaint i = 0;for(int x = 0;x < imageWidth;x++){for(int y = imageHeight-1;y >= 0;y--){yuv[i] = data[y*imageWidth+x];i++;}}// Rotate the U and V color componentsi = 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; }順時針旋轉(zhuǎn)180度
private byte[] rotateYUV420Degree180(byte[] data, int imageWidth, int imageHeight){byte[] yuv =new byte[imageWidth*imageHeight*3/2];int i =0;int count =0;for(i = imageWidth * imageHeight -1; i >=0; i--){yuv[count]= data[i];count++;}i = imageWidth * imageHeight *3/2-1;for(i = imageWidth * imageHeight *3/2-1; i >= imageWidth* imageHeight; i -=2){yuv[count++]= data[i -1];yuv[count++]= data[i]; }return yuv; }用mediacode編碼h264時,因為mediacode編碼視頻只支持yuv420sp的nv12,需要將nv21轉(zhuǎn)為nv12
private void NV21ToNV12(byte[] nv21,byte[] nv12,int width,int height){if(nv21 == null || nv12 == null)return;int framesize = width*height;int i = 0,j = 0;System.arraycopy(nv21, 0, nv12, 0, framesize);for(i = 0; i < framesize; i++){nv12[i] = nv21[i];}for (j = 0; j < framesize/2; j+=2){nv12[framesize + j-1] = nv21[j+framesize];}for (j = 0; j < framesize/2; j+=2){nv12[framesize + j] = nv21[j+framesize-1];} }?
總結(jié)
以上是生活随笔為你收集整理的NV21 旋转+转为NV12的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 网格搜索_Python机器
- 下一篇: Linux Vim替换字符串的方法总结