我想在相机静止后立即自动对焦 Android 相机。我正在寻找教程或示例如何做到这一点,或者至少是小示例,以显示我可以使用哪些类来挂钩此类事件。
问问题
99183 次
6 回答
123
对我来说,这是一种享受:
//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);
于 2014-04-24T11:25:20.497 回答
63
尝试使用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()
会引发运行时异常。(编辑代码现在可以正常工作)
于 2015-02-08T01:03:47.290 回答
19
以下代码对我有用。
设置自动对焦(预览类):
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
}
};
于 2014-11-30T18:45:14.230 回答
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);
于 2017-07-06T11:26:47.400 回答
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
于 2014-06-02T11:03:57.387 回答