Java 生成二维码

本文详细介绍使用Java生成二维码的方法,包括zxing、qrcode和jquery-qrcode等库的使用,对比不同标准如PDF417、DM和QRCode的特性,重点讲解QRCode的纠错能力和生成流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Java 生成二维码

二维码是指用某种特定的几何图形按一定规律在平面分布的黑白相间的图形记录数据符号信息的图形。

优点

  • 高密度编码,信息容量大
  • 编码范围广
  • 容错能力强
  • 译码可靠性高
  • 可引入加密措施
  • 成本低,易制作,持久耐用

缺点

  • 成为手机病毒、钓鱼网站传播的新渠道
  • 信息泄露

二维码标准

  1. PDF417:不支持中文
  2. DM:专利未公开,需支付专利费用
  3. QR Code:专利公开,支持中文

QR Code(Quick Response Code) 相较于其他标准的二维码,具有识读速度快、数据密度大、占用空间小的优势。

QR Code 的纠错能力

  • L 级:约可纠错7%的数据码字
  • M 级:约可纠错15%的数据码字
  • Q 级:约可纠错25%的数据码字
  • H 级:约可纠错30%的数据码字

生成方法

1 zxing

  • 引入依赖
    implementation 'com.google.zxing:core:3.3.3'
    implementation 'com.google.zxing:javase:3.3.3'
  • 创建二维码方法
    public static void createQRCodeByZXing(String format, String content, Path file) {
        Map<EncodeHintType, Object> hint = new HashMap<>(4);
        hint.put(EncodeHintType.CHARACTER_SET, "utf-8");
        hint.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        hint.put(EncodeHintType.MARGIN, 2);

        try {
            BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 500, 500, hint);
            MatrixToImageWriter.writeToPath(bitMatrix, format, file);
        } catch (WriterException | IOException e) {
            e.printStackTrace();
        }
    }
  • 测试方法
public class QrCodeUtilsTest {
    private static final String format = "png";
    private static final String content = "https://github.com/leifchen";

    @Test
    public void createQRCodeByZXing() {
        Path file = new File("E:/zxing.png").toPath();
        QrCodeUtils.createQRCodeByZXing(format, content, file);
    }
}
  • 二维码图片
    zxing

2 qrcode

    implementation fileTree(dir:'lib',include:['*.jar'])
  • 创建二维码方法
   public static void createQRCode(String format, String content, File file) {
        Qrcode qrcode = new Qrcode();
        qrcode.setQrcodeErrorCorrect('M');
        qrcode.setQrcodeEncodeMode('B');
        qrcode.setQrcodeVersion(10);
        int width = 67 + 12 * (10 - 1);
        int height = 67 + 12 * (10 - 1);
        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics = bufferedImage.createGraphics();
        graphics.setBackground(Color.WHITE);
        graphics.setColor(Color.BLACK);
        graphics.clearRect(0, 0, WIDTH, HEIGHT);

        // 偏移量
        int offset = 2;
        byte[] bytes = new byte[0];

        try {
            bytes = content.getBytes("gb2312");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        if (bytes.length > 0 && bytes.length < 120) {
            boolean[][] s = qrcode.calQrcode(bytes);

            for (int i = 0; i < s.length; i++) {
                for (int j = 0; j < s.length; j++) {
                    if (s[j][i]) {
                        graphics.fillRect(j * 3 + offset, i * 3 + offset, 3, 3);
                    }
                }
            }
        }

        graphics.dispose();
        bufferedImage.flush();
        try {
            ImageIO.write(bufferedImage, format, file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
  • 测试方法
public class QrCodeUtilsTest {

    private static final String format = "png";
    private static final String content = "https://github.com/leifchen";

    @Test
    public void createQRCode() {
        File file = new File("E:/qrcode.png");
        QrCodeUtils.createQRCode(format, content, file);
    }
}
  • 二维码图片
    qrcode

3 jquery-qrcode

  • 创建二维码方法
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>QR-Code</title>
    <script src="js/jquery.min.js"></script>
    <script src="js/jquery.qrcode.min.js"></script>
</head>
<body>
<div id="qrcode"></div>

<script>
    $('#qrcode').qrcode({width: 500, height: 500, text: "https://github.com/leifchen"});
</script>
</body>
</html>
  • 二维码图片
    jquery-qrcode

参考

  1. GitHub
  2. Java生成二维码
  3. zxing
  4. jquery-qrcode
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值