我想在相机保持不动时自动对焦Android相机。我正在寻找教程或示例如何做到这一点或至少是小样本,显示我可以用来挂钩此类事件的类。
答案 0 :(得分:115)
对我而言,这是一种享受:
//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);
答案 1 :(得分:58)
尝试使用Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO
或Camera.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);
在尝试使用手机之前测试手机是否支持您选择的模式非常重要,否则setParameters()
会抛出运行时异常。
(编辑代码现在正常工作)
答案 2 :(得分:18)
以下代码适用于我。
设置自动对焦(预览类):
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
}
};
答案 3 :(得分:7)
看起来你应该continuous autofocus,如下所述。
您可以参考question here。
答案 4 :(得分:1)
这适用于预览回调:
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);
答案 5 :(得分:0)
你可以试试代码:
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
}
呼叫捕获:mCamera.autoFocus(autoFocusCallback);
您可以参考: http://android-er.blogspot.com/2011/01/start-camera-auto-focusing-autofocus.html