zxing生成二维码带logo、zxing生成二维码带图片

package com.example.springbootdemo.tt;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

import javax.imageio.ImageIO;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class T9 {

    public static void main(String[] args) throws Exception {

        /**
         * 步骤
         * 1、zxing生成二维码的bufferimage
         * 2、取出logo后缩小为二维码bufferimage的5倍并切圆角
         * 3、创建一个新的bufferimage把二维码的buffer和缩小后的logo的buffer绘画即可
         */

        String content  = "hello";// 内容
        int width = 500;// 宽
        int height = 500;// 高

        Map<EncodeHintType, String> hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        hints.put(EncodeHintType.MARGIN, "0");// 内边距

        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix bitMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
        BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(bitMatrix);// 二维码BufferedImage

        BufferedImage logo = ImageIO.read(new File("F:" + File.separator + "Z.PNG"));// 原logo图
        // 缩小5倍
        BufferedImage compressImage = new BufferedImage(bufferedImage.getWidth() / 5,bufferedImage.getHeight() / 5, BufferedImage.TYPE_INT_ARGB);
        Graphics2D logoGraphics = (Graphics2D) compressImage.getGraphics();
        logoGraphics.setBackground(Color.WHITE);

        // 切圆角 start
        logoGraphics.setComposite(AlphaComposite.Src);
        logoGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        logoGraphics.setColor(Color.WHITE);
        logoGraphics.fill(new RoundRectangle2D.Float(0, 0, compressImage.getWidth(), compressImage.getHeight(), 30, 30));// 核心:30是圆角的大小
        logoGraphics.setComposite(AlphaComposite.SrcAtop);
        // 切圆角 end

        /*
        一个圆
        Ellipse2D.Double shape = new Ellipse2D.Double(0, 0, compressImage.getWidth(), compressImage.getHeight());
        logoGraphics.setClip(shape);*/

        logoGraphics.drawImage(logo, 0,0,compressImage.getWidth(), compressImage.getHeight(),null);
        logoGraphics.dispose();// 完成缩小

        // 新的bufferedImage1,分别装二维码buffer和缩小后的Logo的buffer
        BufferedImage bufferedImage1 = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = (Graphics2D) bufferedImage1.getGraphics();

        int x = (bufferedImage.getWidth() - compressImage.getWidth()) / 2;// 缩小后的logo位置的x坐标
        int y = (bufferedImage.getHeight() - compressImage.getHeight()) / 2;// 缩小后的logo位置的y坐标

        g.drawImage(bufferedImage, 0, 0, null);
        g.drawImage(compressImage, x, y, null);
        g.dispose();

        ImageIO.write(bufferedImage1, "PNG", new File("F:" + File.separator + "Z2.PNG"));


    }
}

猜你喜欢

转载自blog.csdn.net/u013845177/article/details/88254487