有人能告诉我如何使用Zxing Library增强现实应用程序吗?我知道使用Zxing的最简单的方法是通过Intent,但我需要Camera View,所以我不能使用条形码应用程序.
我有一个SurfaceHolder.Callback,它被添加到主活动并覆盖以下方法:
@Override
public void surfaceCreated(SurfaceHolder holder) {
mCamera = Camera.open();
try {
mCamera.setPreviewDisplay(holder);
} catch (IOException e) {
Log.d(TAG, "Can not set surface holder");
}
mCamera.startPreview();
Parameters parameters = mCamera.getParameters();
parameters.setPreviewSize(1280, 720);
parameters.setPictureSize(1280, 720);
mCamera.setParameters(parameters);
QrCodeReader reader = new QrCodeReader();
mCamera.setPreviewCallback(reader);
}
设置的图片大小必须可用,因为它在list.getSupportedPictureSizes()列表中.
而这个方法在QrCodeReader类中实现了PreviewCallback:
private Result result;
private MultiFormatReader reader = new MultiFormatReader();
private boolean init = false;
public QrCodeReader(){
Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType,
Object>();
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
reader.setHints(hints);
}
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
PlanarYUVLuminanceSource source = new PlanarYUVLuminanceSource(data,
1280, 720, 0, 0, 1280, 720, true);
HybridBinarizer hybBin = new HybridBinarizer(source);
BinaryBitmap bitmap = new BinaryBitmap(hybBin);
try {
result = reader.decodeWithState(bitmap);
Log.d("Result", "Result found!");
} catch (NotFoundException e) {
Log.d(TAG, "NotFoundException");
} finally {
reader.reset();
}
}
Logcat只显示NotFoundException.
解决方法:
NotFoundException是正常的.如果框架没有条形码,那就是结果.这并不意味着本身出现任何问题.继续扫描.