Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Camera AutoFocus on Demand

The built in Camcorder App (like the one on the HTC EVO) seems to call camera.autoFocus() only when the preview image changes. If you hold the camera steady no camera.autoFocus() happens.
I would like to duplicate this behavior while camera.startPreview() is active as in the initial preview setup code below:

camera = camera.open();
Camera.Parameters parameters = camera.getParameters();
List<String> focusModes = parameters.getSupportedFocusModes();
if (focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO))
{
    parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
}
camera.setParameters(parameters);
camera.setPreviewDisplay(holder);
camera.startPreview();

All the examples I found for autoFocus() seem to be calling it every 500ms to 2000ms, or once the instant before the picture is taken or recording is started.
The EVO Camcorder app seems to use a sensor or an algorithm to trigger autoFocus(). However this autoFocus() trigger is done it works exceptionally well. Does anyone have any knowledge of how to trigger autoFocus() on demand when it is needed, such as when the camera is moved close or farther from the subject or is panned slightly? Thank you, Gerry

like image 814
GerryL Avatar asked May 04 '11 03:05

GerryL


People also ask

How do I make my Samsung camera focus?

Open the Camera app, then tap the Settings icon at the top left corner. Step 2. Tap the switch next to Tracking auto-focus to turn it off. If your camera still won't focus, resetting the camera to its default settings may also be a solution.


4 Answers

Android has introduced continuous auto focus since API Level 9 (Gingerbread). It works better than calling Camera.autoFocus periodically.

like image 122
arsalank2 Avatar answered Oct 23 '22 18:10

arsalank2


I had the same problem in one of my applications.

My solution was to use a sensor listener and do auto focus when the user shook the device to some threshold. Here is the code.

public void setCameraFocus(AutoFocusCallback autoFocus){
if (mCamera.getParameters().getFocusMode().equals(mCamera.getParameters().FOCUS_MODE_AUTO) ||
        mCamera.getParameters().getFocusMode().equals(mCamera.getParameters().FOCUS_MODE_MACRO)){
    mCamera.autoFocus(autoFocus);
}

}

The callback for auto focus:

// this is the autofocus call back
private AutoFocusCallback myAutoFocusCallback = new AutoFocusCallback(){

    public void onAutoFocus(boolean autoFocusSuccess, Camera arg1) {
        //Wait.oneSec();
        mAutoFocus = true;
    }};

And the way to call the focus.

public void onSensorChanged(SensorEvent event) {

if (mInvalidate == true){
    mView.invalidate();
    mInvalidate = false;
}
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];
if (!mInitialized){
    mLastX = x;
    mLastY = y;
    mLastZ = z;
    mInitialized = true;
}
float deltaX  = Math.abs(mLastX - x);
float deltaY = Math.abs(mLastY - y);
float deltaZ = Math.abs(mLastZ - z);

if (deltaX > .5 && mAutoFocus){ //AUTOFOCUS (while it is not autofocusing)
    mAutoFocus = false;
    mPreview.setCameraFocus(myAutoFocusCallback);
}
if (deltaY > .5 && mAutoFocus){ //AUTOFOCUS (while it is not autofocusing)
    mAutoFocus = false;
    mPreview.setCameraFocus(myAutoFocusCallback);
}
if (deltaZ > .5 && mAutoFocus){ //AUTOFOCUS (while it is not autofocusing) */
    mAutoFocus = false;
    mPreview.setCameraFocus(myAutoFocusCallback);
}

mLastX = x;
mLastY = y;
mLastZ = z;

}

You can see the complete project here: http://adblogcat.com/a-camera-preview-with-a-bounding-box-like-google-goggles/

like image 43
Juan Acevedo Avatar answered Oct 23 '22 17:10

Juan Acevedo


It is very possible to call a refocus with a simpler technique, if you have a white box flash within the cameras view (from code, not a real box) it will rapidly call a refocus. I own the EVO 4G and one of the previous posters is correct, it does continually refocus without the need to change what it is looking at ever since the update to Gingerbread.

like image 32
Speedytech7 Avatar answered Oct 23 '22 18:10

Speedytech7


For taking pictures, you can set this.

Applications can call autoFocus(AutoFocusCallback) in this mode. If the autofocus is in the middle of scanning, the focus callback will return when it completes. If the autofocus is not scanning, the focus callback will immediately return with a boolean that indicates whether the focus is sharp or not. The apps can then decide if they want to take a picture immediately or to change the focus mode to auto, and run a full autofocus cycle.

like image 22
Santosh Avatar answered Oct 23 '22 17:10

Santosh