二维码生成工具包

该博客介绍了一个用于生成和读取二维码的Java工具类`QrCodeCreateUtil`。该工具类提供了生成指定内容、大小和格式的二维码图片的方法,并能读取二维码中的信息。示例代码展示了如何使用该工具类生成二维码图片并保存,以及从二维码图片中读取信息。
摘要由CSDN通过智能技术生成

二维码生成工具包

package com.dhsr.springboot_code.util;

import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeReader;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Hashtable;


/**
 *  * @Description: 二维码生成和读的工具类
 *  * @Author: Administrator
 *  * @CreateDate: 2018/9/12 11:53
 *  * @Version: 1.0
 *  
 */
public class QrCodeCreateUtil {
    /**
     *      * 生成包含字符串信息的二维码图片
     *      * @param outputStream 文件输出流路径
     *      * @param content 二维码携带信息
     *      * @param qrCodeSize 二维码图片大小
     *      * @param imageFormat 二维码的格式
     *      * @throws WriterException
     *      * @throws IOException
     *      
     */
    public static boolean createQrCode(OutputStream outputStream, String content, int qrCodeSize, String imageFormat) throws WriterException, IOException {
//设置二维码纠错级别MAP
        Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
        hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); // 矫错级别
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
//创建比特矩阵(位矩阵)的QR码编码的字符串
        BitMatrix byteMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, qrCodeSize, qrCodeSize, hintMap);
// 使BufferedImage勾画QRCode  (matrixWidth 是行二维码像素点)
        int matrixWidth = byteMatrix.getWidth();
        int revise = matrixWidth * 2 / 9;
        BufferedImage image = new BufferedImage(matrixWidth - revise, matrixWidth - revise, BufferedImage.TYPE_INT_RGB);
        image.createGraphics();
        Graphics2D graphics = (Graphics2D) image.getGraphics();
        graphics.setColor(Color.WHITE);
        graphics.fillRect(0, 0, matrixWidth, matrixWidth);
// 使用比特矩阵画并保存图像
        graphics.setColor(Color.BLACK);
        for (int i = 0; i < matrixWidth; i++) {
            for (int j = 0; j < matrixWidth; j++) {
                if (byteMatrix.get(i, j)) {
                    graphics.fillRect(i - revise / 2, j - revise / 2, 1, 1);
                }
            }
        }
        return ImageIO.write(image, imageFormat, outputStream);
    }

    /**
     *      * 读二维码并输出携带的信息
     *      
     */
    public static String readQrCode(InputStream inputStream) throws IOException {
//从输入流中获取字符串信息
        BufferedImage image = ImageIO.read(inputStream);
//将图像转换为二进制位图源
        LuminanceSource source = new BufferedImageLuminanceSource(image);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        QRCodeReader reader = new QRCodeReader();
        Result result = null;
        try {
            result = reader.decode(bitmap);
        } catch (ReaderException e) {
            e.printStackTrace();
        }
        return result.getText();
    }

    /**
     *      * 测试代码
     *      * @throws WriterException
     *      
     */
    public static void main(String[] args) throws IOException, WriterException {

        String content = "http://baidu.com";
        String path = "E:\\Project_job\\springboot_code\\src\\main\\resources\\img\\test.jpg";
        createQrCode(new FileOutputStream(new File(path)), content, 90, "JPEG");
        String info = readQrCode(new FileInputStream(new File(path)));
        System.out.println(info);
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

&芒果冰沙&

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

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

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

打赏作者

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

抵扣说明:

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

余额充值