I am trying to lock focus after my custom camera finds focus. First it AF mode set to auto:
builder.set(CaptureRequest.CONTROL_AF_MODE,
CaptureRequest.CONTROL_AF_MODE_AUTO);
And After touching the preview it finds focus distance, and I have to lock AF and AE using this code:
builder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
builder.set(CaptureRequest.CONTROL_AE_LOCK, true);
Locking AE works fine in any devices. Locking AF is working on Nexus5 and Nexus 5x. But as for Samsung S5 and S6, it keeps try to search focus.
What is the best way to lock focus?
In order to lock the AF you have to take care of requesting the
AF_TRIGGER
only once by usingcapture()
instead ofrepeatingRequest()
(if not it enters in an af request loop and remains always trying to focus, but some nexus fix this in its FW, so some devices asNexus 5
focus well even it shouldn't)So, the correct order will be:
Set
CONTROL_AF_MODE
toCONTROL_AF_MODE_AUTO
(viasession.setRepeatingRequest()
) and theAF_REGIONS
and theAE_REGIONS
if you wantWait until you check that the
CONTROL_AF_MODE
is already in auto by checking thetotalCaptureRequest
from theCaptureCallback
.Set the
AF_TRIGGER_START
in the builder along with theCONTROL_AF_MODE_AUTO
but this time instead of usingsession.setRepeatingRequest()
usesession.capture()
.Inmediately after that, set the
AF_TRIGGER
to set theAF_TRIGGER_IDLE
(not cancel!) an use againsession.setRepeatingRequest()
along with theCONTROL_AF_MODE_AUTO
.Wait until it has focused,you will receive
FOCUSED_LOCKED
orNOT_FOCUSED_LOCKED
.The
PASSIVE_FOCUSED
state is only when theCONTROL_AF_MODE
is in continuous picture not in auto!Take care of being really in auto focus mode before performing the trigger.
You should use always
session.capture()
with all triggers (withCONTROL_AE_PRECAPTURE_TRIGGER
too) but always after that remember to put the triggers toIDLE
(not cancel) in asession.repeatingRequest()
You can't locus the focus on
CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE
.You should put your AF mode on
CONTROL_AF_MODE_AUTO
adn wait forFOCUSED_LOCKED
state during your AF trigger. You can check how the Android focus machine works on enter link description here