1、引入依赖
<!-- Maven 依赖 -->
<dependency><groupId>com.google.zxing</groupId><artifactId>core</artifactId><version>3.4.1</version>
</dependency>
2、生成二维码
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;public class QRCodeGenerator {// 二维码尺寸private static final int QRCODE_SIZE = 512;// 背景色private static final Color BACKGROUND_COLOR = new Color(230, 132, 72);// 字体颜色private static final Color TEXT_COLOR = Color.BLACK;// 二维码与底部文字之间的间距private static final int TEXT_PADDING = 10;// 底部文字大小private static int FONT_SIZE = 22;/*** 生成带底部文字的二维码** @param content 二维码内容* @param text 底部文字* @param outputFile 输出文件*/public static void generateQRCodeWithText(String content, String text, File outputFile) throws WriterException, IOException {// 创建二维码BufferedImage qrImage = createQRCode(content);// 计算最终图片高度(二维码高度 + 文字区域高度)int height = qrImage.getHeight() + TEXT_PADDING * 2 + FONT_SIZE;int width = qrImage.getWidth();// 创建新的BufferedImage,包含二维码和文字区域BufferedImage combinedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);Graphics2D g = combinedImage.createGraphics();// 设置背景为白色g.setColor(BACKGROUND_COLOR);// 填充整个图片区域g.fillRect(0, 0, width, height);// 绘制二维码到新图片的上部g.drawImage(qrImage, 0, 0, null);// 设置文字样式String fontName = "Microsoft YaHei";// 尝试加载字体,如果找不到则使用默认字体Font font;try {font = new Font(fontName, Font.PLAIN, FONT_SIZE);} catch (Exception e) {font = new Font("Arial", Font.PLAIN, FONT_SIZE);System.err.println("警告: 无法加载中文字体 " + fontName + ", 使用默认字体");}g.setColor(TEXT_COLOR);g.setFont(new Font(fontName, Font.BOLD, FONT_SIZE));// 计算文字位置(居中)FontMetrics fontMetrics = g.getFontMetrics();int textWidth = fontMetrics.stringWidth(text);// 如果文字太长,自动缩小字体,左右各留10像素边距int maxTextWidth = width - 20;while (textWidth > maxTextWidth && FONT_SIZE > 8) {FONT_SIZE--;font = new Font(fontName, Font.PLAIN, FONT_SIZE);g.setFont(font);fontMetrics = g.getFontMetrics();textWidth = fontMetrics.stringWidth(text);}int x = (width - textWidth) / 2;int y = qrImage.getHeight() + TEXT_PADDING + fontMetrics.getAscent() / 2;// 绘制文字g.drawString(text, x, y);g.dispose();// 保存图片ImageIO.write(combinedImage, "png", outputFile);}/*** 创建基本二维码图片** @param content 二维码内容* @return BufferedImage* @throws WriterException*/private static BufferedImage createQRCode(String content) throws WriterException {Map<EncodeHintType, Object> hints = new HashMap<>();hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);hints.put(EncodeHintType.MARGIN, 1);QRCodeWriter qrCodeWriter = new QRCodeWriter();BitMatrix bitMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hints);BufferedImage image = new BufferedImage(QRCODE_SIZE, QRCODE_SIZE, BufferedImage.TYPE_INT_RGB);for (int x = 0; x < QRCODE_SIZE; x++) {for (int y = 0; y < QRCODE_SIZE; y++) {image.setRGB(x, y, bitMatrix.get(x, y) ? TEXT_COLOR.getRGB() : BACKGROUND_COLOR.getRGB());}}return image;}public static void main(String[] args) throws IOException, WriterException {String content = "https://www.baidu.com";String text = "百度一下,你就知道";File outputFile = new File("E:\\DeskTop\\baidu.png");generateQRCodeWithText(content, text, outputFile);System.out.println("二维码已生成到: " + outputFile.getAbsolutePath());}
}
3、效果预览

如您在阅读中发现不足,欢迎留言!!!


















