CameraView takes about 2 seconds to capture a picture

Viewed 290

I am using CameraView from androidx components, when I use takePicutre method it takes about 2 seconds to call onImageSaved method in ImageCapture.OnImageSavedListener listener, which makes the user feels like there is something wrong.

here is my code:

private fun takePicture() {
    Log.d("ThreadRunning", Thread.currentThread().name)
    val file = File(filesDir, UUID.randomUUID().toString())
    val executor = Executors.newSingleThreadExecutor()
    val startTime = System.currentTimeMillis()
    cameraView?.takePicture(file, executor,
            object : ImageCapture.OnImageSavedListener {
                override fun onError(
                        error: ImageCapture.ImageCaptureError,
                        message: String,
                        exc: Throwable?
                ) {
                    runOnUiThread {
                        captureButton.visibility = View.VISIBLE
                        cameraView.visibility = View.VISIBLE
                    }
                    Log.d("CaptureClicked", "{$message}")
                }

                override fun onImageSaved(file: File) {

                    val bmOptions = BitmapFactory.Options()
                    var bitmap = BitmapFactory.decodeFile(file.absolutePath, bmOptions)
                    val displayMetrics = DisplayMetrics()
                    windowManager?.defaultDisplay?.getMetrics(displayMetrics)
                    //val width = displayMetrics.widthPixels
                    //val height = displayMetrics.heightPixels
                    val width = cameraView.width
                    val height = cameraView.height
                    val numratorValue = (height.toDouble() / width.toDouble()).roundToInt()
                    bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true)
                    bitmap = if (cameraView.cameraLensFacing == CameraX.LensFacing.FRONT) {
                        bitmap.rotate(270F).mirror()
                    } else {
                        bitmap.rotate(90F)
                    }
                    runOnUiThread{
                        capturedImageView.setImageBitmap(bitmap)
                        capturedImageView.visibility = View.VISIBLE
                        cancelButton.visibility = View.VISIBLE
                        captureButton.visibility = View.INVISIBLE

                    }


                    Log.d("TestDelay", "${System.currentTimeMillis() - startTime}")
                }
            }
    )

}

private fun Bitmap.rotate(degrees: Float): Bitmap {
    val matrix = Matrix().apply {
        postRotate(degrees)
    }
    return Bitmap.createBitmap(this, 0, 0, width, height, matrix, true)
}

private fun Bitmap.mirror(): Bitmap {
    val matrix = Matrix().apply {
        preScale(-1F, 1F)
    }
    return Bitmap.createBitmap(this, 0, 0, width, height, matrix, true)
}

And here is the xml element for cameraView

<androidx.camera.view.CameraView
    android:id="@+id/cameraView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:captureMode="image"/>

Another question when I use takePicture method in Fragment, I get a black screen for 0.5 seconds, I think because of the lifecyclerOwner object but I don't know where should I put the bind method and where unbind it exactly.

So I have two questions

1 - Why does camera view take too much time to capture a picutre and how to decrease this time ?

2 - Why do I get a black screen after calling takePicutre method when using Fragment ?

0 Answers
Related