I\'m working on an app which has a custom camera screen, for which I\'m supposed to implement tap to focus, like in the Android(more specifically, the Galaxy S4) camera app.
Bumped into this issue recently. As MatheusJardimB said, this question helps a lot.
However, in my case, I wanted to start in the ContinuousPicture
mode then be able to tap to focus and then continue with the ContinuousPicture
mode.
I managed to get it to work by using the onAutoFocus
method of the Camera.AutoFocusCallback()
. I'm not sure if it's the best or the prettiest way of doing it, but it seems to work.
Here's the code:
setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (mCamera != null) {
Camera camera = mCamera.getCamera();
camera.cancelAutoFocus();
Rect focusRect = calculateTapArea(event.getX(), event.getY(), 1f);
Parameters parameters = camera.getParameters();
parameters.setFocusMode(Parameters.FOCUS_MODE_MACRO);
if (parameters.getMaxNumFocusAreas() > 0) {
List mylist = new ArrayList();
mylist.add(new Camera.Area(focusRect, 1000));
parameters.setFocusAreas(mylist);
}
camera.setParameters(parameters);
camera.autoFocus(new Camera.AutoFocusCallback() {
@Override
public void onAutoFocus(boolean success, Camera camera) {
camera.cancelAutoFocus();
Parameters params = camera.getParameters();
if (!params.getFocusMode().equals(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) {
params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
camera.setParameters(params);
}
}
});
}
return true;
}
return false;
});
You could just change the focus area to
ArrayList focusAreas = new ArrayList(1);
focusAreas.add(new Area(new Rect(-1000, -1000, 1000, 0), 750));
and it should work.
UPDATE
I recently acquired a Samsung S5 and tested this out on it. It didn't work that well, so I added a few modifications and it's working now. This was also successfully tested on the Galaxy S6 and Galaxy Note4.
Here's the modified code:
setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (mCamera != null) {
Camera camera = mCamera.getCamera();
camera.cancelAutoFocus();
Rect focusRect = calculateTapArea(event.getX(), event.getY(), 1f);
Parameters parameters = camera.getParameters();
if (parameters.getFocusMode().equals(
Camera.Parameters.FOCUS_MODE_AUTO) {
parameters.setFocusMode(Parameters.FOCUS_MODE_AUTO);
}
if (parameters.getMaxNumFocusAreas() > 0) {
List mylist = new ArrayList();
mylist.add(new Camera.Area(focusRect, 1000));
parameters.setFocusAreas(mylist);
}
try {
camera.cancelAutoFocus();
camera.setParameters(parameters);
camera.startPreview();
camera.autoFocus(new Camera.AutoFocusCallback() {
@Override
public void onAutoFocus(boolean success, Camera camera) {
if (!camera.getParameters().getFocusMode().equals(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) {
Parameters parameters = camera.getParameters();
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
if (parameters.getMaxNumFocusAreas() > 0) {
parameters.setFocusAreas(null);
}
camera.setParameters(parameters);
camera.startPreview();
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
return true;
}
});