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;}