Create Contact Event based QR Code in Java

In this tutorial, we will explain how to create contact event based QR Code in Java. The generation of such a QR code requires us to provide input information either in vCard or MECARD format to the code generating module. Both these formats are different to each other and we will provide example program for both these cases. This tutorial is focussed on producing a QR code using MECARD format that will hold the information about a contact as listed below:
  • First Name and Last Name
  • Company Name
  • Address information that includes Address, Street Address, Address Line 2, City, State, Country
  • Phone number
  • Website details
  • Email address
  • Notes

We will explain with an example for each of the scenarios below:
  • How to generate in pure Java code - outputting PDF and PNG image files.
  • How to generate the code through HTTP servlet – generating PDF and PNG image output based on user inputs captured through a form.
Once a code is generated, you can use it for a variety of purposes directly on your mobile. Some of these include
  • Creating a contact in your mobile
  • Showing a map with the address
  • Direct dialling the telephone number
  • Sending an email to the contact
  • Copy the contact
  • Share contact details / Edit / View More information
To do this, you need a QR code reader like QR Droid. Java implementation of the contact event QR code is provided below
 1 import java.io.FileOutputStream; 
 2 import com.itextpdf.text.Document;
 3 import com.itextpdf.text.pdf.PdfWriter;
 4 import com.itextpdf.text.Paragraph; 
 5 import com.itextpdf.text.pdf.BarcodeQRCode;
 6 import com.itextpdf.text.Rectangle;
 7 import javax.imageio.ImageIO;
 8 import java.io.File;
 9 import java.io.IOException;
10 import java.awt.image.BufferedImage;
11 import java.awt.Color;
12 import java.awt.Image;
13 import javax.media.jai.operator.AWTImageDescriptor; //Java advanced Imaging library
14 import javax.media.jai.RenderedOp; // Java advanced imaging library
15 class QRCodeContact {
16    public static void main (String[] args) throws Exception
17    {
18         //Define the contact event in Java Code
19         //Note that we will use MECARD format to generate the QR Code. 
20         //MECARD event details logged below
21         String header_info="MECARD:";
22         //Need to have a semi-colon as separator at the end.
23         String Contact_name="N:Thinktibits"+";"; //N: is the prefix for MECARD Name
24         String Contact_company="ORG:MyCompany"+";"; //ORG: is the prefix for company
25         String phone_number="TEL:123456"+";";//TEL: is the prefix for telephone number
26         String website="URL:thinktibits.blogspot.com"+";";//URL: is the prefix for website
27         String contact_email="EMAIL:123@abc.com"+";";//EMAIL: is the prefix for email address
28         String address="ADR:Unit Street Address"+";";//ADR: is the prefix for address
29         String notes="NOTE:General Contact notes"+";";//NOTE: is the prefix for notes.
30         //Construct one final contact string in MECARD format
31         String final_mecard=String.format("%s%s%s%s%s%s%s%s", header_info,Contact_name, Contact_company, phone_number, website,contact_email,address,notes);
32         //Create Contact event QR Code
33         BarcodeQRCode my_code = new BarcodeQRCode(final_mecard, 1, 1, null);
34         /* Add QR Code to the PDF document */
35         Document qr_code_contact_Example = new Document(new Rectangle(360, 852));
36         PdfWriter writer = PdfWriter.getInstance(qr_code_contact_Example, new FileOutputStream("My_contact_Info.pdf"));
37         qr_code_contact_Example.open();            
38         qr_code_contact_Example.add(new Paragraph("Tutorial to create QR Code for Contact Event in Java Using iText Library"));        
39         qr_code_contact_Example.add(my_code.getImage());        
40         qr_code_contact_Example.close();
41         /* Create QR Code as a PNG Image file */
42         Image qr_awt_image = my_code.createAwtImage(Color.BLACK,Color.WHITE);        
43         AWTImageDescriptor converter=new AWTImageDescriptor();
44         try {           
45         ImageIO.write(converter.create(qr_awt_image,null), "png",new File("QRCodeImage.png"));        
46         }
47          catch (IOException e) {
48                 e.printStackTrace();
49         }
50    }
51  }
A HTTP Servlet Implementation guide of the contact event QR code is provided below:
 1 import java.io.IOException;
 2 import java.io.OutputStream;
 3 import javax.servlet.ServletException;
 4 import javax.servlet.http.HttpServlet;
 5 import javax.servlet.http.HttpServletRequest;
 6 import javax.servlet.http.HttpServletResponse;
 7 import java.io.FileOutputStream; 
 8 import javax.imageio.ImageIO;
 9 import java.net.URL;
10 import java.awt.image.BufferedImage;
11 import com.itextpdf.text.pdf.BarcodeQRCode;
12 import com.itextpdf.text.Document;
13 import com.itextpdf.text.Paragraph;
14 import com.itextpdf.text.pdf.PdfWriter;
15 import com.itextpdf.text.DocumentException;
16 import java.awt.Color;
17 import java.lang.Integer;
18 import java.awt.Image;
19 import javax.media.jai.operator.AWTImageDescriptor; 
20 import javax.media.jai.RenderedOp;
21 
22 public class QRCodeContactServlet extends HttpServlet {
23 public QRCodeContactServlet() {
24 /* No code here */
25 }
26 public void doGet(HttpServletRequest request, HttpServletResponse response)
27 throws ServletException, IOException {
28         OutputStream out = response.getOutputStream(); /* Get the output stream from the response object */
29         try {
30         int selectedItem;
31         //Find out if the QR code needs to be generated as a PDF file or a PNG image    
32         selectedItem=Integer.parseInt(request.getParameter("element_8"));
33                 
34         // Get Contact Event Attributes 
35         String header_info="MECARD:";
36         String Contact_name="N:"+request.getParameter("element_1_1")+request.getParameter("element_1_2")+";"; //N: is the prefix for MECARD Name
37         String Contact_company="ORG:"+request.getParameter("element_7")+";"; //ORG: is the prefix for company
38         String phone_number="TEL:"+request.getParameter("element_3_1")+request.getParameter("element_3_2")+request.getParameter("element_3_3")+";";//TEL: is the prefix for telephone number
39         String website="URL:"+request.getParameter("element_4")+";";//URL: is the prefix for website
40         String contact_email="EMAIL:"+request.getParameter("element_5")+";";//EMAIL: is the prefix for email address
41         String address="ADR:"+request.getParameter("element_2_1")+request.getParameter("element_2_2")+request.getParameter("element_2_3")+request.getParameter("element_2_4")+request.getParameter("element_2_5")+request.getParameter("element_2_6")+";";//ADR: is the prefix for address
42         String notes="NOTE:"+request.getParameter("element_6")+";";//NOTE: is the prefix for notes.
43         //Construct one final contact string in MECARD format
44         String final_mecard=String.format("%s%s%s%s%s%s%s%s", header_info,Contact_name, Contact_company, phone_number, website,contact_email,address,notes);
45         
46         //Invoke BarcodeQRCode class to create QR Code for the calendar
47         BarcodeQRCode my_code = new BarcodeQRCode(final_mecard, 1, 1, null);
48         
49         //PDF option is selected. Generate QR code into a PDF document and send this response to the output.
50         if (selectedItem==1) {
51                 response.setContentType("application/pdf");
52                 Document document = new Document();            
53                 PdfWriter.getInstance(document, out);
54                 document.open();
55                 document.add(new Paragraph("QR Code - Contact - Java Servlet Example - PDF generation "));              
56                 document.add(my_code.getImage());
57                 document.close();
58         }
59         
60         //PNG image is selected. Generate QR Code as PNG image in Java and send this response to the output.
61         if (selectedItem==2) {
62         response.setContentType("image/png"); /* Set the HTTP Response Type */
63         java.awt.Image qr_awt_image = my_code.createAwtImage(Color.BLACK,Color.WHITE);
64         AWTImageDescriptor converter=new AWTImageDescriptor();
65         ImageIO.write(converter.create(qr_awt_image,null), "png",out);
66         }       
67                 
68         }
69         catch (Exception e) {
70                 System.err.println(e.toString()); /* Throw exceptions to log files */
71         }
72         finally {
73                 out.close();/* Close the output stream */
74         }
75         }
76 
77 public void doPost(HttpServletRequest request,
78                      HttpServletResponse response)
79       throws ServletException, IOException {
80     doGet(request, response);
81   }
82 }  
You need a form to use the servlet code, which you can download from the location below:
A sample image of the QR code generated by this tutorial, in a QR code reader is provided below:
Contact event based QR Code generation in Java
Contact event based QR Code generation in Java

Most of the code provided here are already explained clearly in our tutorial that explained how to create calendar event based QR codes. You can find them there if you are looking for a detailed explanation.

No comments:

Post a Comment