简体   繁体   中英

disable autofocus in android camerax (camera 2)

I'm in project of scanning barcode, so i want to disable auto-focus for improving performance. I tried so many ways but it doesn't work at all. Could anyone give me some help? Thank you.

If you really want to turn off AF, you can do this on CameraX with the Camera2CameraControl class. To do this you have to first bind the use cases you need to the lifecycle which results in a Camera object, you can then use that camera object to get the CameraControl object and then use it to instantiate a Camera2CameraControl which would let you set the focus mode to CameraMetadata.CONTROL_AF_MODE_OFF .

    val camera : Camera = cameraProvider.bindToLifecycle(
                        this,
                        cameraSelector,
                        imagePreview,
                        imageCapture,
                        imageAnalysis
                    )
    
    val cameraControl : CameraControl = camera.cameraControl
    val camera2CameraControl : Camera2CameraControl = Camera2CameraControl.from(cameraControl)
    
    //Then you can set the focus mode you need like this
    val captureRequestOptions = CaptureRequestOptions.Builder()
                .setCaptureRequestOption(CaptureRequest.CONTROL_AF_MODE, CameraMetadata.CONTROL_AF_MODE_OFF)
                .build()
            camera2CameraControl.captureRequestOptions = captureRequestOptions

This was tested on the latest CameraX's "1.0.0-rc03" build.

I use

disableAutoCancel()

with cameraX 1.0.0. The camera focuses once then stays locked, autofocus is not restarted at every X seconds, so something like

    val autoFocusAction = FocusMeteringAction.Builder(
                    autoFocusPoint,
                    FocusMeteringAction.FLAG_AF or
                            FocusMeteringAction.FLAG_AE or
                            FocusMeteringAction.FLAG_AWB
                ).apply {
                            disableAutoCancel()      
                    }
                }.build()
    
myCameraControl!!.startFocusAndMetering(autoFocusAction)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM