如何自动自动对焦摄像头的Android?(How to autofocus Android came

2019-07-23 08:34发布

我想尽快自动对焦摄像头的Android照相机持有不动。 林寻找教程或样本如何做到这一点,或至少小样本,显示我可以用什么类勾上此类事件。

Answer 1:

对于我这种工作一种享受:

//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);


Answer 2:

尝试使用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);

它来测试手机是否试图使用它之前,支持你所选择的模式是很重要的,否则setParameters()将抛出一个运行时异常。 (编辑代码现在能正常使用)



Answer 3:

下面的代码为我工作。

设置自动对焦(预览类):

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
    }
};


Answer 4:

看起来你应该连续自动对焦如这里讨论。

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



Answer 5:

这完全适用预览回调:

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);


Answer 6:

你可以尝试代码:

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



文章来源: How to autofocus Android camera automatically?