Java使用zxing生成二维码(带有logo)

2020-06-08 16:53:40 浏览数 (3)

首先要添加zxing依赖

代码语言:javascript复制
<!-- 二维码生成依赖 -->
<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>core</artifactId>
    <version>3.4.0</version>
</dependency>
<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>javase</artifactId>
    <version>3.4.0</version>
</dependency>

下面是代码:

代码语言:javascript复制
package com.xqd;


import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

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

/**
 * @ClassName: QRCodeUtil
 * @Description: 二维码生成
 * @Authror: XQD
 * @Date: 2020/1/16 10:34
 */
public class QRCode {

    public static void main(String[] args) {

        // 定义二维码的参数
        int width = 300; // 图片宽度
        int height = 300; // 图片高度
        String format = "jpg"; // 图片格式  如果是png类型,logo图变成黑白的,
        String content = "https://blog.csdn.net/weixin_44467567/article/details/103876746";// 二维码内容

        // 1.定义HashMap hints
        HashMap hints = new HashMap();
        // 2.hints调用put函数设置字符集、间距以及纠错度为M
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);//纠错等级【L,M,Q,H】
        hints.put(EncodeHintType.MARGIN, 2);
        // 生成二维码
        try {
            MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
            // 3.最后用MultiformatWriter函数类调用echoed函数并返回一个值 然后写入文件
            BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
            // 这里路径后面的img.png不可省略,前面是自己选取生成的图片地址
            Path file = new File("F:/生成的二维码/img.png").toPath();
            MatrixToImageWriter.writeToPath(bitMatrix, format, file);
            //*************添加logo*****************
            //读取二维码图片
            BufferedImage bufferedImage = ImageIO.read(new File(file.toString()));
            //获取画笔
            Graphics2D graphics = bufferedImage.createGraphics();
            //读取logo图片
            BufferedImage logo = ImageIO.read(new File("D:/图片/Saved Pictures/头像/微信图片_20200109163054.jpg"));
            //设置二维码大小,太大了会覆盖二维码,此处为20%
            int logoWidth = logo.getWidth() > bufferedImage.getWidth()*2 /10 ? (bufferedImage.getWidth()*2 /10) : logo.getWidth();
            int logoHeight = logo.getHeight() > bufferedImage.getHeight()*2 /10 ? (bufferedImage.getHeight()*2 /10) : logo.getHeight();
            //设置logo图片放置的位置,中心
            int x = (bufferedImage.getWidth() - logoWidth) / 2;
            int y = (bufferedImage.getHeight() - logoHeight) / 2;
            //开始合并并绘制图片
            graphics.drawImage(logo,x,y,logoWidth,logoHeight,null);
            graphics.drawRoundRect(x,y,logoWidth,logoHeight,15,15);
            //logob边框大小
            graphics.setStroke(new BasicStroke(2));
            //logo边框颜色
            graphics.setColor(Color.WHITE);
            graphics.drawRect(x,y,logoWidth,logoHeight);
            graphics.dispose();
            logo.flush();
            bufferedImage.flush();
            ImageIO.write(bufferedImage, format ,new File("F:/生成的二维码/img2.png"));

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }


}

1 人点赞