随笔 - 65  文章 - 0  评论 - 21  阅读 - 32万

【Android QR Code】开源项目: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解码之后的字符串

 

运行结果:

posted on   Anthony Li  阅读(6259)  评论(1编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
< 2012年11月 >
28 29 30 31 1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 1
2 3 4 5 6 7 8

博客园博客已停止更新,博客地址:dyinigbleed.com

点击右上角即可分享
微信分享提示