'Convert ImageProxy to Jpeg or Png
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?
Solution 1:[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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
