Android OpenGL YUV 旋转花屏解决及Camera获取图像

一个自拍app,如何获取我们的surface view的截屏?

在Camera的onPreviewFrame回调中(继承的PreviewCallback接口)。我们可以获取到data数组,里面存放着就是实时的图片

data数组转文件为null,且转bitmap也为null怎么办?

data数组里确实存放的是图片,但是格式不对,是YUV420格式。需要进行一个转换。所以采用open gl包下的YuvImage这个类可以解决转化的问题,他有一个compressToJpeg方法。

转换成jpg格式,存到手机文件中,发现图片与拍摄出来的不符合,是经过90度旋转的图像怎么办(这里采用的是手机前置摄像头)?

对byte数组进行1.旋转2.镜像翻转(避免获取出来是反的)即可。再给算法处理后的新数组交给YUV去转换即可。

转换出来发现方向是对了,但是花屏了怎么办(这里是屏幕中出现了4个人并且重影了)?

是因为对byte数组旋转后,比如原来是100*200,旋转后就成了200*100,这个时候你不能把100,200分别交给YUV的宽高了,因为你大小都没指定对,解码自然会错误。而是应该给200,100。

代码

图片数组

?
1
<strong> public void </strong>onPreviewFrame(<strong> byte </strong>[] data, Camera camera) {

就是这个data

顺时针旋转270度代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<strong> private byte </strong>[] rotateYUVDegree270AndMirror(<strong> byte </strong>[] data, <strong> int </strong>imageWidth, <strong> int </strong>imageHeight) {
     <strong> byte </strong>[] yuv = <strong> new byte </strong>[imageWidth * imageHeight * 3 / 2 ];
     <em> // Rotate and mirror the Y luma
</em><em>    </em><strong> int </strong>i = 0 ;
     <strong> int </strong>maxY = 0 ;
     <strong> for </strong>(<strong> int </strong>x = imageWidth - 1 ; x >= 0 ; x--) {
         maxY = imageWidth * (imageHeight - 1 ) + x * 2 ;
         <strong> for </strong>(<strong> int </strong>y = 0 ; y < imageHeight; y++) {
             yuv[i] = data[maxY - (y * imageWidth + x)];
             i++;
         }
     }
     <em> // Rotate and mirror the U and V color components
</em><em>    </em><strong> int </strong>uvSize = imageWidth * imageHeight;
     i = uvSize;
     <strong> int </strong>maxUV = 0 ;
     <strong> for </strong>(<strong> int </strong>x = imageWidth - 1 ; x > 0 ; x = x - 2 ) {
         maxUV = imageWidth * (imageHeight / 2 - 1 ) + x * 2 + uvSize;
         <strong> for </strong>(<strong> int </strong>y = 0 ; y < imageHeight / 2 ; y++) {
             yuv[i] = data[maxUV - 2 - (y * imageWidth + x - 1 )];
             i++;
             yuv[i] = data[maxUV - (y * imageWidth + x)];
             i++;
         }
     }
     <strong> return </strong>yuv;
}

使用

?
1
2
3
Camera.Size previewSize = <strong>mCamera</strong>.getParameters().getPreviewSize(); //获取照相机宽高
 
<strong> byte </strong>[] newData = rotateYUVDegree270AndMirror(data, previewSize.<strong>width</strong>, previewSize.<strong>height</strong>);

交换宽高传入YUV

?
1
2
3
4
5
6
YuvImage yuvimage = <strong> new </strong>YuvImage(
         newData,
         ImageFormat.<strong><em>NV21</em></strong>,
         previewSize.<strong>height</strong>, //这里交换了宽高
         previewSize.<strong>width</strong>,
         <strong> null </strong>);

解码,宽高也是反向传入

?
1
2
3
4
5
6
7
8
9
10
11
12
13
FileOutputStream f = <strong> null </strong>;
<strong> try </strong>{
     f = <strong> new </strong>FileOutputStream(file);
} <strong> catch </strong>(FileNotFoundException e) {
     e.printStackTrace();
}
yuvimage.compressToJpeg(<strong> new </strong>Rect( 0 , 0 , previewSize.<strong>height</strong>, previewSize.<strong>width</strong>), 100 , f);
<strong> try </strong>{
     f.flush();
     f.close();
} <strong> catch </strong>(IOException e) {
     e.printStackTrace();
}

搞定了,经过解码、旋转、镜像翻转、IO的byte数组,成功转化成正常图片文件保存到了本地!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值