您的位置:首页 > 移动开发 > Android开发

android Camera相关问题及NV12剪裁旋转

2015-12-21 09:55 477 查看
1、SurfaceView不显示画面,可能是xml设置了背景色,这回遮挡视频画面。

2、如果想保存方形视频,而又不是通过缩放,可以在预览的适合遮挡不需要的画面。录制的适合剪裁一下。

3、因为Camera数据是按横屏给,竖屏的适合,画面会选择90°,StartPreview前设置:mCamera.setDisplayOrientation(90);

4、网上找的几个NV12剪裁及选择代码:

private byte[] rotateYUV420Degree90(byte[] data, int imageWidth, int imageHeight)
{

final byte [] yuv = new byte[previewWidth*previewHeight*3/2];
// Rotate the Y luma
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++;
}

}
// Rotate the U and V color components
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;
}

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;
}

private byte[] rotateYUV420Degree270(byte[] data, int imageWidth, int imageHeight)
{
final byte [] yuv = new byte[previewWidth*previewHeight*3/2];
int wh = 0;
int uvHeight = 0;
if(imageWidth != 0 || imageHeight != 0)
{
wh = imageWidth * imageHeight;
uvHeight = imageHeight >> 1;//uvHeight = height / 2
}

//旋转Y
int k = 0;
for(int i = 0; i < imageWidth; i++) {
int nPos = 0;
for(int j = 0; j < imageHeight; j++) {
yuv[k] = data[nPos + i];
k++;
nPos += imageWidth;
}
}

for(int i = 0; i < imageWidth; i+=2){
int nPos = wh;
for(int j = 0; j < uvHeight; j++) {
yuv[k] = data[nPos + i];
yuv[k + 1] = data[nPos + i + 1];
k += 2;
nPos += imageWidth;
}
}

}


public byte[] cropYUV420(byte[] data,int imageW,int imageH,int newImageH){
int cropH;
int i,j,count,tmp;
byte[] yuv = new byte[imageW*newImageH*3/2];

cropH = (imageH - newImageH)/2;

count = 0;
for(j=cropH;j<cropH+newImageH;j++){
for(i=0;i<imageW;i++){
yuv[count++] = data[j*imageW+i];
}
}

//Cr Cb
tmp = imageH+cropH/2;
for(j=tmp;j<tmp + newImageH/2;j++){
for(i=0;i<imageW;i++){
yuv[count++] = data[j*imageW+i];
}
}

return yuv;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: