2

I want to get the distance from an object to android device using android camera2 API. In CaptureCallback, I get the value by result.get(CaptureResult.LENS_FOCUS_DISTANCE) and check LENS_STATE, CONTROL_AF_MODE, CONTROL_AF_STATE at the same time. They are "STATIONARY", CONTINUOUS_PICTURE and PASSIVE_FOCUSED. I think the focus distance should be valid and the distance from an object to android device should be 1/focus_distance. But in fact the calculated value is totally different from the actual distance. What's wrong with it? Is LENS_FOCUS_DISTANCE the correct value for distance estimation? Anyone have idea?

final CameraCaptureSession.CaptureCallback captureCallbackListener = new CameraCaptureSession.CaptureCallback() {
    @Override
    public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) {
        super.onCaptureCompleted(session, request, result);
        Log.e(TAG, String.format("captureCallbackListener %s-%f", lensStateToString(result.get(CaptureResult.LENS_STATE)), result.get(CaptureResult.LENS_FOCUS_DISTANCE) ));
        Log.e(TAG, String.format("AF mode %s-%s", ctlAfModeToString(result.get(CaptureResult.CONTROL_AF_MODE)), ctlAfStateToString(result.get(CaptureResult.CONTROL_AF_STATE)) ));
    }
};
Usman lqbal
  • 935
  • 1
  • 9
  • 31
Dong Debin
  • 21
  • 3
  • Possible duplicate of [Is it possible to measure distance to object with camera?](http://stackoverflow.com/questions/4588485/is-it-possible-to-measure-distance-to-object-with-camera) – SaravInfern Nov 23 '16 at 13:16
  • To avoid down vote, try to search your queries first and then post the question – Myth Nov 23 '16 at 13:16

1 Answers1

4

Please check the value of CameraCharacteristics.LENS_INFO_FOCUS_DISTANCE_CALIBRATION. If it's UNCALIBRATED or APPROXIMATE, then the value of LENS_FOCUS_DISTANCE will likely not be close to reality.

And even with CALIBRATED, the farther objects are, the more error there will be - to the camera, everything past the hyperfocal distance (a few meters at best) starts being impossible to accurately measure distance to.

Eddy Talvala
  • 17,243
  • 2
  • 42
  • 47