0

I just started with androidX camera, I'm using it's functionality imagecatpure.takePicture() , and I get ImageProxy as an object, so now my question is how can i convert this to jpeg/png/jpg so I can upload the image through an api?

is there another more efficient way to achieve this?

a_local_nobody
  • 7,360
  • 5
  • 25
  • 45
Akash Jain
  • 547
  • 4
  • 17
  • 1
    Perhaps you should use the other `takePicture()` variant and have it write to a `ByteArrayOutputStream`: https://developer.android.com/reference/androidx/camera/core/ImageCapture.OutputFileOptions.Builder. – CommonsWare Apr 08 '22 at 13:31
  • @CommonsWare technically I do not want to store in localstorage just in a cache where i can use that to upload the image – Akash Jain Apr 08 '22 at 13:38
  • 1
    That is why I suggested using `ByteArrayOutputStream`. – CommonsWare Apr 08 '22 at 13:42
  • @CommonsWare curious, how would i convert the ByteStream to Jpeg or Png? – Akash Jain Apr 11 '22 at 12:22
  • It already would be a JPEG, no different than if you specified the image be written to a file. – CommonsWare Apr 11 '22 at 12:32
  • so can i upload this through an Api? with appropriate headers? – Akash Jain Apr 11 '22 at 13:01
  • 1
    Personally, I would be uploading from a file, to avoid `OutOfMemoryErrors`. Regardless, the `ByteArrayOutputStream` will give you a `byte[]`/`ByteArray` that contains the exact same bytes as would the file if you had written the photo to that file. I cannot tell you how well that will work with your planned upload mechanism. – CommonsWare Apr 11 '22 at 13:05
  • very well, I always wanted to avoid requesting permission from the user I guess that's the best way, Thank you, this worked perfectly. – Akash Jain Apr 11 '22 at 13:11
  • 1
    "I always wanted to avoid requesting permission from the user" -- I do not know what permission you are referring to. If you are referring to writing to a file, there are several locations where you do not need permission. I would use `getCacheDir()` on `Context` for something like this. – CommonsWare Apr 11 '22 at 13:19
  • Brilliant! this works like a charm. – Akash Jain Apr 11 '22 at 13:29
  • @CommonsWare not related to the current question but is there way to compress the above image, it's 5 mb for me? – Akash Jain Apr 12 '22 at 07:38
  • 1
    JPEGs already are compressed. If you want a lower resolution image, see if there are options to capture a lower-resolution image from the camera. – CommonsWare Apr 12 '22 at 11:10

1 Answers1

-1

You can convert the ImageProxy to Bitmap and then Save it as JPEG/JPG/PNG from that Bitmap.

Image Proxy to Bitmap Convert:

private Bitmap imageProxyToBitmap(ImageProxy image) {
    ByteBuffer buffer = image.getPlanes()[0].getBuffer();
    byte[] bytes = new byte[buffer.remaining()];
    buffer.get(bytes);
    return BitmapFactory.decodeByteArray(bytes,0,bytes.length,null);
}

Bitmap to Image (Modify as you need)

Bitmap bmp = null;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteArray = stream.toByteArray();

FileInputStream fileInputStream = null;

File file = new File("yourfile");

byteArray = new byte[(int) file.length()];

try {
    //convert file into array of bytes
    fileInputStream = new FileInputStream(file);
    fileInputStream.read(bFile);
    fileInputStream.close();

    //convert array of bytes into file
    FileOutputStream fileOuputStream =
            new FileOutputStream("C:\\testing2.txt");
    fileOuputStream.write(bFile);
    fileOuputStream.close();

    System.out.println("Done");
} catch (Exception e) {
    e.printStackTrace();
}

Credit: ImageProxy to Bitmap and bitmap to a jpeg

If you want to try a different approach then you can use the methods below. This method uses ImageCapture.OutputFileOptions as output.

  1. Android CameraX Java Example
  2. AndroidX Camera example