android dump yuv buffer为jpeg图片

开始还准备自己写转换函数,yuv转rgb,然后再压缩,最后发现直接import android.graphics.YuvImage, 就可以快速的压缩成jpeg图片。

    public void dumpYuvToJpeg(byte[] data, int width, int height) {
        YuvImage image = new YuvImage(data, ImageFormat.NV21, width, height, null);
        if(image!=null) {
            FileOutputStream stream = null;
            try {
                stream = new FileOutputStream("/sdcard/"  + mDumpIdx +  ".jpg" );
                if (stream != null) {
                    image.compressToJpeg(new Rect(0, 0, width, height), 80, stream);
                    stream.close();
                }
                mDumpIdx++;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

你可能感兴趣的:(android dump yuv buffer为jpeg图片)