So, I managed to create the functionality i wanted with the old camera the way I wanted it.
With mCamera.autoFocus(autoFocusCallback); i detect when I have focus and run the required code while in preview-mode.
Now I have a hard time grasping how to do the same in camera2 API. My first idea was that i'd use
private void process(CaptureResult result) {
switch (mState) {
case STATE_PREVIEW: {
// We have nothing to do when the camera preview is working normally.
int afState = result.get(CaptureResult.CONTROL_AF_STATE);
//if (CaptureResult.CONTROL_AF_STATE == afState) {
Log.d("SOME KIND OF FOCUS", "WE HAVE");
//}
break;
}
}
but I fail to find some kind of state that tells me we have gotten focus. Does someone have any idea how this can be done with Camera2 API?
For those interested I ended up with a mix of this:
private CameraCaptureSession.CaptureCallback mCaptureCallback
= new CameraCaptureSession.CaptureCallback() {
private void process(CaptureResult result) {
switch (mState) {
case STATE_PREVIEW: {
int afState = result.get(CaptureResult.CONTROL_AF_STATE);
if (CaptureResult.CONTROL_AF_TRIGGER_START == afState) {
if (areWeFocused) {
//Run specific task here
}
}
if (CaptureResult.CONTROL_AF_STATE_PASSIVE_FOCUSED == afState) {
areWeFocused = true;
} else {
areWeFocused = false;
}
break;
}
}
}
@Override
public void onCaptureProgressed(CameraCaptureSession session, CaptureRequest request,
CaptureResult partialResult) {
process(partialResult);
}
@Override
public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request,
TotalCaptureResult result) {
process(result);
}
};
It works good enough :)
You've basically got it. The list of states you can check for and their transitions can be found here.
It depends on what CONTROL_AF_MODE
you are using, but generally you check for FOCUSED_LOCKED
or perhaps PASSIVE_FOCUSED
, though you may want to have cases for NOT_FOCUSED_LOCKED
and PASSIVE_UNFOCUSED
in case the camera just cannot focus on the scene.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With