首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从图像文件扫描二维码

从图像文件扫描二维码
EN

Stack Overflow用户
提问于 2015-08-21 14:49:46
回答 1查看 13.4K关注 0票数 14

尝试使用几个库,如ZXing,ZBar和它们的分支,但没有找到扫描条形码的方法,不是从相机,而是从文件。

有人能告诉我正确的方向吗?最好是我正在研究ZXing:如何从文件(而不是相机)扫描图像。

请。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-08-21 16:35:54

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

代码语言:javascript
复制
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引用为:

代码语言:javascript
复制
dependencies {
    compile 'com.google.zxing:core:3.2.1'
}

用法:

代码语言:javascript
复制
InputStream is = new BufferedInputStream(new FileInputStream(file));
Bitmap bitmap = BitmapFactory.decodeStream(is);
String decoded=scanQRImage(bitmap);
Log.i("QrTest", "Decoded string="+decoded);
票数 21
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32134072

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档