这是很早之前我发现的一个问题,当时想利用“焦距”来定位被拍摄物体和手机摄像头之间的距离,注意不是摄像头预览画面里两个点之间的距离,这是个平面距离。本文所说的距离是垂直距离。最终失败,原因是parameters.getFocusDistances返回的值永远是inifinte。手机型号是华为U9200.
下面是测试核心代码:
@Override
public void onAutoFocus(boolean success, Camera camera) {
//READ FOCUS DISTANCES HERE
Camera.Parameters parameters = camera.getParameters();
float[] distances = new float[3];
if (success) {
// Only available with Android 9 (2.3)
// Focus Mode is always reported as auto but
// distances do not appear to be updating
// always: 0.1, 1.2, Infinity, (on my device it's 0,0,0)
Log.d("Focus Mode: ", parameters.getFocusMode());
parameters.getFocusDistances(distances);
Log.d("focus distance near", Float.toString(distances[0]));
Log.d("focus distance optimum", Float.toString(distances[1]));
Log.d("focus distance far", Float.toString(distances[2]));
}
}
或者在surfaceCreate函数里测试自动聚焦前的参数,以此对比上者:
@Override
public void surfaceCreated(SurfaceHolder holder) {
Log.e(getClass().getSimpleName(), "surfaceCreated");
camera = Camera.open();
Camera.Parameters parameters = camera.getParameters();
float[] distances = new float[3];
Log.d("Focus Mode: ", parameters.getFocusMode());
parameters.getFocusDistances(distances);
Log.d("focus distance 0", Float.toString(distances[0]));
Log.d("focus distance 1", Float.toString(distances[1]));
Log.d("focus distance 2", Float.toString(distances[2]));
}
但奇怪的是得到的参数一直是infinite!!!国外的http://code.google.com/p/android/issues/detail?id=14341也发现了同样的bug。
后查资料,早期的手机带有红外,通过发射红外可以测出来被拍摄物体和手机之间的距离。以前的相机的聚焦算法用的是物理算法也就是指的这。现代的大多通过软件聚焦,比如计算出人脸核心区域以此聚焦。但这些算法都是相机厂家的机密哦!!!但android手机里既然提供这个api理论上是可以获得距离的,只能说是个bug了!
参考:http://stackoverflow.com/questions/6401370/camera-focus-distances
-------------------------------------转载请注明作者yanzi1225627