zxing 生成图片 带logo 正方形 圆形 兼容

ZxingImageUtils是一个用于生成和处理二维码的Java工具类,它利用Google的Zxing库进行编码和解码。该类包含了将BitMatrix转换为BufferedImage的方法,创建圆形或椭圆形二维码的功能,并且可以添加logo到二维码中。此外,还提供了将生成的二维码保存到文件或流中的功能,并支持设置纠错级别和字符编码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ZxingImageUtils 工具类


package com.zhengbai.atlas.common.util;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
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;
import java.io.OutputStream;
import java.net.URL;
import java.util.Hashtable;

/**
 * @author wujie
 * @date 2023/3/15 11:46
 */
public class ZxingImageUtilsTest {


    private static final int BLACK = 0xFF000000;//用于设置图案的颜色
    private static final int WHITE = 0xFFFFFFFF; //用于背景色

    private ZxingImageUtilsTest() {
    }

    public static BufferedImage toBufferedImage(BitMatrix matrix) {
        int width = matrix.getWidth();
        int height = matrix.getHeight();
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                image.setRGB(x, y,  (matrix.get(x, y) ? WHITE : BLACK));
            }
        }
        return image;
    }

    /**
     * 传入的图像必须是正方形的 才会 圆形 如果是长方形的比例则会变成椭圆的
     * @return
     * @throws IOException
     */
    public static BufferedImage convertCircular(BufferedImage bi1)  {
        // 透明底的图片
        BufferedImage bi2 = new BufferedImage(bi1.getWidth(), bi1.getHeight(), BufferedImage.TYPE_INT_RGB);
        Ellipse2D.Double shape = new Ellipse2D.Double(0, 0, bi1.getWidth(), bi1.getHeight());
        Graphics2D g2 = bi2.createGraphics();
        g2.fillRect(0,0,bi1.getWidth(),bi1.getHeight());
        g2.setClip(shape);
        // 使用 setRenderingHint 设置抗锯齿
        g2.drawImage(bi1, 0, 0, null);
        g2.dispose();
        return bi2;
    }


    public static void writeToFile(BitMatrix matrix, String format, File file, String avatar) throws IOException {
        BufferedImage image = toBufferedImage(matrix);
        // 这里需要圆形就用 convertCircular 正方形矩阵的就用toBufferedImage
        BufferedImage bufferedImage = convertCircular(image);
        //设置logo图标
        bufferedImage = LogoMatrix(bufferedImage,avatar);

        if (!ImageIO.write(bufferedImage, format, file)) {
            throw new IOException("Could not write an image of format " + format + " to " + file);
        }else{
            System.out.println("图片生成成功!");
        }
    }

    public static void writeToStream(BitMatrix matrix, String format, OutputStream stream, String avatar) throws IOException {
        BufferedImage image = toBufferedImage(matrix);
        //设置logo图标
        image = LogoMatrix(image,avatar);

        if (!ImageIO.write(image, format, stream)) {
            throw new IOException("Could not write an image of format " + format);
        }
    }


    /**
     * 设置 logo
     * @param matrixImage 源二维码图片
     * @return 返回带有logo的二维码图片
     * @throws IOException
     * @author Administrator sangwenhao
     */
    public static BufferedImage LogoMatrix(BufferedImage matrixImage,String avatar) throws IOException {
        /**
         * 读取二维码图片,并构建绘图对象
         */
        Graphics2D g2 = matrixImage.createGraphics();
        int matrixWidth = matrixImage.getWidth();
        int matrixHeight = matrixImage.getHeight();
        /**
         * 读取Logo图片
         */
        URL url = new URL(avatar);
        BufferedImage logo = ImageIO.read(url);

        //开始绘制图片
        g2.drawImage(logo,matrixWidth/5*2,matrixHeight/5*2, matrixWidth/5, matrixHeight/5, null);//绘制
        BasicStroke stroke = new BasicStroke(5,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
        g2.setStroke(stroke);// 设置笔画对象
        //指定弧度的圆角矩形
        Ellipse2D.Float round = new Ellipse2D.Float(matrixWidth/5*2, matrixHeight/5*2, matrixWidth/5, matrixHeight/5);
        g2.setColor(Color.white);
        g2.draw(round);// 绘制圆弧矩形
        g2.dispose();
        matrixImage.flush() ;
        return matrixImage ;
    }


    public static void generateImage(String contents,String avatar) throws IOException, WriterException {
        // contents 二维码内容
        int width = 430; // 二维码图片宽度 300
        int height = 430; // 二维码图片高度300

        String format = "jpg";// 二维码的图片格式 jpg

        Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
        // 指定纠错等级,纠错级别(L 7%、M 15%、Q 25%、H 30%)
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        // 内容所使用字符集编码
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
//		hints.put(EncodeHintType.MAX_SIZE, 350);//设置图片的最大值
//	    hints.put(EncodeHintType.MIN_SIZE, 100);//设置图片的最小值
        hints.put(EncodeHintType.MARGIN, 1);//设置二维码边的空度,非负数

        BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,//要编码的内容
                //编码类型,目前zxing支持:Aztec 2D,CODABAR 1D format,Code 39 1D,Code 93 1D ,Code 128 1D,
                //Data Matrix 2D , EAN-8 1D,EAN-13 1D,ITF (Interleaved Two of Five) 1D,
                //MaxiCode 2D barcode,PDF417,QR Code 2D,RSS 14,RSS EXPANDED,UPC-A 1D,UPC-E 1D,UPC/EAN extension,UPC_EAN_EXTENSION
                BarcodeFormat.QR_CODE,
                width, //条形码的宽度
                height, //条形码的高度
                hints);//生成条形码时的一些配置,此项可选

        // 生成二维码
        File outputFile = new File("E:\\images" + File.separator + IdUtils.uuid()+".jpg");//指定输出路径

        ZxingImageUtils.writeToFile(bitMatrix, format, outputFile,avatar);
    }

    public static void main(String[] args) throws Exception {
        try {
            generateImage("我是图片20981209384","https://img-blog.csdnimg.cn/ac222b87a8714bf2ae64a3386b235393.png");
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }


}


在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值