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.
I've tried using the steps outlined here, but it doesn't seem to cause any noticeable focusing. The Focus Mode is set to Continuous Picture(we are supporting only a specific device).
When the user taps on the camera preview, I need to be focusing on the top half of the image. For this, I use the code snippet
Parameters parameters = mCamera.getParameters();
if (parameters.getMaxNumFocusAreas() > 0) {
ArrayList<Area> focusAreas = new ArrayList<Camera.Area>(1);
focusAreas.add(new Area(new Rect(-1000, -1000, 1000, 0), 750));
parameters.setFocusAreas(focusAreas);
mCamera.setParameters(parameters);
}
I do NOT want AutoFocus as it takes too long to focus on the image. I am interested only in the top half of the image. Has anybody successfully implemented Tap to Focus along with Continuous Picture mode?
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<Area> mylist = new ArrayList<Area>();
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<Area> focusAreas = new ArrayList<Camera.Area>(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<Area> mylist = new ArrayList<Area>();
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;
}
});