Is there a simple way to turn the byte array from the camera's onPreviewFrame into a picture in android?

依然范特西╮ 提交于 2019-12-01 02:52:33

问题


I ask if there is a simple way because there is a google issue report saying that using decodeByteArray isn't possible. But that report originated in 2008 and I was hoping there was a solution not posted on there. The method listed on the issue report was to decode the format yourself, but I'd prefer to not have to put that in and slow down the program. Any help at all would be appreciated.


回答1:


The easiest way is to create a BufferedImage the following way:

Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0. data.length);

data is the byte array.




回答2:


I'm assuming your byte array is from the camera preview? If so you have to decode it but with 2.2 it's quite easy now.

Create a YUV image from the byte array as the data will only be in ImageFormat.NV21( int code 17)

img = new YuvImage(imgData, ImageFormat.NV21, width, height, null);

Create a rectangle the same size as the image.

Create a ByteArrayOutputStream and pass this, the rectangle and the compression value to compressToJpeg().

Then you can use

Bitmap mBitmap = BitmapFactory.decodeByteArray(outputStream.toByteArry(),0,outputStream.size());

I use this for every frame in the callback and it works fine. Hope this helps.



来源:https://stackoverflow.com/questions/3321504/is-there-a-simple-way-to-turn-the-byte-array-from-the-cameras-onpreviewframe-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!