How to lock focus in camera2 api, android?

I try to lock focus after my user camera finds focus. First, the autofocus mode is set to auto:

builder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_AUTO); 

And after touching the preview, it finds the focus distance, and I need to lock AF and AE with this code:

 builder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE); builder.set(CaptureRequest.CONTROL_AE_LOCK, true); 

AE lock works great on any device. Autofocus lock works on Nexus5 and Nexus 5x. But as for the Samsung S5 and S6, he is trying to find the focus.

What is the best way to lock focus?

+7
android android-camera2
source share
2 answers

In order to block autofocus, you should make sure to request AF_TRIGGER only once, using capture() instead of repeatingRequest() (if it is not in the af request cycle and it remains to always try to focus, but some bundles fix this in their FW, therefore some devices like the Nexus 5 focus well, don't even have to)

So the correct order will be:

  • Set CONTROL_AF_MODE to CONTROL_AF_MODE_AUTO (via session.setRepeatingRequest() ) and AF_REGIONS and AE_REGIONS if you want

  • Wait until you verify that CONTROL_AF_MODE already in auto by checking the totalCaptureRequest from CaptureCallback .

  • Set AF_TRIGGER_START in the builder along with CONTROL_AF_MODE_AUTO , but this time use session.setRepeatingRequest() instead of session.capture() .

  • Immediately after that, set AF_TRIGGER to set AF_TRIGGER_IDLE (don't cancel!) To use session.setRepeatingRequest () along with the CONTROL_AF_MODE_AUTO` again.

  • Wait until it focuses, you get FOCUSED_LOCKED or NOT_FOCUSED_LOCKED .

The PASSIVE_FOCUSED occurs only when CONTROL_AF_MODE is in continuous mode and not in automatic mode!

Before executing a trigger, take care to be in auto focus mode.

You should always use session.capture() with all triggers (along with CONTROL_AE_PRECAPTURE_TRIGGER ), but always after that remember that the triggers are IDLE (not canceled) in session.repeatingRequest()

+10
source share

You cannot focus on CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE .

You must enable the autofocus mode at CONTROL_AF_MODE_AUTO and wait for the FOCUSED_LOCKED state during the autofocus trigger. You can check how the Android focus machine works by entering the link here

+2
source share

All Articles