How to autofocus Android camera automatically?
AndroidAndroid CameraAndroid Problem Overview
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.
Android Solutions
Solution 1 - Android
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);
Solution 2 - Android
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)
Solution 3 - Android
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
}
};
Solution 4 - Android
Looks like you should continuous autofocus as is discussed here.
There is a question here that you can reference.
Solution 5 - Android
This works perfectly for preview callback:
Camera.Parameters parameters = camera.getParameters();
if (parameters.getSupportedFocusModes().contains(
Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)) {
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
}
camera.setParameters(parameters);
Solution 6 - Android
you can try code:
ShutterCallback _pfnShutterCallback = new ShutterCallback() {
@Override
public void onShutter() {
// TODO Auto-generated method stub
}
};
PictureCallback _pfnRawPictureCallback = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
// TODO Auto-generated method stub
}
};
private AutoFocusCallback _pfnAutoFocusCallback = new AutoFocusCallback() {
@Override
public void onAutoFocus(boolean success, Camera camera) {
// TODO Auto-generated method stub
camera.autoFocus(null);
camera.takePicture(_pfnShutterCallback, _pfnRawPictureCallback,
mPicture);
}
};
private PictureCallback mPicture = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
new SavePhotoTask().execute(data);
camera.startPreview();
}
};
class SavePhotoTask extends AsyncTask<byte[], String, String> {
// Process save file image
}
call capture : mCamera.autoFocus(autoFocusCallback);
You can refer at: http://android-er.blogspot.com/2011/01/start-camera-auto-focusing-autofocus.html