【发布时间】:2014-05-28 23:58:52
【问题描述】:
我在我的应用程序中集成了 ZXing 3.1.1,使其能够拍摄条形码照片并对输入数据进行操作。
我可以用它扫描二维码(如二维码和数据矩阵),但所有一维条码都无法扫描。
我的代码如下:
public void onPictureTaken(byte[] data, Camera camera){
BinaryBitmap bbm = BinaryBitmapFromJpegData(data); // Impl. below
MultiFormatReader reader = new MultiFormatReader();
Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
Collection<BarcodeFormat> possible_types = new ArrayList<BarcodeFormat>();
possible_types.add(BarcodeFormat.UPC_A);
possible_types.add(BarcodeFormat.UPC_E);
possible_types.add(BarcodeFormat.EAN_8);
possible_types.add(BarcodeFormat.EAN_13);
possible_types.add(BarcodeFormat.QR_CODE);
possible_types.add(BarcodeFormat.DATA_MATRIX);
hints.put(DecodeHintType.POSSIBLE_FORMAT, possible_types);
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
try {
Result result = reader.decode(bbm, hints);
Toast.makeText(context, "Found barcode: " + result.getText(), Toast.LENGTH_LONG).show();
} catch (NotFoundException e) {
Toast.makeText(context, "No barcode found", Toast.LENGTH_LONG).show();
Log.d("myapp", "Not found! (" + e.getMessage() + ")", e);
}
}
private BinaryBitmap BinaryBitmapFromJpegData(byte[] data){
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
int[] intArray = new int[bitmap.getWidth() * bitmap.getHeight()];
bitmap.getPixels(intArray, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
LuminanceSource source = new RGBLuminanceSource(bitmap.getWidth(), bitmap.getHeight(), intArray);
BinaryBitmap bbm = new BinaryBitmap(new HybridBinarizer(source));
return bbm;
}
也不是很有帮助的堆栈跟踪:
有人有什么想法吗?我正在三星 Galaxy Note 2 上进行测试。
【问题讨论】:
-
你能从 logcat 发布堆栈跟踪吗?
-
@bstar55:没有堆栈跟踪 - 在上面的 OP 中添加了屏幕截图。
-
啊,好吧。我查看了文档,似乎在图像中找不到可解码的条形码时会发生异常。
-
是的。一直在阅读。我只是觉得奇怪的是,它扫描二维码不会失败,而且一百分之一的一维码没有一个不注册。
-
出于好奇,您尝试扫描的条形码来自哪里?印刷标签?离开屏幕?
标签: java android camera barcode zxing