二维码生成和解析工具包-zxing

今天在看一个开源项目的时候发现一个工具类,一个简单的生成二维码的工具类,测试了下很是方便。

虽然在平常的开发中没有使用过,为了以后的场景做个备份

1、简介

GitHub 开源地址: github.com/zxing/zxing

zxing 二进制包下载地址:repo1.maven.org/maven2/com/...

zxing Maven 仓库地址:mvnrepository.com/artifact/co...

ZXing支持各种条形码,二维码扫描,由多个模块组成, 而且支持PC端,移动端。

2、做个例子

2.1 添加依赖

如果要生成二维码图片,那么我们只需要它的核心库即可

如果你想通过命令行读取二维码图片,那么我们需要使用它的JavaSE库。您可以为其添加以下依赖项。

js 复制代码
<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>

2.2 工具类

js 复制代码
package com.pdool.demo;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;

import javax.imageio.ImageIO;
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;


/**
 * 二维码生成工具
 * </pre>
 */
public class MatrixToImageWriter {


    private static final int BLACK = 0xFF000000;
    private static final int WHITE = 0xFFFFFFFF;

    private MatrixToImageWriter() {
    }

    /**
     * 根据二维矩阵的碎片 生成对应的二维码图像缓冲
     *
     * @param matrix 二维矩阵的碎片 包含 宽高 行,字节
     * @return 二维码图像缓冲
     * @see BitMatrix
     */
    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) ? BLACK : WHITE);
            }
        }
        return image;
    }


    /**
     * 二维码生成文件
     *
     * @param matrix 二维矩阵的碎片 包含 宽高 行,字节
     * @param format 格式
     * @param file   保持的文件地址
     * @throws IOException 文件保存异常
     */
    public static void writeToFile(BitMatrix matrix, String format, File file)
            throws IOException {
        BufferedImage image = toBufferedImage(matrix);
        if (!ImageIO.write(image, format, file)) {
            throw new IOException("Could not write an image of format " + format + " to " + file);
        }
    }


    /**
     * 二维码生成流
     *
     * @param matrix 二维矩阵的碎片 包含 宽高 行,字节
     * @param format 格式
     * @param stream 保持的文件输出流
     * @throws IOException 文件保存异常
     */
    public static void writeToStream(BitMatrix matrix, String format, OutputStream stream)
            throws IOException {
        BufferedImage image = toBufferedImage(matrix);
        if (!ImageIO.write(image, format, stream)) {
            throw new IOException("Could not write an image of format " + format);
        }
    }


    /**
     * 二维码信息写成JPG文件
     *
     * @param content 二维码信息
     * @param fileUrl 文件地址
     */
    public static void writeInfoToJpgFile(String content, String fileUrl) {
        MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
        Map hints = new HashMap();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        try {
            BitMatrix bitMatrix = multiFormatWriter.encode(content,
                    BarcodeFormat.QR_CODE, 250, 250, hints);
            File file1 = new File(fileUrl);
            MatrixToImageWriter.writeToFile(bitMatrix, "jpg", file1);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 二维码信息写成JPG BufferedImage
     *
     * @param content 二维码信息
     * @return JPG BufferedImage
     */
    public static BufferedImage writeInfoToJpgBuff(String content) {
        BufferedImage re = null;

        MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
        Map hints = new HashMap();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        try {
            BitMatrix bitMatrix = multiFormatWriter.encode(content,
                    BarcodeFormat.QR_CODE, 250, 250, hints);
            re = MatrixToImageWriter.toBufferedImage(bitMatrix);
        }
        catch (Exception e) {
            e.printStackTrace();
        }

        return re;
    }

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

    }

}

2.3 测试

这里直接上测试代码

js 复制代码
public static void main(String[] args) throws IOException {  
    writeInfoToJpgFile("关注我公众号:程序这点事","E:\\work\\test.jpg");  
}

文章中不让贴二维码,测试一下你就知道,这里直接使用支付宝的识别

相关推荐
小码哥_常7 小时前
告别MySQL!大厂集体转投PostgreSQL,到底藏着什么玄机?
后端
刀法如飞8 小时前
Go数组去重的20种实现方式,AI时代解决问题的不同思路
后端·算法·go
AI人工智能+电脑小能手9 小时前
【大白话说Java面试题】【Java基础篇】第30题:JDK动态代理和CGLIB动态代理有什么区别
java·开发语言·后端·面试·代理模式
swipe9 小时前
别再把 AI 聊天做成纯文本:从 agui 这个前后端项目,拆解“可感知工具调用”的流式 AI UI
后端·langchain·llm
GetcharZp9 小时前
GitHub 爆火!纯 Go 编写的文件同步神器 Syncthing,凭什么成为程序员的标配?
后端
hERS EOUS9 小时前
SpringBoot 使用 spring.profiles.active 来区分不同环境配置
spring boot·后端·spring
LucianaiB9 小时前
我用飞书多维表做了一个 AI 活动推荐智能体:每天自动催我别错过截止日期!
后端
铁皮饭盒10 小时前
第2课:5分钟!用 Trae AI 生成你的第一个后端服务(Bunjs + Elysia)
前端·后端·全栈
金銀銅鐵10 小时前
[git] 浅解 git reset 命令
git·后端