ZXing二维码介绍

ZXing(Zebra Crossing)是Google开发的一个二维码解析和生成的开源库。
ZXing GitHub地址
引入

        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>core</artifactId>
            <version>3.3.0</version>
        </dependency>
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>javase</artifactId>
            <version>3.3.0</version>
        </dependency>

public class QRCodeUtil {
    /**
     * 生成二维码
     * @param content   源内容
     * @param outputStream 输出流
     * @throws Exception
     */
    public static void createQRImage(String content, OutputStream outputStream) throws Exception {
        Hashtable hints = new Hashtable();
        // 指定纠错等级
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        hints.put(EncodeHintType.MARGIN, 1);
        BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 300, 300,
                hints);
        int width = bitMatrix.getWidth();
        int height = bitMatrix.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, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
            }
        }
        // 存到磁盘
        ImageIO.write(image, "jpg", outputStream);
        outputStream.flush();
        outputStream.close();
    }

    public static void createQRImage2(String content,OutputStream outputStream) throws Exception {
        Hashtable hints = new Hashtable();
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        hints.put(EncodeHintType.MARGIN, 1);
        BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 300, 300,
                hints);
        //生成png格式的图片保存到imgPath路径位置
        MatrixToImageWriter.writeToStream(bitMatrix, "jpg",outputStream);
        outputStream.flush();
        outputStream.close()
    }
    public static void createQRImage2(String content,File file) throws Exception {
        Hashtable hints = new Hashtable();
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        hints.put(EncodeHintType.MARGIN, 1);
        BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 300, 300,
                hints);
        //生成png格式的图片保存到imgPath路径位置
        MatrixToImageWriter.writeToFile(bitMatrix, "jpg",file);
    }
    public static void main(String[] args) throws Exception {
        String codeUrl = "http://www.baidu.com";
        //使用订单号来作为二维码的图片名称
        File file = new File("E:\\","测试3" + ".jpg");
        FileOutputStream out=new FileOutputStream(file);
        createQRImage(codeUrl, out);
        System.out.println(file.delete());
    }

}

条形码有很多种类,二维(条形)码就是其中一种。表示内容也有不同,有的只能表示纯数字,不能表示字母。


BarcodeFormat.CODE_128; // 表示高密度数据, 字符串可变长,符号内含校验码
BarcodeFormat.CODE_39;
BarcodeFormat.CODE_93;
BarcodeFormat.CODABAR; // 可表示数字0 - 9,字符$、+、 -、还有只能用作起始/终止符的a,b,c d四个字符,可变长度,没有校验位
BarcodeFormat.DATA_MATRIX;
BarcodeFormat.EAN_8;
BarcodeFormat.EAN_13;
BarcodeFormat.ITF;
BarcodeFormat.PDF417; // 二维码
BarcodeFormat.QR_CODE; // 二维码
BarcodeFormat.RSS_EXPANDED;
BarcodeFormat.RSS14;
BarcodeFormat.UPC_E; // 统一产品代码E:7位数字,最后一位为校验位
BarcodeFormat.UPC_A; // 统一产品代码A:12位数字,最后一位为校验位
BarcodeFormat.UPC_EAN_EXTENSION;

参考:https://blog.csdn.net/liudongdong19/article/details/80147404
https://blog.csdn.net/zhoumushui/article/details/51008264

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

晨港飞燕

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

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

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

打赏作者

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

抵扣说明:

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

余额充值