开始还准备自己写转换函数,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();
            }
        }
    }
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐