Search code examples
androidcameraandroid-cameraandroid-camera2android-camerax

Setting Camera Focus Distance in Android


In manual mode, the camera app on my Xperia XA (Sony F3113) allows me to set the focus distance for the rear camera using a slider. (See screenshot here.)

Is there any way I can see or fiddle with the code the app uses to do this?

Is there any way I can set the focus distance (NOT autofocus) in an app of my own?

Thanks!!!

Note 1: I believe the Camera2 API is not an option on this phone. (Please correct me if I'm wrong.)

Note 2: My question (and ultimate intention) is very similar to this, which has not been satisfactorily answered.


Solution

  • Unless the device supports the camera2 capability MANUAL_SENSOR, there is no standard way to do manual focus. If that capability is supported, then you can just use LENS_FOCUS_DISTANCE to change focus.

    If that's not listed, it's likely the manufacturer added some custom control for focus distance control; if the device is a LEGACY camera, it'll be a custom android.hardware.Camera.Parameters string, very likely. If you knew the control value, you could use Parameters.set() to adjust it.

    You might be able to figure it out for that particular device by running the default app, adjusting manual focus, and while the app is still active, using adb to run adb shell dumpsys media.camera, and looking at the current camera parameter values listed. There may be multiple settings that need to be adjusted (such as AF mode) in addition to the actual focus distance control, note.

    Of course, this will only hold for this particular device; since there are undocumented interfaces, they may change depending on OEM, device, or even on OS update. So depending on them is very fragile.