Java生成二维码工具类

 

复制代码
<!--二维码生成架包引用-->
<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>core</artifactId>
    <version>3.3.3</version>
</dependency>
<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>javase</artifactId>
    <version>3.3.3</version>
</dependency>
复制代码

 

复制代码
package com.xuebusi.toutiao.admin.api.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.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * 使用谷歌的zxing生成二维码工具类
 */
public class QRCodeUtil {
    // 二维码宽度
    private static final int width = 100;
    // 二维码高度
    private static final int height = 100;
    // 二维码默认文件格式
    private static final String format = "png";
    // 二维码参数
    private static final Map<EncodeHintType, Object> hints = new HashMap();

    static {
        // 字符编码
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        // 容错等级:L、M、Q、H,其中L最低,H最高
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        // 二维码图片边距
        hints.put(EncodeHintType.MARGIN, 2);
    }

    /**
     * 返回一个BufferedImage对象
     *
     * @param content 二维码内容
     * @param width   宽
     * @param height  高
     * @return BufferedImage
     * @throws WriterException
     */
    public static BufferedImage toBufferedImage(String content, int width, int height) throws WriterException {
        BitMatrix bitMatrix = buildBitMatrix(content, width, height);
        return MatrixToImageWriter.toBufferedImage(bitMatrix);
    }

    /**
     * 将二维码输出到一个流中
     *
     * @param content 二维码内容
     * @param stream  输出流
     * @param width   宽
     * @param height  高
     * @throws WriterException
     * @throws IOException
     */
    public static void writeToStream(String content, OutputStream stream, int width, int height) throws WriterException, IOException {
        BitMatrix bitMatrix = buildBitMatrix(content, width, height);
        MatrixToImageWriter.writeToStream(bitMatrix, format, stream);
    }

    private static BitMatrix buildBitMatrix(String content, int width, int height) throws WriterException {
        if (content == null || content.trim().length() == 0) {
            throw new IllegalArgumentException("二维码内容不能为空");
        }
        return new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
    }

    /**
     * 生成二维码图片文件
     *
     * @param content 二维码内容
     * @param path    文件保存路径
     * @param width   宽
     * @param height  高
     * @throws WriterException
     * @throws IOException
     */
    public static void createQRCode(String content, String path, int width, int height) throws WriterException, IOException {
        BitMatrix bitMatrix = buildBitMatrix(content, width, height);
        MatrixToImageWriter.writeToPath(bitMatrix, format, new File(path).toPath());
    }

    public static void main(String[] args) {
        try {
            QRCodeUtil.createQRCode("https://sina.cn", "D:\\sina.png", width, height);
            System.out.println("生成二维码成功");
        } catch (WriterException | IOException e) {
            System.out.println(e.getMessage());
        }
    }
}
复制代码

 

posted @   xuebusi  阅读(743)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示