4

I would like to determine the distance of an object from my Nexus 5 camera, preferably without using an object like a coin for scale. I figured the Camera.Parameters getFocusDistances function would work for this.

I attempted to do this via something like the following in my takePicture() jpeg callback:

Parameters params = camera.getParameters();
Float focusDistances[] = new float [3];
params.getFocusDistances( focusDistances );

I tried running this a few times with objects of different distances from the camera, though each time, focusDistances[FOCUS_DISTANCE_NEAR_INDEX], focusDistances[FOCUS_DISTANCE_OPTIMAL_INDEX], and focusDistances[FOCUS_DISTANCE_FAR_INDEX] all contained the value positive infinity.

It's possible I'm doing something wrong, in which case please let me know if there is a specific way I'm which this will work on the Nexus 5. However the android API specifically states you can call getParameters() (and then getFocusDistances()) at any time to get the latest focus distances and therefore I think this should work. One thing I haven't tried yet is doing the above in an on auto focus handler, however I don't see why this should matter.

I did some research to try and see what was going on, and I found several questions regarding this sort of behavior from getFocusDistances() and typically the answer, if there was one, was that the function is not supported by the android API and/or the hardware manufacturer. Now a lot of these discussions I found online were from several years ago, and dispite the questionable feelings it gives me about getFocusDistances, I've still seen this function suggested to be used for getting the focus distance so I figure it must work on SOME device for SOME android API version.

Does anybody know if getFocusDistances() works for any particular version of android on the Nexus 5? If not, does anybody know ANY device it does work on?

EDIT:

Since posting, I have tried obtaining the focus distances in the onAutoFocus handler, as well as trying a bit more extensively for objects atvarious distances. The results have been consistent - positive infinity is always returned for all 3 focus distances (NEAR, OPTIMAL, and FAR). I even tried this with a Nexus 7 and getFocusDistances always returns the constant values (0.95, 1.9, and infinity), so apparently getFocusDistances isn't implemented on that device either.

Therefore, I really have two questions:

  1. Is there any way to get somewhat accurate focus distances using the android Camera API with the Nexus 5? I'm even wondering if there is custom android version where getFocusDistances is actually implemented, since otherwise I may attempt to do so myself depending on what I find when examining the API code.

  2. Are there any android capable devices that are known to implement getFocusDistances in a somewhat accurate manner?

user1167662
  • 1,245
  • 3
  • 16
  • 25

1 Answers1

2

First of all, It's very difficult to measure the object distance from one single shot/view. You would find many research papers which tried to employ vision based techniques to judge the object distance. I can refer you one such paper. They tried to implement a positioning system that would solely work on mobile camera+sensors. You would probably realize how non-trivial it is to measure the object distance from one single camera view. They finally used a method called "structure from motion" vision technique to calculate the distance (From multiple photos taken from multiple angle).

Even traditional apps like SmartDistance and SmartMeasure needs to use geometric tricks to measure the distance. None of them could only rely on camera parameters. Sorry for the elongated introduction. I have done a project of this sort before and I am telling you all these based on my experience.

To answer your query, I haven't found any Android device yet which returns realistic values of focus distances. They are either returned as some constant values or sometimes 0 and infinity. I found someone reporting that it worked for Galaxy Nexus but only within 30cm object distance, it doesn't work for distances more than that. The bottom line is that you cannot rely on this function from camera API which is heavily dependent on the device drivers. And, phone camera's are not well-known for their lens/sensor qualities. It would be very very difficult for you to work on any optics based formula for mobile-phone cameras. I would suggest you to rather go for some sensor based geometric tricks.

Hungry Coder
  • 1,800
  • 17
  • 22
  • Having a very limited distance is fine for my application. Are the values returned consistently accurate and precise below 30cm? Basically any method of determining the distance of an object (assumed to close to the camera) from the camera that doesn't require me to use something like a coin for scale would be acceptable. Ultimately I just want to be able to make accurate measurements regarding the size of a (very close) object in the image, millimeter precision or better should be acceptable for my application. Do you know of a reliable way to do this? – user1167662 Aug 22 '14 at 16:19
  • I got your point. Coins are used as reference for the pixel-based calculations. For that kind of approach, I don't think it's possible without any reference object. And, it's a great ask to achieve sub-millimeter precision. Even if you get the `getFocusDistances` method to work, it still won't be that much accurate. Quoting from the Android doc: `The precision depends on the camera hardware, autofocus algorithm, the focus area, and the scene. The error can be large and it should be only used as a reference.` – Hungry Coder Aug 22 '14 at 16:40
  • Regarding the android doc, I am aware of the hardware limitation, which is why I am wondering particularly about the Nexus 5. I am imagining getFocusDistances must at least work for some devices, so I am curious if the nexus 5 is one of them, and how accurate it is on that particular device – user1167662 Aug 22 '14 at 17:36
  • I'm searching for this answer cause i'm with a moto g4 plus and its rear camera comes with laser focus. I've already looked at android developer pages for something about, but we still have not builtin for this on android apis. Seeing the posts here, maybe use the getFocusDistances can be more accurate on laser based focus cameras. – Diogo Paschoal Jan 27 '17 at 12:10