3

I want find the distance from an object to my android device.

I want to use camera or sensors but I don't know how to use them.

Thanks for help.

Hear is my code but it does not give correct values

  ..........
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

    }
saritha
  • 89
  • 10

2 Answers2

0

If you know the actual size of the object, camera can provide you an estimate of the distance.

I don't think other sensors would tell you anything about distance.

Using camera, you'd need to do some image processing, and find out area or other size parameters. Knowing actual and perceived size, you can calculate a distance (approximate but good enough) if the object is easy to detect for the camera.

You can try out JavaCV if you want to go that way.

Hope this helps.
Good luck.

Tanmay Patil
  • 6,882
  • 2
  • 25
  • 45
0

Is it possible to measure distance to object with camera this question has a good discussion on using camera for distance (it is not easy or accurate for most cases)

Practically, distance can be reliably measured with much better accuracy by using either IR (Usually does not work with black objects) or Ultrasonic sensors.

In both cases you will need to hook your android to a microcontroller to control and read the values from these external sensors.

If you wish to take this root, you can consider attaching Arduino or IOIO to your phone with the appropriate sensor to measure the distance.

Community
  • 1
  • 1
iTech
  • 18,192
  • 4
  • 57
  • 80