How to save a YUV_420_888 image?

前端 未结 2 1542
梦如初夏
梦如初夏 2021-01-19 06:07

I built my own camera app with the camera2 API. I started with the sample \"camera2Raw\" and I added YUV_420_888 support instead of JPEG. But now I am wondering how I save t

2条回答
  •  长发绾君心
    2021-01-19 07:11

    If you have the YuvImage object then you can convert it to Jpeg using the compressToJpeg function like so.

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    YuvImage yuvImage = new YuvImage(data, ImageFormat.NV21, width, height, null);
    yuvImage.compressToJpeg(new Rect(0, 0, width, height), 50, out);
    byte[] imageBytes = out.toByteArray();
    Bitmap image = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
    iv.setImageBitmap(image);
    

    You have to set the width and height of the image explicitly though.

提交回复
热议问题