自動對焦
1、通過Camera的AutoFocusCallback
camera.cancelAutoFocus(); //每次對焦前取消自動對焦
final String currentFocusMode = params.getFocusMode();
params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
camera.setParameters(params);
camera.autoFocus(new Camera.AutoFocusCallback() {
@Override
public void onAutoFocus(boolean success, Camera camera) {
if (success) {
focusImageView.onFocusSuccess(); //對焦完成可以進行一些操作,比如對焦框顯示對焦成功的動畫
} else {
Camera.Parameters params = camera.getParameters(); //有時對焦失敗重新設定對焦模式
params.setFocusMode(currentFocusMode);
camera.setParameters(params);
}
}
});
複製程式碼
這種方式發現在一些裝置根本無法對上焦,當無法對上焦時,但是這種方式對焦會有回撥可以知道啥時候相機對好焦了
2、設定對焦模式為 FOCUS_MODE_CONTINUOUS_PICTURE 這種方式用於拍照的連續自動對焦模式,但是記得加上camera.cancelAutoFocus()。
3、自動觸發手動對焦(對於掃碼這種應用,每隔一段兩秒中心點手動對焦一次,效果也非常的好)
public void timerFocuse(int x,int y){
long downTime = SystemClock.uptimeMillis();
final MotionEvent downEvent = MotionEvent.obtain(downTime, downTime,
MotionEvent.ACTION_DOWN, x, y, 0);
downTime += 1000;
final MotionEvent upEvent = MotionEvent.obtain(downTime, downTime,
MotionEvent.ACTION_UP,x, y, 0);
onManualEvent(downEvent); //呼叫手動對焦
downEvent.recycle();
upEvent.recycle();
}
複製程式碼
手動對焦
public static final int FOCUS_METERING_AREA_WEIGHT_DEFAULT = 1000;
public static final int FOCUS_AREA_SIZE_DEFAULT = 300;
private boolean manualFocuse( MotionEvent event) {
if (camera != null) {
Camera.Parameters parameters = camera.getParameters();
String focusMode = parameters.getFocusMode();
Rect rect = calculateFocusArea(event.getX(), event.getY());
List<Camera.Area> meteringAreas = new ArrayList<>();
meteringAreas.add(new Camera.Area(rect, FOCUS_METERING_AREA_WEIGHT_DEFAULT));
if (parameters.getMaxNumFocusAreas() != 0 && focusMode != null &&
(focusMode.equals(Camera.Parameters.FOCUS_MODE_AUTO) ||
focusMode.equals(Camera.Parameters.FOCUS_MODE_MACRO) ||
focusMode.equals(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE) ||
focusMode.equals(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO))
) {
if(!parameters.getSupportedFocusModes().contains(Camera.Parameters.FOCUS_MODE_AUTO)) {
return false; //cannot autoFocus
}
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
parameters.setFocusAreas(meteringAreas);
if (parameters.getMaxNumMeteringAreas() > 0) {
parameters.setMeteringAreas(meteringAreas);
}
camera.setParameters(parameters);
} else if (parameters.getMaxNumMeteringAreas() > 0) {
if(!parameters.getSupportedFocusModes().contains(Camera.Parameters.FOCUS_MODE_AUTO)) {
return false; //cannot autoFocus
}
camera.setParameters(parameters);
} else {
}
}
return false;
}
private Rect calculateFocusArea(float x, float y) {
int buffer = FOCUS_AREA_SIZE_DEFAULT / 2;
int centerX = calculateCenter(x, surfaceView.getWidth(), buffer);
int centerY = calculateCenter(y, surfaceView.getHeight(), buffer);
return new Rect(
centerX - buffer,
centerY - buffer,
centerX + buffer,
centerY + buffer
);
}
private static int calculateCenter(float coord, int dimen, int buffer) {
int normalized = (int) ((coord / dimen) * 2000 - 1000);
if (Math.abs(normalized) + buffer > 1000) {
if (normalized > 0) {
return 1000 - buffer;
} else {
return -1000 + buffer;
}
} else {
return normalized;
}
}
複製程式碼