如何获取Android相机的物体距离?
在自动对焦模式下如何计算物体与相机的距离?我尝试使用 Camera.Parameters.getFocusDistances(float[]) 方法,但在 Galaxy S2 上它始终返回值 0.15、1.2 和 Infinity。任何帮助将不胜感激。 这是我正在使用的代码:-
..........
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
}
还有其他方法可以实现此目的吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不:( 查看其他 stackoverflow 答案。“不。相机只能为您提供图像数据,而单独的图像并不能为您提供足够的信息来提供深度信息。如果您有多个具有位置信息的图像甚至视频,您可以将其处理为三角测量距离,但仅凭一张图像不足以给你一个距离”
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"
也许可以使用公式 1/f=1/v+1/u 来解决,其中 f 是焦距,v 是 CCD/CMOS 到镜头的距离,u 是物体到相机的距离。问题是,我们能得到v的值吗?
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 ?