简体   繁体   English

从图像文件扫描二维码

[英]QR code scan from image file

Tried to use several libraries like ZXing, ZBar and their forks but didn't find way to scan barcode not from camera but from file.尝试使用多个库,如 ZXing、ZBar 及其分支,但没有找到不是从相机而是从文件扫描条码的方法。

Can someone point me to right direction?有人可以指出我正确的方向吗? Preferably I'm looking into ZXing: how to scan image from file (not from camera).最好我正在研究 ZXing: how to scan image from file (not from camera)。

Please.请。

In the end I've found solution.最后我找到了解决方案。 Code is (originated from 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 referencing as: Gradle 引用为:

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

Usage:用法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM