Android相机实时自动对焦的完美实现
github,由于android碎片化严重,而且各大厂商极有可能去修改相关API的实现,其中遇到了不少坑,包括实时相机高斯模糊,自动对焦的兼容问题,以及一系列性能问题。换过很多搜索引擎,访问过很多网站,访问过很多网站,拜读过很多代码,没有发现对于相机实时自动对焦特别完美的实现方式。现对相机的自动对焦问题单独做一个记录,算是对这部分的一个总结。也希望后人在这部分能够快速地解决问题,不必浪费过多的时间。测试手机包括:MX4 pro,小米4,华为荣耀3C等等。
参考过@yanzi1225627 大神的一些做法,测试结果不是特别满意。
一,一些对焦方案的尝试
1,autoFocus()的尝试:
private Camera.AutoFocusCallback mAutoFocusCallback;
mAutoFocusCallback = new Camera.AutoFocusCallback() {
public void onAutoFocus(boolean success, Camera camera) {
// TODO Auto-generated method stub
if(success){
myCamera.setOneShotPreviewCallback(null);
Toast.makeText(TestPhotoActivity.this,
"自动聚焦成功" , Toast.LENGTH_SHORT).show();
}
}
};
myCamera.autoFocus(mAutoFocusCallback);
在一部分手机上,始终只对焦一次,也就是说根本不能实现。即使是在按下拍照的时候去调用一次对焦,等对焦成功后再进行拍照,实现的效果也不是很完美。
还见部分博客把autoFocus()方法放在Camera预览SurfaceView的surfaceChanged()中的一些实现,发现也只对焦了一次。
2,设置对焦模式FOCUS_MODE_CONTINUOUS_PICTURE
/**
* Continuous auto focus mode intended for taking pictures. The camera
* continuously tries to focus. The speed of focus change is more
* aggressive than {@link #FOCUS_MODE_CONTINUOUS_VIDEO}. Auto focus
* starts when the parameter is set.
*
*
Applications can call {@link #autoFocus(AutoFocusCallback)} in
* this mode. If the autofocus is in the middle of scanning, the focus
* callback will return when it completes. If the autofocus is not
* scanning, the focus callback will immediately return with a boolean
* that indicates whether the focus is sharp or not. The apps can then
* decide if they want to take a picture immediately or to change the
* focus mode to auto, and run a full autofocus cycle. The focus
* position is locked after autoFocus call. If applications want to
* resume the continuous focus, cancelAutoFocus must be called.
* Restarting the preview will not resume the continuous autofocus. To
* stop continuous focus, applications should change the focus mode to
* other modes.
*
* @see #FOCUS_MODE_CONTINUOUS_VIDEO
<