| | |
| | | //二维码图上带字 |
| | | public static void contextLoads2(String content, String name, String filePathStr) |
| | | { |
| | | int qrCodeWidth = 700; |
| | | int qrCodeHeight = 700; |
| | | int textPadding = 5; // 文本与二维码之间的间距 |
| | | int textSize = 30; // 文本字体大小 |
| | | int totalHeight = qrCodeHeight + 300; |
| | | int qrCodeWidth = 350; |
| | | int qrCodeHeight = 350; |
| | | int textPadding = 2; // 文本与二维码之间的间距 |
| | | int textSize = 10; // 文本字体大小 |
| | | int totalHeight = 380; |
| | | |
| | | try { |
| | | // 生成二维码的BitMatrix |
| | |
| | | |
| | | // 创建一个新的BufferedImage来容纳二维码和文本 |
| | | BufferedImage combinedImage = new BufferedImage( |
| | | qrCodeWidth, 900, BufferedImage.TYPE_INT_RGB); |
| | | qrCodeWidth, totalHeight, BufferedImage.TYPE_INT_RGB); |
| | | |
| | | // 绘制二维码到新的BufferedImage上 |
| | | Graphics2D g2d = combinedImage.createGraphics(); |
| | | g2d.setColor(Color.WHITE); |
| | | g2d.fillRect(0, 0, qrCodeWidth, 900); |
| | | g2d.drawImage(qrCodeImage, 0, 0, null); |
| | | g2d.fillRect(0, 0, qrCodeWidth, totalHeight); |
| | | g2d.drawImage(qrCodeImage, -5, -10,360,360, null); |
| | | |
| | | // 设置文本样式 |
| | | Font font = new Font("BLACK", Font.PLAIN, textSize); |
| | | g2d.setFont(font); |
| | | g2d.setColor(Color.BLACK); // 文本颜色 |
| | | |
| | | |
| | | // 绘制文本到图片下方 |
| | | FontMetrics metrics = g2d.getFontMetrics(); |
| | | Font font = new Font("Black", Font.BOLD, 20); |
| | | FontMetrics metrics = g2d.getFontMetrics(font); |
| | | int textX = (qrCodeWidth - metrics.stringWidth("沈丘惠企执法")) / 2; |
| | | int textX1 = (qrCodeWidth - metrics.stringWidth(name)) / 2; |
| | | int textY = qrCodeHeight + textPadding; |
| | | g2d.drawString("沈丘惠企执法", textX, textY); |
| | | g2d.drawString(name, textX1, textY+50); |
| | | // 设置文本样式 |
| | | g2d.setFont(font); |
| | | g2d.setColor(Color.BLACK); // 文本颜色 |
| | | g2d.drawString("沈丘惠企执法", textX, 330); |
| | | Font font1 = new Font("Serif", Font.PLAIN, 15); |
| | | FontMetrics metrics1 = g2d.getFontMetrics(font1); |
| | | int textX1 = (360 - metrics1.stringWidth(name)) / 2; |
| | | g2d.setFont(font1); |
| | | g2d.setColor(Color.BLACK); // 文本颜色 |
| | | g2d.drawString(name, textX1, 355); |
| | | |
| | | g2d.dispose(); |
| | | |