where to put autofocus in the class android camera

2020-07-17 05:36发布

问题:

I am trying to use autofoucs i have no idea where to put autofocus anyone has an idea how to implement autofocus?

----------preview class----------

 package com.marakana;

 import java.io.IOException;

 import android.content.Context;
 import android.hardware.Camera;
 import android.hardware.Camera.PreviewCallback;
 import android.util.Log;
 import android.view.SurfaceHolder;
 import android.view.SurfaceView;

class Preview extends SurfaceView implements SurfaceHolder.Callback { // <1>
private static final String TAG = "Preview";

SurfaceHolder mHolder;  // <2>
public Camera camera; // <3>

Preview(Context context) {
super(context);

// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();  // <4>
mHolder.addCallback(this);  // <5>
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); // <6>
}

 // Called once the holder is ready
public void surfaceCreated(SurfaceHolder holder) {  // <7>
// The Surface has been created, acquire the camera and tell it where
// to draw.
camera = Camera.open(); // <8>
try {
  camera.setPreviewDisplay(holder);  // <9>

  camera.setPreviewCallback(new PreviewCallback() { // <10>
    // Called for each frame previewed
    public void onPreviewFrame(byte[] data, Camera camera) {  // <11>
      Log.d(TAG, "onPreviewFrame called at: " + System.currentTimeMillis());
      Preview.this.invalidate();  // <12>
     }
   });
 } catch (IOException e) { // <13>
  e.printStackTrace();
   }
  }

 // Called when the holder is destroyed
public void surfaceDestroyed(SurfaceHolder holder) {  // <14>
camera.stopPreview();
camera = null;
}

// Called when holder has changed
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { // <15>
  camera.startPreview();
 }

}

-----------------camera activity------------------------

preview = new Preview(this); // <3>
((FrameLayout) findViewById(R.id.preview)).addView(preview); // <4>

buttonClick = (Button) findViewById(R.id.buttonClick);
buttonClick.setOnClickListener(new OnClickListener() {
  public void onClick(View v) { // <5>

      preview.camera.takePicture(shutterCallback, rawCallback, jpegCallback);
  }
});

// Handles data for jpeg picture
PictureCallback jpegCallback = new PictureCallback() { // <8>
public void onPictureTaken(byte[] data, Camera camera) {
  FileOutputStream outStream = null;
  try {
    // Write to SD Card

    outStream = new FileOutputStream(imgPath); // <9>
    outStream.write(data);
    outStream.close();
    intImg = new Intent(CameraActivity.this, DisplayImg.class);
    intImg.putExtra("imgPath",imgPath);
    startActivity(intImg);
    Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length);
  } catch (FileNotFoundException e) { // <10>
    e.printStackTrace();
  } catch (IOException e) {
    e.printStackTrace();
  } finally {
  }
  Log.d(TAG, "onPictureTaken - jpeg");
}
};

回答1:

If you have autofocus set, you should do something like this:

camera.autoFocus(new AutoFocusCallback() {
        @Override
        public void onAutoFocus(boolean success, Camera camera) {
            if(success){
                camera.takePicture(shutterCallback, rawCallback, jpegCallback);
            }
        }
    });

That's the way the camera waits for the autofocus ;)



回答2:

Since API level 9 you can implement auto focus in this way (automatically):

In the surfaceChanged() method, please make the following changes:

    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_CONTINUOUS_PICTURE);
        camera.setParameters(parameters);

camera.startPreview();

camera.autoFocus(null);
}


回答3:

camera.autoFocus (new Camera.AutoFocusCallback {});

http://developer.android.com/reference/android/hardware/Camera.html#autoFocus%28android.hardware.Camera.AutoFocusCallback%29



回答4:

You can use it just before you take a picture.

preview.camera.autoFocus(null) // you can make optional callback once focus is achieved
preview.camera.takePicture(shutterCallback, rawCallback, jpegCallback);

In surface changed and before preview, you can use of 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);
}