java生成帶圓形logo的二維碼圖片,且支持背景圖片


1、pom.xml中添加

<dependency>
          <groupId>cn.hutool</groupId>
          <artifactId>hutool-all</artifactId>
          <version>5.2.3</version>
      </dependency>
       <dependency>
          <groupId>com.google.zxing</groupId>
          <artifactId>core</artifactId>
          <version>3.3.3</version>
      </dependency>

2、實現代碼

package app.action.test;

import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class QRCodeUtil {
    public static void main(String[] args) throws Exception {
        createLogoQrCode("https://www.baidu.com/", "C:\\Users\\saq\\Desktop\\QR\\11.png", "C:\\Users\\saq\\Desktop\\QR\\123.png", "打開微信掃一掃辦理Hello World", "C:\\Users\\saq\\Desktop\\QR\\555.png");
    }

    /**
     * 生成帶背景圖片帶圓形logo的二維碼
     *
     * @param content  二維碼內容
     * @param logoPath logo圖片地址
     * @param bgPath   背景圖片地址
     * @param text     下發描述文字
     * @param savePath 生成圖片的保存地址
     */
    private static void createLogoQrCode(String content, String logoPath, String bgPath, String text, String savePath) {
        BufferedImage image = generateQrCode(content);
        BufferedImage QcLogoCode = insertLogoImage(image, logoPath);
        createPicture(QcLogoCode, bgPath, text, savePath);
    }

    /**
     * 生成中間帶logo的二維碼
     *
     * @param content 二維碼內容
     * @return
     */
    private static BufferedImage generateQrCode(String content) {
        BufferedImage image = QrCodeUtil.generate(
                content, // 二維碼內容
                QrConfig.create()
                        .setErrorCorrection(ErrorCorrectionLevel.H) //糾錯級別使用zxing的ErrorCorrectionLevel枚舉封裝,包括:L、M、Q、H幾個參數,由低到高。低級別的像素塊更大
                        .setWidth(290) // 二維碼的寬
                        .setHeight(290) // 二維碼的高
                        .setMargin(0)); // 邊距
        return image;
    }

    /**
     * 在二維碼上畫上圓形logo圖標
     *
     * @param image
     * @return
     * @throws Exception
     */
    private static BufferedImage insertLogoImage(BufferedImage image, String logoPath) {
        try {
            Image src = ImageIO.read(new File(logoPath));
            // 插入LOGO
            Graphics2D graph = image.createGraphics();
            // 圖片是一個圓型
            Ellipse2D.Double shape = new Ellipse2D.Double(110, 110, 70, 70);
            // 需要保留的區域
            graph.setClip(shape);
            graph.drawImage(src, 110, 110, 70, 70, null);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return image;
    }

    /**
     * 帶文字和背景圖片的二維碼創建
     *
     * @param image    生成的二維碼
     * @param bgPath   背景圖片的路徑
     * @param text     二維碼上的文字
     * @param savePath 生成圖片的保存位置
     * @throws IOException
     */
    private static void createPicture(BufferedImage image, String bgPath, String text, String savePath) {
        try {
            // 首先先畫背景圖片
            BufferedImage backgroundImage = ImageIO.read(new File(bgPath));
            // 背景圖片的寬度
            int bgWidth = backgroundImage.getWidth();
            // 二維碼的寬度
            int qrWidth = image.getWidth();
            // 二維碼距離背景圖片橫坐標(X)的距離,居中顯示
            int distanceX = (bgWidth - qrWidth) / 2;
            // 二維碼距離背景圖片縱坐標(Y)的距離
            int distanceY = distanceX;
            // 基於圖片backgroundImage對象打開繪圖
            Graphics2D rng = backgroundImage.createGraphics();
            rng.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP));
            rng.drawImage(image, distanceX, distanceY, null);

            //設置字體
            Font font = new Font("宋體", Font.PLAIN, 22);
            rng.setFont(font);
            rng.setColor(Color.red);
            // 獲取當前文字的對象
            FontMetrics metrics = rng.getFontMetrics(font);
            // 文字在圖片中的坐標 這里設置在中間
            int startX = (bgWidth - metrics.stringWidth(text)) / 2; //當前文字對象到橫坐標(X)的距離
            int startY = backgroundImage.getHeight() - 60; //當前文字對象到縱坐標(Y)的距離
            rng.drawString(text, startX, startY);
            // 處理繪圖
            rng.dispose();
            image = backgroundImage;
            image.flush();
            // 將繪制好的圖片寫入當前路徑中
            ImageIO.write(image, "png", new File(savePath));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 3、效果圖


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM