JAVA生成二维码

646 阅读1分钟

这篇文章主要介绍Java生成二维码和带logo的二维码

1.Maven依赖

<dependency>
    <groupId>com.github.binarywang</groupId>
    <artifactId>qrcode-utils</artifactId>
    <version>1.3</version>
</dependency>

2.demo示例

import com.github.binarywang.utils.qrcode.MatrixToImageWriter;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import lombok.extern.slf4j.Slf4j;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class QRCodeUtils {
    private static final String QR_CODE_IMAGE_FORMAT = "png";

    public static final int width = 400, height = 400;

    private static final int LOGO_SIZE = 60;

    /**
     * 生成二维码
     *
     * @param text
     * @param outFilePath
     * @return
     */
    public static String generateQRCodeImage(String text, String outFilePath) {
        try {
            QRCodeWriter qrCodeWriter = new QRCodeWriter();
            Map<EncodeHintType, Object> hints = new HashMap<>();
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height, hints);
            BufferedImage qrCodeImage = MatrixToImageWriter.toBufferedImage(bitMatrix);
            ImageIO.write(qrCodeImage, QR_CODE_IMAGE_FORMAT, new File(outFilePath));
            return outFilePath;
        } catch (Exception e) {
            log.error("生成二维码失败,错误信息:", e);
        }
       return null;
    }

    /**
     * 生成带logo的二维码
     *
     * @param text
     * @param outFilePath  包含文件名
     * @param logoFilePath
     */
    public static String generateQRCodeWithLogo(String text, String outFilePath, String logoFilePath) {
        try {
            QRCodeWriter qrCodeWriter = new QRCodeWriter();
            Map<EncodeHintType, Object> hints = new HashMap<>();
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height, hints);
            BufferedImage qrCodeImage = MatrixToImageWriter.toBufferedImage(bitMatrix);
            // 加载logo图片
            BufferedImage logoImage = ImageIO.read(new File(logoFilePath));
            int logoWidth = logoImage.getWidth() > LOGO_SIZE ? LOGO_SIZE : logoImage.getWidth();
            int logoHeight = logoImage.getHeight() > LOGO_SIZE ? LOGO_SIZE : logoImage.getHeight();
            int logoX = (width - logoWidth) / 2;
            int logoY = (height - logoHeight) / 2;
            // 合并二维码和logo图片
            Graphics2D graphics = qrCodeImage.createGraphics();
            graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f));
            graphics.drawImage(logoImage, logoX, logoY, logoWidth, logoHeight, null);
            graphics.dispose();
            // 保存合并后的图片
            ImageIO.write(qrCodeImage, QR_CODE_IMAGE_FORMAT, new File(outFilePath));
            return outFilePath;
        } catch (Exception e) {
            log.error("生成带logo的二维码失败,错误信息:", e);
        }
        return null;
    }

    public static void main(String[] args) {
        String text = "Hello World";
        String path = "/Users/camel/Desktop/pay/2.jpg";
        generateQRCodeImage(text, path);
    }

}