最近陷入这个问题。正如MatheusJardimB所说,这个问题
stackoverflow.com/questions/18460647有很多帮助。
然而,在我的情况下,我想要在ContinuousPicture模式中开始,然后可以点击聚焦,然后继续使用ContinuousPicture模式。
我通过使用Camera.AutoFocusCallback()的onAutoFocus方法设法使其工作。我不知道这是否是最好的或最漂亮的做法,但它似乎工作。
以下是代码:
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() != Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE){
params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
camera.setParameters(params);
}
}
});
}
return true;
}
});
您可以将焦点区域更改为
ArrayList focusAreas = new ArrayList(1);
focusAreas.add(new Area(new Rect(-1000, -1000, 1000, 0), 750));
它应该工作。
希望它有帮助。
UPDATE
我最近收购了一款三星S5,并对其进行了测试。它没有那么好,所以我补充了一些修改,现在正在工作:
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() != 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() != 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;
}
});
更新2
在Galaxy S6和Galaxy Note4上测试,它的效果很好。
干杯!