'android camera2 Api autoflash not working properly, the first picture is taken before the flash fires

I'm using camera 2 API, my project is heavily based on the archived project from google: https://github.com/googlearchive/android-Camera2Basic/

I'm starting the preview in the same way:

 private fun createCameraPreviewSession() {
    try {
        val texture = textureView.surfaceTexture

        // We configure the size of default buffer to be the size of camera preview we want.
        texture.setDefaultBufferSize(previewSize.width, previewSize.height)

        // This is the output Surface we need to start preview.
        val surface = Surface(texture)

        // We set up a CaptureRequest.Builder with the output Surface.
        previewRequestBuilder = cameraDevice!!.createCaptureRequest(
                CameraDevice.TEMPLATE_PREVIEW
        )
        previewRequestBuilder.addTarget(surface)

        // Here, we create a CameraCaptureSession for camera preview.
        cameraDevice?.createCaptureSession(Arrays.asList(surface, imageReader?.surface),
                object : CameraCaptureSession.StateCallback() {

                    override fun onConfigured(cameraCaptureSession: CameraCaptureSession) {
                        // The camera is already closed
                        if (cameraDevice == null) return

                        // When the session is ready, we start displaying the preview.
                        captureSession = cameraCaptureSession
                        try {
                            // Auto focus should be continuous for camera preview.
                            previewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,
                                    CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE)
                            // Flash is automatically enabled when necessary.
                            setAutoFlash(previewRequestBuilder)

                            // Finally, we start displaying the camera preview.
                            previewRequest = previewRequestBuilder.build()
                            captureSession?.setRepeatingRequest(previewRequest,
                                    captureCallback, backgroundHandler)
                        } catch (e: CameraAccessException) {
                            Log.e(TAG, e.toString())
                        }

                    }

                    override fun onConfigureFailed(session: CameraCaptureSession) {
                        activity.showToast("Failed")
                    }
                }, null)
    } catch (e: CameraAccessException) {
        Log.e(TAG, e.toString())
    }

} 

I'm capturing the image, running pre-capture, capturing still image, changing the states in the same way as in the project above. However, the first picture with auto flash after opening the app, will always be taken before the flash fires (it'll come out very dark). All of the pictures taken after that single fail, will come out just fine.I've tried changing the process function with all of the states, but can't figure it out.



Solution 1:[1]

Does the camera2basic sample by itself work as expected? If it does, then there's something different in your code to track down. If it doesn't, then there may be a device-specific bug, which would be frustrating.

I would double-check that you're setting the flash mode to AUTO for the preview request at the very beginning, as one thing to look at.

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 Eddy Talvala