我使用zbar扫描仪安卓,它很容易捕获条形码。 但问题是,在具有自动对焦功能的手机上,它会过快地捕获条形码以便正确检测到它。 如果它只能等待几毫秒,那么它就能够捕获更清晰的图像,从而不会显示“未找到”页面。
我该如何解决这个问题? 有没有规定延迟对条形码的关注? 也许延迟捕捉图像?
答案 0 :(得分:0)
您是在谈论示例代码CameraTestActivity.java吗?
实施一个计数相似扫描结果的计数器。如果扫描结果保持不变(例如连续10次),我们可以假设结果非常可靠。
答案 1 :(得分:0)
我非常喜欢@ 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());
}
}