CameraX Image take picture slow
Asked Answered
M

3

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?

Meistersinger answered 22/2, 2020 at 11:11 Comment(4)
I think you need call image.close(). Try it, before runOnUiThreadArthralgia
Hey @Meistersinger 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 secondsOsteoporosis
Hello Rasul Agakishiyev, have you found solution for this problem? Could you share it to me or people in need?Cryptozoite
I think that in your case rotating the bitmap is causing the delayClaptrap
H
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

Hagiarchy answered 17/5, 2020 at 17:6 Comment(4)
Nice idea but not works. This is your code result: user-images.githubusercontent.com/9253378/…Falmouth
I checked the repository. I ran the app which works in a Samsung Galaxy A21 s GIF. @BinhHo can you add more details in order to solve this issue?Hagiarchy
This issue related to #56773467Falmouth
And using this to resolve the issue: github.com/alexcohn/camera-samples/blob/…Falmouth
E
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()
Epode answered 22/3, 2022 at 8:2 Comment(0)
F
-1

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()

Falmouth answered 16/2, 2023 at 3:35 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.