LogUtil.i("previewSize", previewSize.width+" "+previewSize.height); // Decode image from the retrieved buffer to JPEG yuv = new YuvImage(data, ImageFormat.NV21,previewSize.width, previewSize.height, null); yuv.compressToJpeg(new Rect(0, 0,previewSize.width, previewSize.height),100, baos); rawImage = baos.toByteArray(); // This is the same image as the preview but in JPEG // and not rotated bitmap = BitmapFactory.decodeByteArray(rawImage, 0, rawImage.length); // We rotate the same Bitmap // 旋转 matrix.postRotate(90); bitmap = Bitmap.createBitmap(bitmap, 0, 0,previewSize.width, previewSize.height,matrix, false); // We dump the rotated Bitmap to the stream bitmap.compress(CompressFormat.JPEG,100, rotatedStream); rawImage = rotatedStream.toByteArray(); // rawImage就是最后要用的视频流了
我也套用了……if (data.length != 0)
{
previewSize = camera.getParameters().getPreviewSize();//后面用于获取长、宽
yuv = new YuvImage(data, ImageFormat.NV21,previewSize.width, previewSize.height, null);
LogUtil.i("previewSize", previewSize.width+" "+previewSize.height);
// Decode image from the retrieved buffer to JPEG
yuv = new YuvImage(data, ImageFormat.NV21,previewSize.width, previewSize.height, null);
yuv.compressToJpeg(new Rect(0, 0,previewSize.width, previewSize.height),100, baos); rawImage = baos.toByteArray();
// This is the same image as the preview but in JPEG
// and not rotated
bitmap = BitmapFactory.decodeByteArray(rawImage, 0, rawImage.length); // We rotate the same Bitmap
// 旋转
matrix.postRotate(90);
bitmap = Bitmap.createBitmap(bitmap, 0, 0,previewSize.width, previewSize.height,matrix, false); // We dump the rotated Bitmap to the stream
bitmap.compress(CompressFormat.JPEG,100, rotatedStream);
rawImage = rotatedStream.toByteArray();
// rawImage就是最后要用的视频流了
代码如上。我运行后发现,rawImage的大小比原本data[]大,还有增加的趋势。应该是哪里写错了……
这里没声明的变量都是全局的。rawImage 要清空么?
能否帮忙看下,谢谢~
目前使用stackoverflow提供的方法,转为jpeg时,哪怕质量设置为100,获得的数据流依然比之前的小。损失很大。
而使用对视频流流Byte数组直接变换方向,则有了初步效果。
android预览的效果默认为NV21和yv12(华为G750-T00),在目前的项目中使用算法转换后,再通知下层长和宽对调了。
得到的结果是:图像方向算是正确,还需要做一次镜像处理。不过颜色没了,估计是YUV算法里,UV没调整对。还有待改进。
希望有研究的人可以分享下自己的结果。
项目中用的是YV12的转换算法。排列是U在前V在后,而I420是V在前U在后,所占位子一样。
NV21除了Y是一样的,UV是排列在一起的。百度一下有很多资料。
我并不觉得这是最优解,不过基于没有找到资料,只能说出这些了。希望日后有大神看到这个帖子后,多写优秀的文章分享大家,如同《超体》所提到:生命的意义就在于知识的传递。