首页 > 解决方案 > QRcode 和 BarQRCode itextpdf

问题描述

我需要按照一些技术规范创建一个二维码,例如:二维码符号版本()、模块()、模块宽度()、ECC级别()和字符集()。我必须使用itextpdf库,而我获得的必须成为awt.Image

我尝试同时使用QRCodeBarcodeQRCode。使用 QRCode 我设置符号版本、模块、模块宽度和 ECC 级别。然后使用 BarcodeQRCode 设置字符集,我可以获得 awt.Image。

问题是我无法将 QRCode 传递给 BarcodeQRCode。你知道如何解决这个问题并使用这个库获得完整的二维码/图像吗?

这是我的代码:

       StringBuffer sb = new StringBuffer ();
    sb.append ( QRCODE_IDENTIFICATIVO );
    // other lines with the content of qrcode

    QRCode qrCode = new QRCode ();
    qrCode.setVersion ( versione );
    qrCode.at ( modulesWidth, modulesHeight );
    qrCode.setMatrixWidth ( modulesWidth );
    qrCode.setECLevel ( ErrorCorrectionLevel.M );

    Map<EncodeHintType, Object> qrParam = new HashMap<EncodeHintType, Object> ();
    qrParam.put ( EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M );
    qrParam.put ( EncodeHintType.CHARACTER_SET, "UTF-8" );

    BarcodeQRCode qrcode = new BarcodeQRCode ( sb.toString (), (int) mmToPt ( 30f ), (int) mmToPt ( 30f ), qrParam );
    return qrcode.createAwtImage ( Color.BLACK, Color.WHITE );

谢谢

标签: javaitextawtqr-codebarcode

解决方案


我明白如何解决这个问题。版本 4 是具有 33 个模块(或模块宽度 = 33)的版本。因此,当 barQRcode 初始化时,第二个和第三个参数设置模块的数量,因此,版本和模块宽度也设置。而 EncodeHintType 则包含有关字符和纠错的信息。这样,所有信息都可以在不使用二维码的情况下使用。这是:

 StringBuffer sb = new StringBuffer ();
sb.append ( QRCODE_IDENTIFICATIVO );
// other lines with the content of qrcode

Map<EncodeHintType, Object> qrParam = new HashMap<EncodeHintType, Object> ();
qrParam.put ( EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M );
qrParam.put ( EncodeHintType.CHARACTER_SET, "UTF-8" );

BarcodeQRCode qrcode = new BarcodeQRCode ( sb.toString (), 33, 33, qrParam );
return qrcode.createAwtImage ( Color.BLACK, Color.WHITE );

推荐阅读