开源项目:ZXing(三)二维码解码

继续上一节的内容

本节我们将对上一节的QQ群号二维码进行解码

QQ群号二维码图片另存为后,将下载的.jpg拷贝到项目assets目录下

 

1、解码配置

Map<DecodeHintType,Object> hints = new EnumMap<DecodeHintType,Object>(DecodeHintType.class);
Collection<BarcodeFormat> decodeFormats = EnumSet.noneOf(BarcodeFormat.class);
decodeFormats.addAll(EnumSet.of(BarcodeFormat.QR_CODE));
hints.put(DecodeHintType.POSSIBLE_FORMATS, decodeFormats);
hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");

配置需要解码的图片格式为二维码,字符集编码为UTF-8

 

2、加载图片

复制代码
private BinaryBitmap loadImage(String fileName, Context context) throws IOException {
    Bitmap bitmap = BitmapFactory.decodeStream(context.getResources().getAssets().open(fileName));
    
    int lWidth = bitmap.getWidth();
    int lHeight = bitmap.getHeight();
    int[] lPixels = new int[lWidth * lHeight];
    bitmap.getPixels(lPixels, 0, lWidth, 0, 0, lWidth, lHeight);
    return new BinaryBitmap(new HybridBinarizer(new RGBLuminanceSource(lWidth, lHeight, lPixels)));
}
复制代码

首先,从assets目录下加载图片,编码为Android定义的位图Bitmap对象

然后,取得该图片的像素数据,存入整形数组中

最后,将图片的像素数据,转成ZXing定义的位图BinaryBitmap对象

类BinaryBitmap:This class is the core bitmap class used by ZXing to represent 1 bit data. Reader objects accept a BinaryBitmap and attempt to decode it.

注意:目前仅支持.jpg格式图片

 

3、解码

Result lResult = new MultiFormatReader().decode(loadImage(fileName, context), hints);
String lText = lResult.getText();

lText解码之后的字符串

 

运行结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值