【问题标题】:ZXing on Android scanning 2D codes but throwing NotFoundException for 1D codesAndroid 上的 ZXing 扫描二维码但对一维码抛出 NotFoundException
【发布时间】: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


【解决方案1】:

解决办法是摄像头在侧面拍摄,而 ZXing 似乎没有从某个角度检查一维条码。

以 90 度间隔旋转位图为我解决了这个问题。

新增功能如下:

public void onPictureTaken(byte[] data, Camera camera){
    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);

    Result result = null;

    for(int i = 0; i < 4; i++){
        BinaryBitmap bitmap = BinaryBitmapFromJpegData(data, i * 90);

        try {
            result = reader.decode(bitmap, hints);
            break;
        } catch (NotFoundException e) {}
    }

    if(result == null){
        Toast.makeText(context, "No Barcode Found", Toast.LENGTH_LONG).show();
    } else {
        Toast.makeText(context, "Found barcode: " + result.getText(), Toast.LENGTH_LONG).show();
    }
}

private BinaryBitmap BinaryBitmapFromJpegData(byte[] data, int rotation){
    Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

    if(rotation != 0){
        bitmap = RotateBitmap(bitmap, rotation);
    }

    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;
}

public static Bitmap RotateBitmap(Bitmap source, float angle){
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);
    return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}

【讨论】:

  • 您可以尝试修改相机设置以捕捉旋转 90 度的预览帧。然后你就可以避免这个黑客:-)
  • 以正确的角度拍摄是对的 - 但是,这很重要,因为应用程序将扫描的某些条形码的角度会导致此问题再次发生。
猜你喜欢
  • 2011-11-06
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-05
  • 1970-01-01
相关资源
最近更新 更多