java 生成二维码图片及生成二维码图片(带字)

导入maven依赖



    com.google.zxing
    core
    3.4.1


    com.google.zxing
    javase
    3.4.1

自动生成至D:\\data文件夹下(根据自己的需求可修改)

package me.zhengjie;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import org.junit.jupiter.api.Test;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

public class QRCodeGenerator {
    public static void main(String[] args) {
        //基本样例
        String text = "abc";
        int width = 300;
        int height = 300;

        // 设置二维码的编码方式和其他参数
        Map hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");

        try {
            // 生成二维码的BitMatrix
            BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints);

            // 指定存储二维码图片的路径
            Path filePath = Paths.get("D:\\data\\样例.png");

            // 确保文件路径的父目录存在
            filePath.getParent().toFile().mkdirs();

            // 将BitMatrix转换为图片并保存到指定路径
            MatrixToImageWriter.writeToPath(bitMatrix, "PNG", filePath);

            System.out.println("QR Code has been generated: " + filePath.toAbsolutePath());
        } catch (WriterException | IOException e) {
            e.printStackTrace();
        }
    }


    @Test
    public void contextLoads() {
        contextLoads2("abc","样例");
        String a = "abc-";
        for (int i = 0; i <12 ; i++) {
            String b = a+(10000+i);
            contextLoads2(b,b);
        }
    }
    //生成二维码图片
    public void contextLoads1(String text,String name) {
        int width = 100;
        int height = 100;

        // 设置二维码的编码方式和其他参数
        Map hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");

        try {
            // 生成二维码的BitMatrix
            BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints);

            // 指定存储二维码图片的路径
            Path filePath = Paths.get("D:\\data\\"+name+".png");

            // 确保文件路径的父目录存在
            filePath.getParent().toFile().mkdirs();

            // 将BitMatrix转换为图片并保存到指定路径
            MatrixToImageWriter.writeToPath(bitMatrix, "PNG", filePath);

            System.out.println("QR Code has been generated: " + filePath.toAbsolutePath());
        } catch (WriterException | IOException e) {
            e.printStackTrace();
        }
    }
    //二维码图上带字
    public void contextLoads2(String text,String name) {
        String textToEncode = text;
        int qrCodeWidth = 100;
        int qrCodeHeight = 100;
        int textPadding = 10; // 文本与二维码之间的间距
        int textSize = 10; // 文本字体大小
        int totalHeight = qrCodeHeight + textSize + textPadding;

        try {
            // 生成二维码的BitMatrix
            BitMatrix bitMatrix = generateQRCode(textToEncode, qrCodeWidth, qrCodeHeight);

            // 将BitMatrix转换为BufferedImage
            BufferedImage qrCodeImage = MatrixToImageWriter.toBufferedImage(bitMatrix);

            // 创建一个新的BufferedImage来容纳二维码和文本
            BufferedImage combinedImage = new BufferedImage(
                    qrCodeWidth, qrCodeHeight + textSize + textPadding, BufferedImage.TYPE_INT_RGB);

            // 绘制二维码到新的BufferedImage上
            Graphics2D g2d = combinedImage.createGraphics();
            g2d.setColor(Color.WHITE);
            g2d.fillRect(0, 0, qrCodeWidth, totalHeight);
            g2d.drawImage(qrCodeImage, 0, 0, null);

            // 设置文本样式
            Font font = new Font("Arial", Font.PLAIN, textSize);
            g2d.setFont(font);
            g2d.setColor(Color.BLACK); // 文本颜色

            // 绘制文本到图片下方
            FontMetrics metrics = g2d.getFontMetrics();
            int textX = (qrCodeWidth - metrics.stringWidth(textToEncode)) / 2;
            int textY = qrCodeHeight + textPadding;
            g2d.drawString(textToEncode, textX, textY);

            g2d.dispose();

            // 指定存储图片的路径
            Path filePath = Paths.get("D:\\data\\"+name+".png");

            // 确保文件路径的父目录存在
            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 hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        return new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints);
    }

}

运行contextLoads方法可实现

java 生成二维码图片及生成二维码图片(带字)_第1张图片

你可能感兴趣的:(java)