Android CameraX运行时获取当前曝光、对焦、感光度等参数及自动控制锁定问题
Absolutely, you can pull real-time camera parameters like exposure time, ISO, focus state, and more using Android’s Camera2 API. Let’s walk through how to do this, plus a tailored solution for your cross-device compatibility issue.
Step 1: Grab CaptureResult from the Camera Callback
To access live camera parameters, you need to listen for CaptureResult objects in your camera session’s capture callback. These results hold the current state of all active camera controls every time a frame is captured.
Here’s how to set this up:
// Configure your camera session's capture callback CameraCaptureSession.CaptureCallback captureCallback = new CameraCaptureSession.CaptureCallback() { @Override public void onCaptureCompleted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) { super.onCaptureCompleted(session, request, result); // Extract parameters from the latest result Long exposureTime = result.get(CaptureResult.SENSOR_EXPOSURE_TIME); Integer iso = result.get(CaptureResult.SENSOR_SENSITIVITY); Float focusDistance = result.get(CaptureResult.LENS_FOCUS_DISTANCE); // Store these values—you'll need them when switching to manual controls } }; // Start repeating the preview request with the callback attached session.setRepeatingRequest(previewRequest.build(), captureCallback, backgroundHandler);
Step 2: Capture Parameters Before Disabling Auto Controls
Your goal is to avoid the "black screen" issue on Pixel 4a when using CONTROL_MODE_OFF_KEEP_STATE. The fix is to capture the auto-adjusted parameters first, then manually apply them after disabling auto controls:
- Let the camera’s auto controls stabilize (e.g., wait 5 seconds as you originally did)
- Save the latest
CaptureResultvalues for exposure time, ISO, etc. - Disable auto controls, then manually set these saved values in your
CaptureRequest
Here’s a code snippet for this flow:
// After auto controls stabilize, fetch the latest parameters Long currentExposure = latestCaptureResult.get(CaptureResult.SENSOR_EXPOSURE_TIME); Integer currentIso = latestCaptureResult.get(CaptureResult.SENSOR_SENSITIVITY); // Build a new manual capture request CaptureRequest.Builder manualRequestBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); manualRequestBuilder.set(CaptureRequest.CONTROL_MODE, CaptureRequest.CONTROL_MODE_OFF); // Apply the captured auto-adjusted values manualRequestBuilder.set(CaptureRequest.SENSOR_EXPOSURE_TIME, currentExposure); manualRequestBuilder.set(CaptureRequest.SENSOR_SENSITIVITY, currentIso); // Lock other required parameters manualRequestBuilder.set(CaptureRequest.CONTROL_AWB_LOCK, true); manualRequestBuilder.set(CaptureRequest.BLACK_LEVEL_LOCK, true); // Update the session with this manual request session.setRepeatingRequest(manualRequestBuilder.build(), captureCallback, backgroundHandler);
Step 3: Fix Cross-Device Compatibility
Your Pixel 4a vs Galaxy S7 issue stems from how different OEMs implement the Camera2 API. Here’s a targeted approach:
- Galaxy S7 (with flash enabled):
CONTROL_AE_MODE_OFFworks, but you must capture the auto-adjusted exposure/ISO first before disabling AE mode. - Pixel 4a:
CONTROL_AE_LOCKis reliable, but if you need to useCONTROL_MODE_OFF, manually apply the captured parameters to avoid black screens.
Add device-specific checks to apply the right strategy:
String manufacturer = Build.MANUFACTURER.toLowerCase(); String model = Build.MODEL.toLowerCase(); if (manufacturer.contains("samsung") && model.contains("s7")) { // Galaxy S7 logic: capture params first, then disable AE mode manualRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_OFF); } else { // Pixel 4a and other devices: use AE lock or manual params with CONTROL_MODE_OFF manualRequestBuilder.set(CaptureRequest.CONTROL_AE_LOCK, true); }
Key Parameters You Can Retrieve
Here are the most useful parameters for your use case:
- Exposure time:
CaptureResult.SENSOR_EXPOSURE_TIME(in nanoseconds) - ISO sensitivity:
CaptureResult.SENSOR_SENSITIVITY - Focus distance:
CaptureResult.LENS_FOCUS_DISTANCE(0 = infinity, 1 = closest focus) - AWB gains:
CaptureResult.CONTROL_AWB_GAINS(for manual white balance tuning) - AE stabilization state:
CaptureResult.CONTROL_AE_STATE(check if auto exposure has settled)
Important Notes
- Always retrieve parameters after auto controls stabilize (e.g., wait 5 seconds) to get valid, usable values.
- Some parameters may return
nullon older or less capable devices—always add null checks before using them. - When setting manual parameters, ensure they fall within the device’s supported ranges (you can get these from
CameraCharacteristics, likeSENSOR_INFO_EXPOSURE_TIME_RANGE).
内容的提问来源于stack exchange,提问作者Sergei Belozerov

