我在三星 Galaxy Grand 2 (Android 4) 上遇到了同样的问题,我找到了一个解决方案。我在启动相机之前禁用了自动对焦功能。1-2秒后我启用它。我尝试了几种方法,并决定定期切换自动对焦功能。为了实现这一点,我创建了 util 类 - FocusHandler。
public class FocusHandler implements Runnable{
private final int FOCUS_OFF_TIME = 2000;
private final int FOCUS_ON_TIME = 20000;
private boolean flag = false;
private boolean state = false;
private Handler handler;
private WeakReference<ZXingScannerView> scannerView;
public FocusHandler(Handler handler, ZXingScannerView scannerView){
this.handler = handler;
this.flag = false;
this.scannerView = new WeakReference<>(scannerView);
}
public void start(){
state = true;
this.handler.post(this);
}
public void stop(){
state = false;
scannerView.clear();
}
@Override
public void run() {
if (!state || this.scannerView.get() == null){
return;
}
int time;
if (!flag){
this.scannerView.get().setAutoFocus(flag);
time = FOCUS_OFF_TIME;
}
else{
this.scannerView.get().setAutoFocus(flag);
time = FOCUS_ON_TIME;
}
flag = !flag;
handler.postDelayed(this, time);
}
}
/*************** 活动 ******************/
private ZXingScannerView scannerView;
private FocusHandler focusHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scanner);
scannerView = new ZXingScannerView(this);
focusHandler = new FocusHandler(new Handler(), scannerView);
frameLayout.addView(scannerView);
}
@Override
protected void onResume() {
super.onResume();
scannerView.setResultHandler(this);
scannerView.setAutoFocus(false);
scannerView.startCamera();
focusHandler.start();
}
@Override
public void onPause() {
super.onPause();
scannerView.stopCamera();
focusHandler.stop();
}