Home » Android » Android setting manual focus by touching

Android setting manual focus by touching

Posted by: admin June 15, 2020 Leave a comment


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() {
            public void onAutoFocus(boolean success, Camera camera) {
                camera.takePicture(shutterCallback, rawCallback, jpegCallback);
        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);
How to&Answers:

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)

        Camera.Parameters p = mCamera.getParameters();


In the constructor, you must place


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


    return true;


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


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/


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




credit: where to put autofocus in the class android camera