1

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,947
  • 5
  • 29
  • 51
Akash Jain
  • 684
  • 9
  • 23
  • 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
  • @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

0

I have solved the issue by using the below code

    private void takePicture() {
        if (imageCapture != null) {
            ByteArrayOutputStream result = new ByteArrayOutputStream();
            getCacheDir();
            imageCapture.takePicture(new ImageCapture.OutputFileOptions.Builder(result).build(),
                    Executors.newSingleThreadExecutor(),
                    new ImageCapture.OnImageSavedCallback() {
                        @Override
                        public void onImageSaved(@NonNull ImageCapture.OutputFileResults outputFileResults) {
                            File cache = new File(getCacheDir().getAbsolutePath() + "/" + "captured.png");
                            try (FileOutputStream stream = new FileOutputStream(cache)) {
                                stream.write(result.toByteArray());
                                upload(cache); // your upload function
                            } catch (Exception e) {
                                Log.e(TAG, "onImageSaved: Exception occurred", e);
                            }
                        }

                        @Override
                        public void onError(@NonNull ImageCaptureException exception) {
                            Log.i(TAG, "onError: ", exception);
                        }
                    });
        }
    }

thanks to @Commonsware

Akash Jain
  • 684
  • 9
  • 23