I want to autofocus Android camera as soon as camera holds still. Im looking for tutorials or samples how to do it or at least small sample that shows what classes I can use to hook on such events.
Open the Camera app and tap the Settings icon. Tap the switch next to Tracking auto-focus to turn it off.
Autofocus (AF) on smartphones is the process of determin- ing how to move a camera's lens such that certain scene content is in focus. The underlying algorithms used by AF systems, such as contrast detection and phase differencing, are well established.
For me this worked a treat:
//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);
Try to use Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO
or Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE
.
See below:
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);
It's important to test whether the phone is supporting your chosen mode before attempting to use it, otherwise setParameters()
will throw a runtime exception.
(Edit code now working properly)
Following code works for me.
Setting autofocus (preview class):
Parameters params = camera.getParameters();
params.setFocusMode(Parameters.FOCUS_MODE_AUTO);
//some more settings
camera.setParameters(params);
Call camera for shot a picture in case that autofocus is ready (activity class):
public void butClick(View v){
preview.camera.autoFocus(new AutoFocusCallback() {
public void onAutoFocus(boolean success, Camera camera) {
if(success){
camera.takePicture(shutterCallback, rawCallback, jpegCallback);
}
}
});
}
Get picture (activity class):
PictureCallback jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
//do something
}
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With