Java生成带logo的二维码,并将二维码添加到图片中

1.pom.xml

<!--生成二维码-->
<dependency>
   <groupId>cn.hutool</groupId>
   <artifactId>hutool-extra</artifactId>
   <version>5.4.3</version>
</dependency>
<dependency>
   <groupId>com.google.zxing</groupId>
   <artifactId>core</artifactId>
   <version>3.3.3</version>
</dependency>
 
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

2.工具类

package com.octv.nft.common.utils;

import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.springframework.util.ResourceUtils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @Auther: ***
 * @Date: 2022/8/25 10:33
 * @Description:
 *  图片添加水印
 */
public class AddWatermarkUtil {

    /***
     * 在一张背景图上添加二维码
     */
    public static void addWater(String roundImage,String logoImage, String url, String outImgPath) {
        try {
            // 读取原图片信息
            //得到文件
            File file = ResourceUtils.getFile(roundImage);
            //文件转化为图片
            Image srcImg = ImageIO.read(file);
            //获取图片的宽
            int srcImgWidth = srcImg.getWidth(null);
            //获取图片的高
            int srcImgHeight = srcImg.getHeight(null);
            // 加水印
            BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = bufImg.createGraphics();
            g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);
            //使用工具类生成二维码
            Image image = createQrCode(logoImage, url, 200, 200);
            //将小图片绘到大图片上,500,300 .表示你的小图片在大图片上的位置。
            g.drawImage(image, 70, 300, null);
            //设置颜色。
            g.setColor(Color.WHITE);
            g.dispose();
            // 输出图片
            FileOutputStream outImgStream = new FileOutputStream(outImgPath);
            ImageIO.write(bufImg, "jpg", outImgStream);
            outImgStream.flush();
            outImgStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 将字符串转为二维码
     * @param roundImage
     * @param url
     * @param width
     * @param height
     * @return
     * @throws IOException
     */
    private static BufferedImage createQrCode(String roundImage, String url, int width, int height) throws IOException {
        QrConfig config = new QrConfig(width, height);
        Image image = ImageIO.read(new FileInputStream(roundImage));
        config.setImg(image);
        config.setErrorCorrection(ErrorCorrectionLevel.H);
        return QrCodeUtil.generate(url, config);
    }



}

3.测试

    public static void main(String[] args) {
        //在图片上生成二维码
        String roundImage = "F:/testShare/1.jpg";
        String logoImage = "F:/testShare/logo.jpg";
        String url = "https://www.baidu.com";
        String outImgPath = "F:/testShare/2.jpg";
        //原图片路径 loge图片 二维码 输出地址
        addWater(roundImage, logoImage,url, outImgPath);

    }

4.效果图

 

posted @ 2022-08-25 14:36  八块腹肌的青书  阅读(1365)  评论(0编辑  收藏  举报
Live2D