通过触摸Android设置手动对焦

时间:2012-10-12 01:51:44

标签: android android-camera

我打算通过打开相机来设置我的Android应用程序,并通过触摸相机中的点来启用手动对焦。相机可以重新聚焦到我指向屏幕的位置。您能告诉我方法或应该从哪个组件开始修改吗?

以下是我的代码:

public void takePhoto(File photoFile, String workerName, int width, int height, int quality) {
    if (getAutoFocusStatus()){
        camera.autoFocus(new AutoFocusCallback() {
            @Override
            public void onAutoFocus(boolean success, Camera camera) {
                camera.takePicture(shutterCallback, rawCallback, jpegCallback);
            }
        }); 
    }else{
        camera.takePicture(shutterCallback, rawCallback, jpegCallback);
    }

this.photoFile = photoFile;
this.workerName = workerName;
this.imageOutputWidth = width;
this.imageOutputHeight = height;
}

public void takePhoto(File photoFile, int width, int height, int quality) {
takePhoto(photoFile, null, width, height, quality);
}

3 个答案:

答案 0 :(得分:13)

虽然这个答案没有说明如何专注于单个区域,但它绝对有助于展示如何精确地开始对焦相机。

这就是我所做的。这适用于我的设备(HTC的Droid DNA),内置于Android Studio

OnSurfaceChanged()OnSurfaceCreated()中,我都有以下代码: (mCamera是我的私人Camera对象)

        mCamera.stopPreview();
        Camera.Parameters p = mCamera.getParameters();
        p.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);

        mCamera.setParameters(p);
        mCamera.setPreviewDisplay(surfaceHolder);
        mCamera.startPreview();
        mCamera.autoFocus(null);

在构造函数中,您必须放置

        setFocusable(true);
        setFocusableInTouchMode(true);

这将允许您接收焦点事件。至于捕获它们......

 public boolean onTouchEvent(MotionEvent event){
    if(event.getAction() == MotionEvent.ACTION_DOWN){
        Log.d("down", "focusing now");

        mCamera.autoFocus(null); 
    }

    return true;
}

答案 1 :(得分:1)

您是否尝试过使用setFocusAreas()来设置用户触摸的焦点区域?

答案 2 :(得分:0)

  

这里的关键是调用camera.autoFocus(autoFocusCallBack)和之后   我们专注于方法autofocusCallback.onAutoFocus(布尔成功,   相机摄像头)被调用 - 调用camera.takePicture   (Camera.ShutterCallback快门,Camera.PictureCallback raw,   Camera.PictureCallback jpeg)

查看更多:http://achorniy.wordpress.com/2009/12/29/how-to-use-autofocus-in-android/

  

在曲面更改和预览之前,您可以使用自动对焦   设置将自动设置焦距,然后开始   预览,然后自动对焦显示的任何内容......

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {

// Set camera properties first
Camera.Parameters parameters = camera.getParameters();
        parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
        camera.setParameters(parameters);

camera.startPreview();

camera.autoFocus(null);

}

信用:where to put autofocus in the class android camera