Android开发:实时处理摄像头预览帧视频------浅析PreviewCallback,onPreviewFrame,AsyncTask的综合应用 这里将大致框架介绍了,但很多人对onPreviewFrame()里的处理提出质疑。认为下面的转换是多余的:
final YuvImage image = new YuvImage(mData, ImageFormat.NV21, w, h, null); ByteArrayOutputStream os = new ByteArrayOutputStream(mData.length); if(!image.compressToJpeg(new Rect(0, 0, w, h), 100, os)){ return null; } byte[] tmp = os.toByteArray(); Bitmap bmp = BitmapFactory.decodeByteArray(tmp, 0,tmp.length);
因为这个mData是byte[ ]格式,转换流程是:byte[ ]---YuvImage----ByteArrayOutputStream---byte[ ]-----Bitmap。乍一看这个转换还真是多余了。看看看goolge的api:
public abstract void onPreviewFrame (byte[] data, Camera camera)Added in API level 1Called as preview frames are displayed. This callback is invoked on the event thread open(int) was called from.If using the YV12 format, refer to the equations in setPreviewFormat(int) for the arrangement of the pixel data in the preview callback buffers.Parametersdata the contents of the preview frame in the format defined by ImageFormat, which can be queried with getPreviewFormat(). If setPreviewFormat(int) is never called, the default will be the YCbCr_420_SP (NV21) format.camera the Camera service object.
大致意思是:可以用getPreviewFormat()查询
支持的预览帧格式。如果setPreviewFormat(INT)
从未被调用,默认将使用YCbCr_420_SP的格式(NV21)。
setPreviewFormat里,它又说:
public void setPreviewFormat (int pixel_format)Added in API level 1Sets the image format for preview pictures.If this is never called, the default format will be NV21, which uses the NV21 encoding format.Use getSupportedPreviewFormats() to get a list of the available preview formats.It is strongly recommended