当前位置: 首页 > news >正文

zxing去白边

2025年了,可能干不了几年了,还能写这种文章还是有点可笑。

背景

zxing库生成的二维码自带白边

分析

生产二维码主要分两步:

1.用QRCodeWriter生成BitMatrix信息

2.根据信息生成bitmap

问题在1。

生成二维码的尺寸实际是有一些规格的,代码详细参见Encoder#encode,以下是具体规格:

确定输出变成的核心代码在QRCodeWriter#renderResult方法里,传入的边长和生成二维码的宽度可能不是倍数关系,比如传入边长15,但是二维码边长是7,那其实7*2=14就是最佳的最终二维码的边长了,剩余部分就是白边。知道了原因,解决方法如下,把输出尺寸变为二维码的倍数就好了。

解决

    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();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);// >> 去除白边 add by 某某outputWidth = qrWidth * multiple;outputHeight = qrHeight * multiple;// << 去除白边 add by 某某// 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;}

http://www.dtcms.com/a/266605.html

相关文章:

  • 督皇口粮酱酒 平价不平质
  • 第十五节:第三部分:特殊文件:XML概述、解析
  • C语言中的输入输出函数:构建程序交互的基石
  • Linux的压缩与解压缩
  • WPF 右键菜单 MenuItem 绑定图片时只显示最后一个 Icon
  • OpenCV 相机标定中的畸变系数及调试硬件
  • 前端渲染大量图片的首屏加载优化方案
  • 刷题笔记--串联所有单词的子串
  • [附源码+数据库+毕业论文]基于Spring+MyBatis+MySQL+Maven+jsp实现的个人财务管理系统,推荐!
  • [附源码+数据库+毕业论文]基于Spring+MyBatis+MySQL+Maven+jsp实现的电影小说网站管理系统,推荐!
  • 儿童益智玩具+AI大模型能不能原地起飞?
  • Unity URP法线贴图实现教程
  • 三、jenkins使用tomcat部署项目
  • RK-Android11-性能优化-限制App内存上限默认512m
  • 利用TCP协议,创建一个多人聊天室
  • 使用reactor-rabbitmq库监听Rabbitmq
  • Go中使用Google Authenticator
  • 东软8位MCU低功耗调试总结
  • 如何使用python识别出文件夹中全是图片合成的的PDF,并将其移动到指定文件夹
  • 【ASP.NET Core】REST与RESTful详解,从理论到实现
  • 当前主流AI智能代理框架对比分析报告
  • 分布式光伏监控系统防孤岛保护装置光功率预测
  • 【论文阅读】VARGPT-v1.1
  • Webpack构建工具
  • node.js下载教程
  • 机器学习数学基础与Python实现
  • 机器学习在智能建筑中的应用:能源管理与环境优化
  • 每日问题总结记录
  • 一、如何用MATLAB画一个三角形 代码
  • 基于AR和SLAM技术的商场智能导视系统技术原理详解