我用过安卓的zbar扫描仪,它很容易捕捉到条形码。但问题在于,在具有自动对焦功能的手机上,它捕捉条形码的速度太快而无法正确检测到。如果它可以再等待几毫秒,它就可以捕捉到更清晰的图像,从而不会显示“未找到”页面。
我怎么解决这个问题?是否有延迟关注条形码的规定?可能是捕捉图像的延迟?
您是在谈论示例代码CameraTestActivity.java吗?
实现一个计数器来计算类似的扫描结果。如果扫描结果保持不变(例如连续 10 次),我们可以认为结果是相当可靠的。
我真的很喜欢@Juuso_Ohtonen 的回复,实际上只是在我自己的阅读器中使用它,但是如果您想要 AutoFocus 延迟,您可以创建一个 Camera.AutoFocusCallback 对象并使用 .postDelayed 实现其 onAutoFocus 方法。然后在您的 Camera camera.autoFocus() 方法上使用该对象。
// Mimic continuous auto-focusing
Camera.AutoFocusCallback autoFocusCB = new Camera.AutoFocusCallback() {
public void onAutoFocus(boolean success, Camera camera) {
autoFocusHandler.postDelayed(doAutoFocus, 1000);
}
};
这部分用于扩展SurfaceView的类中,然后实现surfaceChanged();
public CameraPreview(Context context, Camera camera,
PreviewCallback previewCb,
AutoFocusCallback autoFocusCb) {
super(context);
mCamera = camera;
previewCallback = previewCb;
autoFocusCallback = autoFocusCb;
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
/*
* If your preview can change or rotate, take care of those events here.
* Make sure to stop the preview before resizing or reformatting it.
*/
if (mHolder.getSurface() == null) {
// preview surface does not exist
return;
}
// stop preview before making changes
try {
mCamera.stopPreview();
} catch (Exception e) {
// ignore: tried to stop a non-existent preview
}
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.setPreviewCallback(previewCallback);
mCamera.startPreview();
mCamera.autoFocus(autoFocusCallback);
} catch (Exception e) {
Log.d("DBG", "Error starting camera preview: " + e.getMessage());
}
}