QR码从图像文件扫描

Bar*_*ica 8 java android qr-code zxing zbar

试图使用像ZXing,ZBar和他们的叉子这样的几个库,但没有找到扫描条形码的方法,而不是从相机而是从文件中扫描条形码.

有人能指出我正确的方向吗?我最好看一下ZXing:如何从文件中扫描图像(而不是从相机中扫描).

请.

Bar*_*ica 13

最后我找到了解决方案.代码是(源自此处):

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;
}
Run Code Online (Sandbox Code Playgroud)

Gradle参考为:

dependencies {
    compile 'com.google.zxing:core:3.2.1'
}
Run Code Online (Sandbox Code Playgroud)

用法:

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