| | |
| | | |
| | | @Autowired |
| | | private IArchiveRecordsService iArchiveRecordsService; |
| | | private static BufferedImage trimWhiteBorder(BufferedImage img) { |
| | | int width = img.getWidth(); |
| | | int height = img.getHeight(); |
| | | |
| | | int minX = width, minY = height, maxX = 0, maxY = 0; |
| | | |
| | | // 查找非白色像素的边界 |
| | | for (int y = 0; y < height; y++) { |
| | | for (int x = 0; x < width; x++) { |
| | | int rgb = img.getRGB(x, y); |
| | | // 如果不是白色 |
| | | if ((rgb & 0x00FFFFFF) != 0x00FFFFFF) { |
| | | if (x < minX) minX = x; |
| | | if (x > maxX) maxX = x; |
| | | if (y < minY) minY = y; |
| | | if (y > maxY) maxY = y; |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 如果全是白色,返回原图 |
| | | if (minX > maxX || minY > maxY) { |
| | | return img; |
| | | } |
| | | |
| | | // 裁剪 |
| | | int newWidth = maxX - minX + 1; |
| | | int newHeight = maxY - minY + 1; |
| | | |
| | | // 添加1像素安全边距(可选) |
| | | minX = Math.max(0, minX - 1); |
| | | minY = Math.max(0, minY - 1); |
| | | newWidth = Math.min(width - minX, newWidth + 2); |
| | | newHeight = Math.min(height - minY, newHeight + 2); |
| | | |
| | | return img.getSubimage(minX, minY, newWidth, newHeight); |
| | | } |
| | | //生产二维码 |
| | | public byte[] createQrCodeN(String content, int width, int height) throws IOException { |
| | | QrConfig config = new QrConfig(width, height); |
| | | |
| | | config.setMargin(0); |
| | | config.setMargin(2); |
| | | // 高纠错级别 |
| | | config.setErrorCorrection(ErrorCorrectionLevel.H); |
| | | // 设置前景色,既二维码颜色(自行选择颜色修改) |
| | | config.setForeColor(new Color(11, 11, 11).getRGB()); |
| | | // 设置背景色(灰色)需要背景色时候打开链接 |
| | | config.setBackColor(new Color(242,242,242).getRGB()); |
| | | config.setForeColor(java.awt.Color.BLACK); |
| | | config.setBackColor(java.awt.Color.WHITE); |
| | | |
| | | |
| | | BufferedImage bufferedImage = QrCodeUtil.generate(// |
| | | content, //二维码内容 |
| | | config |
| | | ); |
| | | |
| | | // 步骤2:手动裁剪白边 |
| | | bufferedImage = trimWhiteBorder(bufferedImage); |
| | | System.out.println(bufferedImage); |
| | | System.out.println("---02340230949394"); |
| | | ByteArrayOutputStream os = new ByteArrayOutputStream(); |