【问题标题】:QR code scan from image file从图像文件扫描二维码
【发布时间】:2020-01-14 20:00:11
【问题描述】:

尝试使用 ZXing、ZBar 及其分支等几个库,但没有找到不是从相机而是从文件中扫描条形码的方法。

有人能指出我正确的方向吗?最好我正在研究 ZXing: how to scan image from file (not from camera).

请。

【问题讨论】:

标签: java android zxing qr-code zbar


【解决方案1】:

最后我找到了解决方案。代码是(源自here):

import com.google.zxing.*;

public static String scanQRImage(Bitmap bMap) {
    String contents = null;

    int[] intArray = new int[bMap.getWidth()*bMap.getHeight()];
    //copy pixel data from the Bitmap into the 'intArray' array
    bMap.getPixels(intArray, 0, bMap.getWidth(), 0, 0, bMap.getWidth(), bMap.getHeight());

    LuminanceSource source = new RGBLuminanceSource(bMap.getWidth(), bMap.getHeight(), intArray);
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

    Reader reader = new MultiFormatReader();
    try {
        Result result = reader.decode(bitmap);
        contents = result.getText();
    }
    catch (Exception e) {
        Log.e("QrTest", "Error decoding barcode", e);
    }
    return contents;
}

Gradle 引用为:

dependencies {
    compile 'com.google.zxing:core:3.2.1'
}

用法:

InputStream is = new BufferedInputStream(new FileInputStream(file));
Bitmap bitmap = BitmapFactory.decodeStream(is);
String decoded=scanQRImage(bitmap);
Log.i("QrTest", "Decoded string="+decoded);

【讨论】:

  • @barmley,知道如何使用 zbar 进行操作
  • 谢谢,它确实适用于复制的二维码。但是,如果我试图扫描一张照片,尽管图像对我来说看起来非常好,但它无法识别。有没有机会让它识别真实的照片?调整质量或一些过滤器之类的?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-15
  • 1970-01-01
相关资源
最近更新 更多