0

In an Android application, we can easily save a YUV frame to image using below code:

try {
            YuvImage image = new YuvImage(data, format, width, height, null);
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
            File file = new File(imageStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpeg");
            FileOutputStream fileOut = new FileOutputStream(file);
            image.compressToJpeg(new Rect(0, 0, image.getWidth(), image.getHeight()), 90,
                    fileOut);
        } catch (Exception e) {
            e.printStackTrace();
        }

But my current function returns an RGBA frame so how could I save it to an image? Because I saw that the Android framework does not support RGBA class.

Puja
  • 192
  • 9
  • 1
    Use Android Bitmap class and then call the [compress](https://developer.android.com/reference/android/graphics/Bitmap#compress(android.graphics.Bitmap.CompressFormat,%20int,%20java.io.OutputStream)) method with the output format you want. If you need RGBA, save as PNG. If you just need RGB, then use JPEG. – James Poag Sep 11 '18 at 12:01
  • I already try to create Bitmap by this LOC: Bitmap image = BitmapFactory.decodeByteArray(data, 0, data.length); But image object is NULL. Do you have idea for this? – Tran Intent Sep 11 '18 at 13:19
  • 1
    decodeByteArray is for compressed streams, you want [Bitmap.createBitmap](https://developer.android.com/reference/android/graphics/Bitmap#createBitmap(int[],%20int,%20int,%20android.graphics.Bitmap.Config)) – James Poag Sep 11 '18 at 13:33
  • I saw that Bitmap.createBitmap does not support input is byte[] type, only int[]. Do i need to convert byte[] data to int[] data first? – Tran Intent Sep 11 '18 at 13:37
  • 1
    https://stackoverflow.com/questions/11437203/byte-array-to-int-array – James Poag Sep 11 '18 at 13:39
  • I could create Bitmap to save to image on Andorid 8.0 and upper device with this: Bitmap image = Bitmap.createBitmap(data, width, height, Bitmap.Config.RGBA_F16). But lower then 8.0 does not support RGBA_F16 config yet so i can not create bitmap from frame. Do you have any suggestion? – Tran Intent Oct 15 '18 at 10:45

0 Answers0