ZXing二维码白边控制
使用zxing生成二维码bitmap:
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();hints.put(EncodeHintType.CHARACTER_SET, "utf-8");// 图像数据转换,使用了矩阵转换BitMatrix bitMatrix = new QRCodeWriter().encode(url,BarcodeFormat.QR_CODE, width, height, hints);int[] pixels = new int[width * height];// 下面这里按照二维码的算法,逐个生成二维码的图片,// 两个for循环是图片横列扫描的结果for (int y = 0; y < height; y++) {for (int x = 0; x < width; x++) {if (bitMatrix.get(x, y)) {pixels[y * width + x] = 0xff000000;} else {pixels[y * width + x] = 0xffffffff;}}}// 生成二维码图片的格式,使用ARGB_8888Bitmap bitmap = Bitmap.createBitmap(width, height,Bitmap.Config.ARGB_8888);bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
这是最普通的二维码生成使用方法,用这个方法生成的二维码发现白色边距很大,图像是根据矩阵生成的,那我们看一下这个矩阵是怎么生成的,点击QRCodeWriter().encode()进入源码查看:
public BitMatrix encode(String contents,BarcodeFormat format,int width,int height,Map<EncodeHintType,?> hints) throws WriterException {if (contents.isEmpty()) {throw new IllegalArgumentException("Found empty contents");}if (format != BarcodeFormat.QR_CODE) {throw new IllegalArgumentException("Can only encode QR_CODE, but got " + format);}if (width < 0 || height < 0) {throw new IllegalArgumentException("Requested dimensions are too small: " + width + 'x' +height);}ErrorCorrectionLevel errorCorrectionLevel = ErrorCorrectionLevel.L;int quietZone = QUIET_ZONE_SIZE;if (hints != null) {if (hints.containsKey(EncodeHintType.ERROR_CORRECTION)) {errorCorrectionLevel = ErrorCorrectionLevel.valueOf(hints.get(EncodeHintType.ERROR_CORRECTION).toString());}if (hints.containsKey(EncodeHintType.MARGIN)) {quietZone = Integer.parseInt(hints.get(EncodeHintType.MARGIN).toString());}}QRCode code = Encoder.encode(contents, errorCorrectionLevel, hints);//看这里,这里返回的是我们需要的矩阵return renderResult(code, width, height, quietZone);}
从这里可以看到,最终返回的矩阵是从另一个方法返回的,继续点击renderResult进去查看:
private static BitMatrix renderResult(QRCode code, int width, int height, int quietZone) {ByteMatrix input = code.getMatrix();if (input == null) {throw new IllegalStateException();}//这是我们传进来的宽高int inputWidth = input.getWidth();int inputHeight = input.getHeight();//再看,就是这里,最终的二维码宽高竟然给加了2个边距,乘以2是左右和上下2个边距的意思int qrWidth = inputWidth + (quietZone * 2);int qrHeight = inputHeight + (quietZone * 2);int outputWidth = Math.max(width, qrWidth);int outputHeight = Math.max(height, qrHeight);int multiple = Math.min(outputWidth / qrWidth, outputHeight / qrHeight);// Padding includes both the quiet zone and the extra white pixels to accommodate the requested// dimensions. For example, if input is 25x25 the QR will be 33x33 including the quiet zone.// If the requested size is 200x160, the multiple will be 4, for a QR of 132x132. These will// handle all the padding from 100x100 (the actual QR) up to 200x160.int leftPadding = (outputWidth - (inputWidth * multiple)) / 2;int topPadding = (outputHeight - (inputHeight * multiple)) / 2;BitMatrix output = new BitMatrix(outputWidth, outputHeight);for (int inputY = 0, outputY = topPadding; inputY < inputHeight; inputY++, outputY += multiple) {// Write the contents of this row of the barcodefor (int inputX = 0, outputX = leftPadding; inputX < inputWidth; inputX++, outputX += multiple) {if (input.get(inputX, inputY) == 1) {output.setRegion(outputX, outputY, multiple, multiple);}}}return output;}
从这里看到了,最终的宽高被加了边距,边距quietZone是从调用处传进来的,那我们回头看一下encode这个方法传进来的这个值是怎么来的:
//这里定义了一个默认值int quietZone = QUIET_ZONE_SIZE;if (hints != null) {if (hints.containsKey(EncodeHintType.ERROR_CORRECTION)) {errorCorrectionLevel = ErrorCorrectionLevel.valueOf(hints.get(EncodeHintType.ERROR_CORRECTION).toString());}//这里判断了hints是否包含marginif (hints.containsKey(EncodeHintType.MARGIN)) {//再看这里,从hint中获取了margin并赋值quietZone = Integer.parseInt(hints.get(EncodeHintType.MARGIN).toString());}}
到了这里就可以发现,我们的白边其实是quietZone这个玩意儿捣鬼,还记得最开始的时候我们传进来的hints吗,我们在里面添加了一个编码格式hints.put(EncodeHintType.CHARACTER_SET, "utf-8"),同样我们可以在这里传进来一个margin,这样如果encode时可以取到我们的margin,那么就会使用我们的margin,这样就可以随意控制白边的宽度了,如下:
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.MARGIN,"2");
经过测试,一点白边也没有很丑,margin为2时最好看,如果你不想要白边,那么margin赋值0即可。
完毕!
ZXing二维码白边控制相关推荐
- 使用Zxing玩转二维码白边的各个花样
现在在实际应用中使用的最为广泛的二维码生成工具就是Zxing库,本文以Zxing-1.60版本做的研究分析,获取最新版本的Zxing. 1.生成二维码 简单介绍一下Zxing二维码库的使用方式,Zxi ...
- zxing 二维码生成深度定制
二维码生成服务之深度定制 之前写了一篇二维码服务定制的博文,现在则在之前的基础上,再进一步,花样的实现深度定制的需求,我们的目标是二维码上的一切都是可以由用户来随意指定 设计 1. 技术相关 zxin ...
- 谷歌zxing 二维码生成工具
一.加入maven依赖 <!-- 谷歌zxing 二维码 --> <dependency><groupId>com.google.zxing</groupId ...
- 自定义ZXing二维码扫描界面并解决取景框拉伸等问题
自定义ZXing二维码扫描界面并解决取景框拉伸等问题 参考文章: (1)自定义ZXing二维码扫描界面并解决取景框拉伸等问题 (2)https://www.cnblogs.com/tommylemon ...
- ZXing二维码自定义绘画文字
最近项目需要,重新了解了下二维码的自定义文字绘画,直接上代码,记录一下. 一:导入Maven依赖 <!-- Zxing --> <dependency><groupId& ...
- com.google.zxing 二维码工具类
com.google.zxing 二维码工具类 pom 工具类 使用 pom <dependency><groupId>com.google.zxing</groupId ...
- Marco's Java【小工具篇 之 Google Zxing 二维码生成】
前言 二维码相信大家一定不陌生啦,去菜市场买菜,路边摊买个小吃都可以扫一扫,近几年来中国的二维码技术的普及也是大家有目共睹的,而二维码在其他国家虽然有用到,但极少,更没有说全国普及. 二维码又称二维条 ...
- 开发技术-修正二维码白边
1.现状: 在前后端代码都一样的情况下,不同服务器上,生成二维码白边间距不一样. 扒拉了下先前伙伴写的代码,定义了二维码整体的宽和高,但是并没有定义最外围白边的区域. 从网上找了一段代码,亲测可用 / ...
- zxing二维码扫描预览变形的解决方案
-----------------------------------2018.6.1更新--------------------------------------- 附上github地址: htt ...
最新文章
- 2020全球Top10 AI专利公司:美国过半,中国仅占两席
- 【Linux】一步一步学Linux——group文件详解(108)
- APP:分享六款非常实用的冷门APP软件,值得一试!
- 近期GitHub上最热门的开源项目(附链接)
- 前端学习(2658):vue3优化
- 没有完美的软件:编程永远不容易
- QT之Qt之Q_PROPERTY宏理解
- python基础之类的属性方法、魔术方法普通方法
- [转载] 随机游走和随机重启游走_网络动画API的随机游走
- 矩阵论复习笔记:盖尔圆的隔离技巧
- 左耳朵耗子:996不是福气,但努力就会成功么?
- CrossApp推出移动应用开发神器 CrossApp Style
- C语言每日一练——第118天:百钱百鸡问题
- Spring子项目了解
- 自动刷抖音脚本(解放你的双手)
- 2021-2027全球与中国CMF电池市场现状及未来发展趋势
- ubuntu16.04 独立显卡驱动安装
- 微信早安定时推送 简单方法教程(windows通用)
- antd input框获取焦点点击输入历史保存的值后,背景颜色变成淡蓝色解决方法,输入框获取焦点后边框会有一圈淡蓝色光边
- VRTK_Artificial Rotator(人工旋转器)脚本属性详解