1

When trying out the Camera2Basic sample app from Google, I want the focus distance value after auto focus mode is locked. When locked, the code calls CaptureStillPicture() function. I need to get the LENS_FOCUS_DISTANCE value, but it always returns null. Here's the CaptureStillPicture() function:

        private void CaptureStillPicture(){
        try {
            final Activity activity = getActivity();
            if (null == activity || null == mCameraDevice) {
                return;
            }

            final CaptureRequest.Builder captureBuilder =
                    mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
            captureBuilder.addTarget(mImageReader.getSurface());


            captureBuilder.set(CaptureRequest.CONTROL_AF_MODE,
                    CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
            setAutoFlash(captureBuilder);


            int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
            captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, getOrientation(rotation));

            CameraCaptureSession.CaptureCallback CaptureCallback
                    = new CameraCaptureSession.CaptureCallback() {
                //Here's where I want the focus distance.
                //TotalCaptureResult class can give the focus distance value
                @Override
                public void onCaptureCompleted(@NonNull CameraCaptureSession session,
                                               @NonNull CaptureRequest request,
                                               @NonNull TotalCaptureResult result) {
                    showToast("Saved: " + mFile);
                    //This value, while debugging shows null :
                    Float focus_distance = result.get(result.LENS_FOCUS_DISTANCE);
                    Log.d(TAG, mFile.toString());
                    unlockFocus();
                }
            };

            mCaptureSession.stopRepeating();
            mCaptureSession.abortCaptures();
            mCaptureSession.capture(captureBuilder.build(), CaptureCallback, null);
        } catch (CameraAccessException e) {
            e.printStackTrace();
          }
         }
        }

Here's the piece of code that calls this function:

        private CameraCaptureSession.CaptureCallback mCaptureCallback
            = new CameraCaptureSession.CaptureCallback() {

          private void process(CaptureResult result) {

            if (CaptureResult.CONTROL_AF_STATE_FOCUSED_LOCKED == afState ||
                            CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED == afState) {

                        Integer aeState = result.get(CaptureResult.CONTROL_AE_STATE);
                        if (aeState == null ||
                                aeState == CaptureResult.CONTROL_AE_STATE_CONVERGED) {
                            mState = STATE_PICTURE_TAKEN;
                            captureStillPicture();
                        } else {
                            runPrecaptureSequence();
                        }
                    } }}

What has gone wrong here?

I've tried this but I'm not getting a 0, I'm getting null. I have the same question as this, but there's no answer that I can understand.

Carlos Cavero
  • 3,011
  • 5
  • 21
  • 41
KKM
  • 626
  • 5
  • 12
  • 1
    I am afraid that this answer by Eddy Talvala, *the* developer working on **camera2** API, is the most precise answer you can possibly get. Let me translate it in a less technical way: for many camera devices, there is a technical limitation that makes it impossible to report `LENS_FOCUS_DISTANCE` reliably. Different manufacturers address this limitation at their own discretion. So, you will see devices that return **0**, or **null**, or some other value that cannot be trusted. – Alex Cohn May 03 '19 at 16:15
  • Do you have a link on that comment? I can't see which answer you're talking about. Is there anyway to find out if the device actually supports returning a proper `LENS_FOCUS_DISTANCE `? – KKM May 05 '19 at 10:05
  • 1
    https://stackoverflow.com/a/40776229/192373 – Alex Cohn May 05 '19 at 11:20
  • Can any photogrammetry concepts come into play to get the distance? Perhaps by measuring the accelerometer value or with the field of view? I've seen a bunch of questions on SO but haven't found an answer. What I'm looking for is the real life size of the object, given its pixel size. One formula says that you need to know the distance from the lens and the object. Is there another way to do this? – KKM May 05 '19 at 11:41
  • I am afraid there is no generic way to find the real life size of an object captured by your camera, even if you know the distance from the lens (and even this you cannot derive from LENS_FOCUS_DISTANCE even when this value is available). Some cameras can provide true 3D image of the object, and they may have enough info to provide a reliable estimate of the real life size of the object. – Alex Cohn May 05 '19 at 12:39
  • Is there anyway to find out the focal length when the camera locks its focus? I tried this as well and still got a null value. This is particularly valid for a OnePlus One phone with Android Lollipop OS. – KKM May 06 '19 at 01:41
  • No, there is no binding requirement for a camera to provide this. As for OnePlus One, I believe they have a legacy level of camera2, so you better rely on the old camera API. – Alex Cohn May 06 '19 at 04:29
  • Isn't there any value which the user can access to know what was changed when the camera locked its focus? – KKM May 06 '19 at 07:45
  • generally speaking, no. All you have is the **onCaptureProgressed()** callback that will inform you that `result.get(CaptureResult.CONTROL_AF_STATE)` is `CaptureResult.CONTROL_AF_STATE_FOCUSED_LOCKED` (if you are lucky enough). It may be eye opening to read the instructions for camera manufacturers: https://source.android.com/devices/camera/camera3_3Amodes – Alex Cohn May 06 '19 at 08:50
  • Would manual focus somehow give the estimated distance from the lens to the object? – KKM May 06 '19 at 09:27
  • no, manual focus won't help – Alex Cohn May 06 '19 at 12:52

0 Answers0