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

 目录

1、简介

2、做个例子

2.1 添加依赖

2.2 工具类

2.3 测试

3、总结


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

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

1、简介

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

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

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

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

2、做个例子

2.1 添加依赖

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

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

<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 工具类

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 测试

这里直接上测试代码

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

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

15abb351b53ae5532c1ebb3b43107e8.jpg

3、总结

只是无意中看到的一个工具类,很好用,记录下。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

香菜+

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值