Search code examples
javaitextawtqr-codebarcode

QRcode and BarQRCode itextpdf


I need to create a qrcode following some technical specifications, such as: qrcode symbol version(), modules(), modules width(), ECC level() and character set(). I have to use itextpdf library and what I obtain has to become and awt.Image.

I tried to use both QRCode and BarcodeQRCode. With QRCode I set symbol version, modules, modules width and ECC level. Then with BarcodeQRCode I set the characater set and I can obtain an awt.Image.

The problem is that I cannot pass the QRCode to BarcodeQRCode. Do you know how solve this problem and obtain a complete qrcode/image using this library?

This is my code:

       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 );

Thank you


Solution

  • I understood how solve the problem. The version 4 is the version with 33 modules (or with modules width=33). Therefore, when the barQRcode is initialized, the second and third parameters set the number of modules and, so, the version and the module width also. While the EncodeHintType has the information about the character and the error correction. In this way all the information are used without use the QRcode. It is:

     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 );