【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解码之后的字符串
运行结果:
Android 开发讨论群:84778336
iOS 开发讨论群:82873648

本作品采用知识共享署名-非商业性使用 3.0 许可协议进行许可。
转载请署名李震(博客地址:http://www.cnblogs.com/dyingbleed/),且不得用于商业目的。
博客园博客已停止更新,博客地址:dyingbleed.com
iOS 开发讨论群:82873648

本作品采用知识共享署名-非商业性使用 3.0 许可协议进行许可。
转载请署名李震(博客地址:http://www.cnblogs.com/dyingbleed/),且不得用于商业目的。
博客园博客已停止更新,博客地址:dyingbleed.com
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?