ZQN
2024-08-15 2562d6ce39aa405514c9e22ff0237f3f145040b6
project-common/src/main/java/com/project/common/utils/qrcode/QRCodeUtil.java
@@ -2,6 +2,7 @@
import com.google.zxing.*;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
@@ -14,7 +15,11 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
public class QRCodeUtil {
    private static final String CHARSET = "utf-8";
@@ -106,7 +111,8 @@
        QRCodeUtil.encode(content, null, outputStream, false);
    }
    public static String decode(File file) throws IOException, NotFoundException {
    public static String decode(File file) throws IOException, NotFoundException
    {
        BufferedImage image;
        image = ImageIO.read(file);
        if (image == null) {
@@ -133,5 +139,74 @@
        }
    }
    //二维码图上带字
    public static void contextLoads2(String content, String name, String filePathStr)
    {
        int qrCodeWidth = 350;
        int qrCodeHeight = 350;
        int textPadding = 2; // 文本与二维码之间的间距
        int textSize = 10; // 文本字体大小
        int totalHeight = 380;
        try {
            // 生成二维码的BitMatrix
            BitMatrix bitMatrix = generateQRCode(content, qrCodeWidth, qrCodeHeight);
            // 将BitMatrix转换为BufferedImage
            BufferedImage qrCodeImage = MatrixToImageWriter.toBufferedImage(bitMatrix);
            // 创建一个新的BufferedImage来容纳二维码和文本
            BufferedImage combinedImage = new BufferedImage(
                    qrCodeWidth, totalHeight, BufferedImage.TYPE_INT_RGB);
            // 绘制二维码到新的BufferedImage上
            Graphics2D g2d = combinedImage.createGraphics();
            g2d.setColor(Color.WHITE);
            g2d.fillRect(0, 0, qrCodeWidth, totalHeight);
            g2d.drawImage(qrCodeImage, -5, -10,360,360, null);
            // 绘制文本到图片下方
            Font font = new Font("Black", Font.BOLD, 20);
            FontMetrics metrics = g2d.getFontMetrics(font);
            int textX = (qrCodeWidth - metrics.stringWidth("沈丘惠企执法")) / 2;
            // 设置文本样式
            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();
            // 指定存储图片的路径
            Path filePath = Paths.get(filePathStr);
            // 确保文件路径的父目录存在
            filePath.getParent().toFile().mkdirs();
            // 保存图片到文件
            ImageIO.write(combinedImage, "PNG", filePath.toFile());
            System.out.println("QR Code with text has been generated: " + filePath.toAbsolutePath());
        } catch (WriterException | IOException e) {
            e.printStackTrace();
        }
    }
    private static BitMatrix generateQRCode(String text, int width, int height) throws WriterException
    {
        Map<EncodeHintType, Object> hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        return new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints);
    }
}