基于Camera2Basic的相机Fragment常亮闪光灯模式预捕获序列卡滞求助
Hey there, let's dig into this pre-capture hang issue you're facing when using the always-on flash mode in your Camera2Basic-based fragment. I've dealt with similar Camera2 quirks before, so here are actionable fixes and checks to try:
1. Fix Pre-Capture Callback State Handling
The most common culprit here is waiting indefinitely for AE convergence when flash is set to torch mode. Some devices behave differently in this scenario—AE might never send the AE_PRECAPTURE_CONVERGED signal because the flash is already locked on full brightness.
- Add a timeout mechanism: If the pre-capture sequence doesn't complete within a reasonable window (e.g., 1.5 seconds), skip the wait and trigger the still capture directly to avoid infinite hanging.
- Adjust callback logic: In your
CameraCaptureSession.CaptureCallback, check if the current flash mode isFLASH_MODE_TORCH. If yes, you can bypass waiting for AE convergence and proceed to capture immediately after starting the pre-capture sequence.
2. Resolve Capture Request Configuration Conflicts
Flash torch mode can clash with default AE settings, leading to stuck pre-capture sequences. Make sure your still capture request is configured correctly for torch mode:
// Example in your still capture request setup val captureBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE) when (currentFlashMode) { CaptureRequest.FLASH_MODE_TORCH -> { // Lock AE to keep flash on, instead of relying on auto AE captureBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON) captureBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_TORCH) // Ensure focus stays locked if needed captureBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE) } CaptureRequest.FLASH_MODE_AUTO -> { // Default auto flash configuration captureBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH) captureBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_AUTO) } CaptureRequest.FLASH_MODE_OFF -> { captureBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON) captureBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF) } }
3. Prevent Duplicate Pre-Capture Triggers
If your UI allows repeated tap actions to take photos, you might be firing multiple pre-capture sequences before the first one finishes, causing a queue block.
- Add a state flag: Introduce a boolean like
isPreCaptureInProgressthat you set totruewhen starting the sequence, and reset tofalsein all callback paths (success, failure, timeout). This prevents overlapping requests from clogging the pipeline.
4. Handle Device-Specific Compatibility
Some Android devices have buggy Camera2 implementations for torch mode pre-capture. For these cases:
- Detect problematic devices: Maintain a small list of devices known to have this issue (e.g., certain older Samsung or Xiaomi models).
- Skip pre-capture for affected devices: When flash is in torch mode and the device is on the list, directly trigger the still capture without running the pre-capture sequence. Most devices will still capture a usable photo even without pre-capture in torch mode.
Start with checking your callback logic and request configuration—those are the easiest fixes to implement and will resolve the issue for most devices.
内容的提问来源于stack exchange,提问作者EarlGrey

