工具系列——Java生成二维码

5 篇文章 0 订阅

Java如何生成二维码工具
在这里插入图片描述
直接上代码

/**
 * @author wyy
 * @version 1.0
 * @date 2020/12/4 13:42
 * 二维码描述方法
 **/
public class QrCode {


    /**
     * 生成二维码的方法写入输出流
     *
     * @param text     内容
     * @param width    宽度
     * @param height   高度
     * @param filePath 输出流
     * @throws WriterException 异常
     * @throws IOException     异常
     * @author wyy
     */
    public static void generateQRCodeImage(String content, int width, int height, OutputStream filePath) throws WriterException, IOException {
        generateQRCode(content, width, height, filePath);
    }


    /**
     * 生成二维码的方法直接输出
     *
     * @param text     内容
     * @param width    宽度
     * @param height   高度
     * @param filePath 输出流
     * @throws WriterException 异常
     * @throws IOException     异常
     * @author wyy
     */
    public static void generateQRCodeImage(String content, int width, int height, String filePath) throws WriterException, IOException {
        generateQRCode(content, width, height, filePath);

    }


    /**
     * 生成二维码的方法写入直接输出
     *
     * @param text     内容
     * @param width    宽度
     * @param height   高度
     * @param filePath 输出流
     * @throws WriterException 异常
     * @throws IOException     异常
     */
    private static void generateQRCode(String content, int width, int height, String filePath) throws WriterException, IOException {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();

        BitMatrix bitMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, width, height);

        Path path = FileSystems.getDefault().getPath(filePath);

        MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
    }

    /**
     * 生成二维码的方法写入输出流
     *
     * @param text     内容
     * @param width    宽度
     * @param height   高度
     * @param filePath 输出流
     * @throws WriterException 异常
     * @throws IOException     异常
     */
    private static void generateQRCode(String content, int width, int height, OutputStream filePath) throws WriterException, IOException {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();

        BitMatrix bitMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, width, height);

        MatrixToImageWriter.writeToStream(bitMatrix, "PNG", filePath);
    }
}

utils

/**
 * @author wyy
 * @version 1.0
 * @date 2020/12/4 13:33
 * 二维码生成工具
 **/
public class QrCodeUtil {


    /**
     * 生成二维码的方法
     *
     * @param content      内容
     * @param width        宽度
     * @param height       高度
     * @param outputStream 输出流
     */
    public static void generateQRCodeImge(String content, int width, int height, OutputStream outputStream) throws IOException {
        try {
            QrCode.generateQRCodeImage(content, width, height, outputStream);
        } catch (WriterException e) {
            e.printStackTrace();
        }
    }

    /**
     * 生成二维码的方法
     *
     * @param content      内容
     * @param width        宽度
     * @param height       高度
     * @param outputStream 输出流
     */
    public static void generateQRCodeImge(String content, int width, int height, String outputStream) throws IOException {
        try {
            QrCode.generateQRCodeImage(content, width, height, outputStream);
        } catch (WriterException e) {
            e.printStackTrace();
        }
    }

}

演示代码

 @Test
    void contextLoads() throws IOException {
        QrCodeUtil.generateQRCodeImge("www.chuyuan.ink",350,350,"D:\\work\\img\\MyBlogAddress.png");
     
    }

结果 会自动跳转至博主的博客网址 如果是文本信息识别出来的就是文本信息

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值