Java QR Code Example Tutorial iText

In part 2 of the QR code series tutorial, we will run through a pure Java code example, (no PDF generation involved) to create a calendar event using QR Code. The code example that we will provide for the code is completely different to the one we discussed in part 1. Part 1 explained creating QR code in PDF document (calendar event) using iText library.

Let us get to Example -2 of this series.

Example -2: Java code example that creates a QR Code for a calendar event, and creates an image file back in response.

This illustration is a straightforward one. Instead of reinventing the wheel, we will continue to use the iText library for this purpose also. But, we will make the code smart by writing the output as a PNG image file directly instead of passing it to a PDF file. A bit of theory is involved in explaining how this works. Before I provide a step by step guide, let me run through the theory.

We will use "createAwtImage" method in the class "com.itextpdf.text.pdf.BarcodeQRCode" to create a RAW image that holds the QR code. The output image will be of type "java.awt.Image". The next challenge would be to create an image file from "java.awt.Image" object. To achieve this conversion, you need to have a copy of Java Advanced Imaging API from Oracle to work with the example. So, go to Oracle website and download the required JAR files. I'm using 1.1.3 version of the library to create the image file. The final output would be a PNG image file.We will use "AWTImageDescriptor" defined in the class "javax.media.jai.operator.AWTImageDescriptor" to convert the image of type "java.awt.Image" to "javax.media.jai.RenderedOp". We will then pass this output to "javax.imageio.ImageIO" and use ImageIO.write method to create the QR code PNG image.Though we use iText library in this approach, you can take that class as a standalone one and compile into your JAR file if required.

On the whole, this becomes a pure Java code example to create calendar event based QR code in Java. The complete working code sample is provided below


 1 import java.io.FileOutputStream; 
 2 import javax.imageio.ImageIO;
 3 import java.io.File;
 4 import java.io.IOException;
 5 import java.net.URL;
 6 import java.awt.image.BufferedImage;
 7 import com.itextpdf.text.pdf.BarcodeQRCode;
 8 import java.awt.Color;
 9 import java.awt.Image;
10 import javax.media.jai.operator.AWTImageDescriptor; //Java advanced Imaging library
11 import javax.media.jai.RenderedOp; // Java advanced imaging library
12 class QRCodeCalendarImage {
13    public static void main (String[] args) throws Exception
14    {
15         //Step -1: Define the calendar event first     
16         String begin="BEGIN:VEVENT";
17         String summary="SUMMARY:QR code for calendar event, pure Java Code";
18         String startdate="DTSTART;VALUE=DATE:20121109";
19         String enddate="DTEND;VALUE=DATE:20121111";
20         String location="LOCATION:Italy";
21         String description="DESCRIPTION:Java, iText, QR Code, Example Code";
22         String endevent="END:VEVENT";
23         //Step -2: Construct one final calendar string with new line separator
24         String finala=String.format("%s%n%s%n%s%n%s%n%s%n%s%n%s%n", begin, summary, startdate, enddate,location,description,endevent);
25         //Step -3: Invoke BarcodeQRCode class to create QR Code for the calendar
26         BarcodeQRCode my_code = new BarcodeQRCode(finala, 10, 10, null);
27         //Step-4: Read output QR Code calendar object into an image object of type java.awt.Image
28         Image qr_awt_image = my_code.createAwtImage(Color.BLACK,Color.WHITE);
29         //Step-5: Convert the RAW image into a PNG image using Oracle Advanced Imaging tool
30         AWTImageDescriptor converter=new AWTImageDescriptor();
31         try {
32         //Step -6: Use javax.imageio.ImageIO to write the converted image to the output         
33         ImageIO.write(converter.create(qr_awt_image,null), "png",new File("QRCodeImage.png"));
34         //Step-7: Done, we got the code we wanted!iText, Java advanced imageing API rocks.!
35         }
36          catch (IOException e) {
37                 e.printStackTrace();
38         }
39    }
40  }
This example is tested working and produces the following QR code PNG image in the output:
QR Code created with iText and Java Advanced Imaging  Library
QR Code created with iText and Java Advanced Imaging  Library
If you scan this QR Code with any supported reader, you will see a sample output as shown below:

QR Code - Scanned with a supported reader
QR Code - Scanned with a supported reader
Wow, that completes a Java implementation of QR Code by using supported libraries. We are now left with Part 3 of this tutorial, in which we will cover how to create a HTTP Servlet that will dump a QR code with input calendar events. We will also cover some advanced options in calendar like daylight savings, all day event etc in part 3 of this tutorial. Stay connected.


Like this tutorial? Share it..and spread the knowledge

No comments:

Post a Comment