Smita Ahinave
Smita Ahinave

Reputation: 1888

QR Code in BIRT

I want to generate QR code in BIRT . Before generating report I will enter number of labels as an input say x,then report should contain x labels along with QR code.QR code data is dynamic and based on input.I searched alot on web but not proper solution found.I want report like below

Upvotes: 4

Views: 7599

Answers (2)

Xavier Lambros
Xavier Lambros

Reputation: 876

To complete Dominique post, for beginners like me.

  1. Create a Java Project

Download core and javase jars on ZXing Maven repository and add them as dependencies (or create a maven project...).

  1. Java Project : Add the following class
package your.package.path;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

public class QRCodeGenerator {

    /**
     * Method that generate a qrcode as byte array which could be consumed by a birt image item
     * @param text
     * @param width (pixels)
     * @param height (pixels)
     * @return
     * @throws WriterException
     * @throws IOException
     */
    public static byte[] getQRCodeImage(String text, int width, int height) throws WriterException, IOException {
        // No margin around the code
        Map<EncodeHintType, Integer> hints = new HashMap<>();
        hints.put(EncodeHintType.MARGIN, 0);

        // Use the code you want, here QR_CODE
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height, hints);

        // Generate the PNG which include the code
        ByteArrayOutputStream pngOutputStream = new ByteArrayOutputStream();
        MatrixToImageWriter.writeToStream(bitMatrix, "PNG", pngOutputStream);

        // Return the PNG as byte array
        return pngOutputStream.toByteArray();
    }

}
  1. Export your Java Project as a jar and add it to your Birt Project

Right click on birt project -> Properties -> Report Design -> Classpath -> Add External JARs (or Add Projects if you prefer)

  1. Birt Project : Create a Variable parameter "qrCode"

  2. Birt Project : Add following script on a onCreate event (after loading your text and before generating your image)

Use your java project path (so change your.package.path by your).

    importPackage(Packages.your.package.path);

    // We call the Java method and get PNG bytes that we put inside qdCode variable and that will be consumed by the dynamic image
    vars["qrCode"] =  QRCodeGenerator.getQRCodeImage(this.getRowData().getColumnValue("YourTextVariableName"), 220, 220); 
  1. Birt Project : Add an image to your layout

Right click on it -> Edit, check Dynamic image radio button and add vars["qrCode"] in the textbox.

If you want to test your report on Birt Viewer War, don't forget to add your Java Project jar and ZXing jars to WEB-INF/lib folder.

Upvotes: 3

Dominique
Dominique

Reputation: 4332

The QRCode itself can be created using ZXing library using for example this tutorial.

In this example a small generator using zxing is developed to keep scripts as simple as possible but this is facultative, you could put all the java stuff directly into BIRT scripts. Here is basically what this script looks like:

importPackage(Packages.java.awt);
importPackage(Packages.org.my.package.using.zxing);

var url="Generate a URL or a message with data bindings, report parameters etc.";
try{
  vars["QRCode"]=QRcodeGenerator.getImage64QR(url, Color(params["QRColor"].value), Color.WHITE,params["QRSize"].value); 
}catch(e){
  vars["QRexception"]=e.message;
}

In your case a similar script could be put in a data binding of a dynamic image, or in a onCreate event, etc. At this point there are two options:

  • Generate a different temporary .png or .jpg for each dataset row, and set a native BIRT image element with an URI expression returning this temporary file
  • Generate a base64 image for each dataset row and embed it in a Dynamic image element or a HTML text element

The example is using the second approach with a HTML text element and an expression such:

<img alt="This QRCode can't be displayed" src='<VALUE-OF>vars["QRCode"]</VALUE-OF>'/>

Upvotes: 4

Related Questions