8

I am using CameraX

Here is my image capture :

 mImageCapture = ImageCapture.Builder()
            .setCaptureMode(ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY)
            .setTargetAspectRatio(screenAspectRatio)
            .build()

ImageCaptureListener :

mImageCapture.takePicture(
                executor!!,
                object : ImageCapture.OnImageCapturedCallback() {


                    override fun onCaptureSuccess(image: ImageProxy) {
                        Log.d("AAAA", "Success")

                            val rotatedBitmap = bitmapHelper.rotateImage(
                                bitmapHelper.imageToBitmap(image = image.image!!),
                                image.imageInfo.rotationDegrees.toFloat()
                            )

                            runOnUiThread {
                                mImageView.setImageBitmap(rotatedBitmap)
                            }

                    }

                    override fun onError(
                        imageCaptureError: Int,
                        message: String,
                        cause: Throwable?
                    ) {
                        2
                        super.onError(imageCaptureError, message, cause)
                    }
                })

When i call takePicture app freezes, and only after 3-4 seconds onCaptureSuccess called

How can I make this process faster?

Rasul
  • 727
  • 7
  • 20
  • I think you need call `image.close()`. Try it, before `runOnUiThread` – Victor Gomes Apr 14 '20 at 02:12
  • 1
    Hey @Rasul did you ever able to find out why it was slow. I mean I don't have anything like file operations and still only cameraX is taking about 1.5 seconds – AJay Aug 22 '20 at 06:23
  • 1
    Hello Rasul Agakishiyev, have you found solution for this problem? Could you share it to me or people in need? – TaQuangTu Oct 09 '20 at 06:45
  • I think that in your case rotating the bitmap is causing the delay – kostik Jul 20 '21 at 07:31

3 Answers3

1

I was comparing this solution with the freeze view solution described in stackoverflow and the second one is faster than getting the image with OnImageCapturedCallback. This is an example of how to freeze the screen and show the ImageProxy using ImageAnalysis, check the following example github

Camilo
  • 31
  • 3
  • Nice idea but not works. This is your code result: https://user-images.githubusercontent.com/9253378/207067746-5ef15e4d-5b80-4b91-8ad0-e1f3dca27e6b.png – Binh Ho Dec 12 '22 at 14:17
  • I checked the repository. I ran the app which works in a Samsung Galaxy A21 s [GIF](https://github.com/camroga/camera-x/blob/master/gif/camera-x.gif). @BinhHo can you add more details in order to solve this issue? – Camilo Feb 07 '23 at 18:26
  • This issue related to https://stackoverflow.com/questions/56772967/converting-imageproxy-to-bitmap – Binh Ho Feb 16 '23 at 03:31
  • And using this to resolve the issue: https://github.com/alexcohn/camera-samples/blob/f3b797736153c6e0d03cf05e497677dfd168e765/CameraXTfLite/utils/src/main/java/com/example/android/camera/utils/YuvToRgbConverter.kt#L100 – Binh Ho Feb 16 '23 at 03:32
1

CameraX take photo slow because of compressing into JPEG.

The difference between MINIMIZE_LATENCY_MODE and MAXIMIZE_QUALITY_MODE is the JPEG image compression quality.

  private static final byte JPEG_QUALITY_MAXIMIZE_QUALITY_MODE = 100;
  private static final byte JPEG_QUALITY_MINIMIZE_LATENCY_MODE = 95;

Therefore, even if the MINIMIZE_LATENCY_MODE mode is set, the image quality will only decrease from 100 to 95. Not much difference.

From the Version 1.1.0-alpha11, the API provides an additional function setJpegQuality that allows to customize the compression quality. Can improve the delay when taking pictures.

If you don't care about the best image quality, you can use this method.

 imageCapture = ImageCapture.Builder()
            .setJpegQuality(QUALITY_JPEG_75)
            .build()
tr4n
  • 122
  • 8
0

You no need to use ImageAnalysis usecase because CameraX already supported get bitmap from previewView

See more detail here: https://developer.android.com/reference/androidx/camera/view/PreviewView#getBitmap()

Binh Ho
  • 3,690
  • 1
  • 31
  • 31