3

How can i calculate the distance of object from camera in autofocus mode? I have tried using the Camera.Parameters.getFocusDistances(float[]) method, but it always returns the values .15, 1.2, and Infinity on my Galaxy S2. Any help will be appreciated. Here is the code i am using:-

..........
cam=Camera.open();
Camera.Parameters pa=cam.getParameters();
pa.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
cam.setParameters(pa);
cam.startPreview();
cam.autoFocus(this);
..........


@Override
public void onAutoFocus(boolean arg0, Camera arg1) {
    // TODO Auto-generated method stub
        float f[]=new float[3];
        arg1.getParameters().getFocusDistances(f);
        text.setText(""+f[1]);  //Always returns 1.2
        t2.setText(""+f[0]);    //Always returns .15
        t3.setText(""+f[2]);    //Always returns Infinity

    }

Is there any other way to accomplish this?

awareeye
  • 3,051
  • 3
  • 22
  • 22
  • I have the same problem. Have you found a solution? – shizik Oct 29 '11 at 15:54
  • With the small sensors on phones, depth of field is almost infinite so focusing is not needed. Possibly this mean that the lens has a fixed focus of 1.2 meters with a depth of field where everything from .15 meters to infinity is always in focus. – Tom Kincaid Apr 29 '14 at 01:11

2 Answers2

1

Maybe there is a solution by using the formula 1/f=1/v+1/u, in which f is the focal length, v is the distance of CCD/CMOS from the lens, u is the distance of object from camera. The problem is, can we get the value of v ?

Wocaozhe
  • 11
  • 2
1

Nope :( Check out this other stackoverflow answer. "Nope. The camera can only give you image data and an image alone doesn't give you enough information to give you depth information. If you had multiple images that you had location information for or even video you could then process it to triangulate the distance, but a single image alone would not be enough to give you a distance"

Community
  • 1
  • 1
Captain Charmi
  • 583
  • 8
  • 20