Android decode Bitmap from Camera Preview

怎甘沉沦 提交于 2019-12-06 07:29:39

On API level 8 or higher you can compress the images very quickly to JPEG using the following code.

public void onPreviewFrame(byte[] data, Camera arg1) {
    FileOutputStream outStream = null;
    try {
        YuvImage yuvimage = new YuvImage(data,ImageFormat.NV21,arg1.getParameters().getPreviewSize().width,arg1.getParameters().getPreviewSize().height,null);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        yuvimage.compressToJpeg(new Rect(0,0,arg1.getParameters().getPreviewSize().width,arg1.getParameters().getPreviewSize().height), 80, baos);

        outStream = new FileOutputStream(String.format("/sdcard/%d.jpg", System.currentTimeMillis()));  
        outStream.write(baos.toByteArray());
        outStream.close();

        Log.d(TAG, "onPreviewFrame - wrote bytes: " + data.length);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
    }
    Preview.this.invalidate();
}

From https://code.google.com/p/android/issues/detail?id=823#c37

There's a detailed information about your issue here: https://code.google.com/p/android/issues/detail?id=823

You're using getPictureSize instead of getPreviewSize :)

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