Android相机实时自动对焦的完美实现

时间:2022-03-25 12:21:26
https://zhidao.baidu.com/question/873328177698804372.html

Android相机实时自动对焦的完美实现 http://blog.csdn.net/huweigoodboy/article/details/51378751

如何实现Android手机摄像头的的自动对焦 http://www.open-open.com/lib/view/open1406536473812.html

android 前置摄像头可以autofocus方法:
第一:最常见的实现---利用按键或拍照前一次性自动聚焦在Activity里申明变量private
AutoFocusCallback myAutoFocusCallback = null;然后
自动聚焦变量回调 myAutoFocusCallback = new AutoFocusCallback()
{ public void onAutoFocus(boolean success, Camera
camera) { // TODO Auto-generated method stub if(success)//success表示对焦成功 { Log.i(tag, "myAutoFocusCallback:
success..."); //myCamera.setOneShotPreviewCallback(null); } else { //未对焦成功 Log.i(tag, "myAutoFocusCallback: 失败了..."); } } }; 在Camera.startPreview()之后,拍照Camera.takePicture()
之前调用myCamera.autoFocus(myAutoFocusCallback)。 第二:
通过TimerTask设置一个定时器来循环,每隔一定时间调用myCamera.autoFocus(myAutoFocusCallback)一次。结果是摄像头一直在自我聚焦,重复清晰--模糊----清晰---模糊的过程。 第三:通过Handler和Thread,开启一个线程进行自动聚焦。本质和第二种是一样的。后续我会写一个demo发上来。 第四:
通过监听传感器来触发autoFocus。常用的是角度传感器加速度传感器,当监听的值超过一定阈值时触发。这也是国外程序员们常用的思路。 第五:设置聚焦方式为myParam.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
Continuous auto focus mode intended for video
recording. The camera continuously tries to focus. This is the best
choice for video recording because the focus changes smoothly .
Applications still can call takePicture(Camera.ShutterCallback,
Camera.PictureCallback,
Camera.PictureCallback) in this mode
but the subject may not be in focus. Auto focus starts when the
parameter is set.
Since API level 14, applications can
call autoFocus(AutoFocusCallback) in
this mode. The focus callback will immediately return with a
boolean that indicates whether the focus is sharp or not. 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.
意思是在录像的时候调用这种聚焦方式好,在拍照情况下这种聚焦方式可能不成功。
设置之后myParam.set("rotation",
90);
这句话的设置不起作用。拍摄得到的照片和预览的方向是错90度的。需要得到Bitmap之后,利用Matrix自己旋转下。
旋转示例:mBitmap = BitmapFactory.decodeByteArray(data, 0,
data.length);//data是字节数据,将其解析成位图myCamera.stopPreview();isPreview = false;
设置FOCUS_MODE_CONTINUOUS_VIDEO)之后,myParam.set("rotation",
90)失效。图片竟然不能旋转了,故这里要旋转下
Matrix matrix = new Matrix();
matrix.postRotate((float)90.0);
Bitmap rotaBitmap = Bitmap.createBitmap(mBitmap,
0, 0, mBitmap.getWidth(), mBitmap.getHeight(), matrix,
false);
设置成FOCUS_MODE_CONTINUOUS_VIDEO之后,再调用Camera.autoFocus()会聚焦失败。 第六: 就是通过分析onPreviewFrame得到预览帧视频,分析亮度变化,来触Camera.autoFocus()。如果亮度变化,肯定是摄像头和被拍摄物体之间发生了相对运动,需要触发autoFocus()。某些手机里自带的
拍照软件可能就是采用这种方式。