Jeff Bootsholz
Jeff Bootsholz

Reputation: 3068

Android setting manual focus by touching

I am going to set the my Android App by opening the camera and enable manual focus by touching the point in to camera. The camera can refocus to the point where I have pointed on to the screen. Would you please tell me the methodology or which component should I start with to modify ?

Below is my code:

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

Upvotes: 9

Views: 23433

Answers (3)

Nam Vu
Nam Vu

Reputation: 5725

The key here is to call camera.autoFocus(autoFocusCallBack) and after we focused method autofocusCallback.onAutoFocus(boolean success, Camera camera) is called – call the camera.takePicture (Camera.ShutterCallback shutter, Camera.PictureCallback raw, Camera.PictureCallback jpeg)

See more: http://achorniy.wordpress.com/2009/12/29/how-to-use-autofocus-in-android/

or

In surface changed and before preview, you can use the auto-focus setting which will set the focal distance automatically, then start the preview, and then auto-focus on whatever is displayed...

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

}

credit: where to put autofocus in the class android camera

Upvotes: 0

Nlinscott
Nlinscott

Reputation: 796

Though this answer does not show how to focus in on a single area, it is definitely useful in showing how exactly to focus the camera to begin with.

Here is what i have done. This works on my device (Droid DNA by HTC), built in Android Studio

In both OnSurfaceChanged() and OnSurfaceCreated(), I have the following code: (mCamera is my private Camera object)

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

In the constructor, you must place

        setFocusable(true);
        setFocusableInTouchMode(true);

This will allow you to receive focus events. As for capturing them...

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

        mCamera.autoFocus(null); 
    }

    return true;
}

Upvotes: 14

mchouhan
mchouhan

Reputation: 1873

have you tried using setFocusAreas() to set the focusarea where user has touched ?

Upvotes: 1

Related Questions