二维码生成和解析工具包-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 小时前
身弱体质|你的 FM 该关了📻
后端
Csvn8 小时前
📊 SQL 入门 Day 18:索引原理
后端·sql
Csvn8 小时前
🐍 Day3 : Python 容器精讲 — list、dict、set、tuple 底层实现与高级操作
后端·python
用户938515635078 小时前
ESLint 代码规范完全指南——从 AST 原理到 flat config 逐行解析
javascript·后端·代码规范
用户938515635078 小时前
深入理解 Next.js:从 SPA 的 SEO 问题到约定式路由与 SSR 原理
前端·后端·全栈
码事漫谈10 小时前
Deepseek涨价了,前后对比,竟然差这么多
后端
东风破_11 小时前
大前端手里的 Next.js:从 #root 到 SEO,一份 HTML 的两种命运
前端·后端
IT_陈寒12 小时前
Vue的v-for不听话?我被这个Key的坑整懵了
前端·人工智能·后端
众人皆醒我独醉12 小时前
训练加速实战:Flash Attention、Gradient Checkpointing 与数据流水线
后端·面试·gpu
阿黎梨梨12 小时前
Next.js 全栈开发:从 SPA 的痛点到 SSR 的破局之道
前端·后端