Android Camera2 API设置LENS_FOCUS_DISTANCE手动聚焦异常求助
Hey there, let's troubleshoot this manual focus discrepancy you're hitting on the Pixel 2 with the Camera2 API. It's frustrating when your calculated focus distance doesn't match what the camera actually locks onto, so let's break down possible causes and fixes:
Possible Causes & Fixes
1. Double-check your diopter calculation (and avoid integer division!)
First, confirm your formula uses floating-point arithmetic correctly. The Camera2 API expects LENS_FOCUS_DISTANCE in diopters, which equals 1 / focus_distance_in_meters. For 45cm, that's 1 / 0.45 ≈ 2.222f.
Make sure you're not accidentally doing integer division (e.g., 100 / 45 instead of 100.0f / 45) — that would give you an integer result (2) instead of the correct float (2.222). While this doesn't exactly explain your 35cm focus issue, it's a critical first check to rule out basic calculation errors.
2. Disable all automatic control modes to prevent interference
Your current code turns off AF mode, but other automatic controls might still be overriding your manual focus setting. Try explicitly locking down the control mode to ensure no auto-adjustments are happening:
final CaptureRequest.Builder captureBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE); // Disable all automatic system control captureBuilder.set(CaptureRequest.CONTROL_MODE, CaptureRequest.CONTROL_MODE_OFF); // Turn off auto-focus explicitly captureBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_OFF); // Apply your calculated focus distance captureBuilder.set(CaptureRequest.LENS_FOCUS_DISTANCE, mLensFocusDistance);
Some devices prioritize automatic control logic even if AF is disabled, so setting CONTROL_MODE to off helps enforce your manual parameters.
3. Rule out optical stabilization interference
Optical Image Stabilization (OIS) can sometimes interact with manual focus adjustments on devices like the Pixel 2. Try disabling OIS temporarily to see if it resolves the mismatch:
captureBuilder.set(CaptureRequest.LENS_OPTICAL_STABILIZATION_MODE, CaptureRequest.LENS_OPTICAL_STABILIZATION_MODE_OFF);
4. Verify your request submission flow
Ensure you're properly submitting the modified capture request to your CaptureSession. For still captures, use the capture() method and wait for the CaptureCallback to confirm the request was processed. If you're relying on a repeating preview request, make sure you update that too — the still capture template might inherit settings from the preview if you don't explicitly override them.
5. Account for device-specific lens calibration
Unfortunately, not all camera hardware maps diopter values to physical focus distances perfectly. The Pixel 2's lens might have a slight calibration offset or non-linear focus response. To work around this, create a device-specific calibration table:
- Test a range of input diopter values (e.g., 10cm to 100cm)
- Record which physical distance actually comes into focus for each value
- Use this mapping to adjust your input distance before converting to diopters for the Pixel 2
Final Notes
If none of the above fixes work, it might be a quirk of the Pixel 2's Camera2 implementation. You can also check if the issue persists in preview mode (using TEMPLATE_PREVIEW instead of TEMPLATE_STILL_CAPTURE) to narrow down whether it's specific to still capture processing.
内容的提问来源于stack exchange,提问作者Vivek Gabadiya

