Skip to content

[camera_android] Reset AE and AF triggers to idle after capture#11330

Open
q2a3z wants to merge 1 commit intoflutter:mainfrom
q2a3z:fix/Camera2-ae-af-reset
Open

[camera_android] Reset AE and AF triggers to idle after capture#11330
q2a3z wants to merge 1 commit intoflutter:mainfrom
q2a3z:fix/Camera2-ae-af-reset

Conversation

@q2a3z
Copy link

@q2a3z q2a3z commented Mar 23, 2026

Issue

App freezes during focus/flash sequence in low light.
On high-end camera devices, focusing takes longer in dark environments. Repeatedly requesting focus while using the flash can cause the CaptureRequest to hang.

The issues has been resolved.
I reproduced the same behavior on a different device (not an Xperia).

This log has been captured on my Xperia device.

D/CameraCaptureCallback(12647): CameraCaptureCallback | state: STATE_CAPTURING | afState: 1 | aeState: 5
D/CameraCaptureCallback(12647): CameraCaptureCallback | state: STATE_CAPTURING | afState: 1 | aeState: 5
D/CameraCaptureCallback(12647): CameraCaptureCallback | state: STATE_CAPTURING | afState: 5 | aeState: 4
D/CameraCaptureCallback(12647): CameraCaptureCallback | state: STATE_CAPTURING | afState: 5 | aeState: 4
D/CameraCaptureCallback(12647): CameraCaptureCallback | state: STATE_CAPTURING | afState: 1 | aeState: 4
I/Camera  (12647): refreshPreviewCaptureSession

Root Cause

Camera2 trigger controls are driven by one-shot states (START), but they are not always reset to IDLE once the sequence is complete. On certain devices (like the Xperia 1), this failure to reset can block subsequent capture requests.

Fix

runPrecaptureSequence():
Send an AE trigger START, then immediately follow with IDLE in the same execution path.

lockAutoFocus():
Send an AF trigger START, then reset to IDLE after the preview request is captured.

Results

AE/AF triggers are now consistently returned to the IDLE state.
The freeze during capture after using the flash on Xperia 1 has been resolved.
Camera trigger sequences are now stable across various device models.


By the way,

I believe CameraX is the way to go for Android camera development. It's been the default since version 0.11.0.

Pre-Review Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the gemini-code-assist bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.

Footnotes

  1. Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling. 2

@q2a3z q2a3z requested a review from camsim99 as a code owner March 23, 2026 14:09
@flutter-dashboard
Copy link

It looks like this pull request may not have tests. Please make sure to add tests or get an explicit test exemption before merging.

If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix?

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing. If you believe this PR qualifies for a test exemption, contact "@test-exemption-reviewer" in the #hackers channel in Discord (don't just cc them here, they won't see it!). The test exemption team is a small volunteer group, so all reviewers should feel empowered to ask for tests, without delegating that responsibility entirely to the test exemption group.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

The pull request modifies the Camera.java file to explicitly reset camera capture triggers to an idle state. Specifically, it adds code to reset CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER to IDLE after runPrecaptureSequence and CaptureRequest.CONTROL_AF_TRIGGER to IDLE after lockAutoFocus, ensuring proper state management for auto-exposure and auto-focus operations.

Copy link
Contributor

@camsim99 camsim99 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this fix! At a high level, this makes sense to me, but there's two things I want to address:

1: The main concern that I have is that CameraCaptureSession.capture is an asynchronous call, so I worry we may be resetting the AE and AF triggers back to idle too soon potentially. Can you clarify if this is true? If so, a solution may be to provide a callback to set the triggers to idle.

2: In order to land this, we will need tests for this change. A good place to add them would be in CameraTest.java.

@q2a3z
Copy link
Author

q2a3z commented Mar 23, 2026

@camsim99

Thank you for the quick reply and the suggestions.

Yap, I also thought it might be an async function. It seems like the flash turns off just a split second before the photo is captured. I'll try your suggestion and let you know as soon as I have any updates.

Once I've finished checking step 1, I’ll add some tests to CameraTest.java.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants