如何自动自动对焦Android相机?

一旦相机静止不动,我想自动对焦安卓相机。 我正在寻找教程或样本如何做到这一点或至less小样本,显示什么类我可以用来钩这样的事件。

对我来说,这工作得很好:

//set camera to continually auto-focus Camera.Parameters params = c.getParameters(); //*EDIT*//params.setFocusMode("continuous-picture"); //It is better to use defined constraints as opposed to String, thanks to AbdelHady params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); c.setParameters(params); 

尝试使用Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEOCamera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE 。 见下文:

 Camera.Parameters params = camera.getParameters(); if (params.getSupportedFocusModes().contains( Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)) { params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO); } camera.setParameters(params); 

在尝试使用它之前testing手机是否支持您select的模式非常重要,否则setParameters()将引发运行时exception。 (编辑代码现在正常工作)

以下代码适用于我。

设置自动对焦(预览课程):

 Parameters params = camera.getParameters(); params.setFocusMode(Parameters.FOCUS_MODE_AUTO); //some more settings camera.setParameters(params); 

在自动对焦准备好的情况下调用相机拍摄照片(活动等级):

 public void butClick(View v){ preview.camera.autoFocus(new AutoFocusCallback() { public void onAutoFocus(boolean success, Camera camera) { if(success){ camera.takePicture(shutterCallback, rawCallback, jpegCallback); } } }); } 

获取图片(活动课):

 PictureCallback jpegCallback = new PictureCallback() { public void onPictureTaken(byte[] data, Camera camera) { //do something } }; 

看起来你应该像这里讨论的那样连续自动对焦 。

这里有一个问题 ,你可以参考。

你可以试试代码:

 ShutterCallback _pfnShutterCallback = new ShutterCallback() { @Override public void onShutter() { // TODO Auto-generated method stub } }; PictureCallback _pfnRawPictureCallback = new PictureCallback() { @Override public void onPictureTaken(byte[] data, Camera camera) { // TODO Auto-generated method stub } }; private AutoFocusCallback _pfnAutoFocusCallback = new AutoFocusCallback() { @Override public void onAutoFocus(boolean success, Camera camera) { // TODO Auto-generated method stub camera.autoFocus(null); camera.takePicture(_pfnShutterCallback, _pfnRawPictureCallback, mPicture); } }; private PictureCallback mPicture = new PictureCallback() { public void onPictureTaken(byte[] data, Camera camera) { new SavePhotoTask().execute(data); camera.startPreview(); } }; class SavePhotoTask extends AsyncTask<byte[], String, String> { // Process save file image } 

调用capture: mCamera.autoFocus(autoFocusCallback);

你可以参考: http : //android-er.blogspot.com/2011/01/start-camera-auto-focusing-autofocus.html

这对预览callback完美的作品:

 Camera.Parameters parameters = camera.getParameters(); if (parameters.getSupportedFocusModes().contains( Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)) { parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO); } camera.setParameters(parameters);