QR码从图像文件扫描 [英] QR code scan from image file

查看:167
本文介绍了QR码从图像文件扫描的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:如何扫描文件中的图像(不是来自相机)。

Can someone point me to right direction? Preferably I'm looking into ZXing: how to scan image from file (not from camera).

请。

推荐答案

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

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参考为:

Gradle referencing as:

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

这篇关于QR码从图像文件扫描的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆