QR-Code and other barcodes

The bar code was first patented 57 years ago today, a fact unknown to me as I was putting the finishing touches on what I expected to be a fairly obscure post. So when articles started sprouting like mushrooms on the topic it seemed like a good time to get this finished. Maybe you've already had your fill of barcode stories today, but if you think you can handle one more then read on - after this one, a cup of tea and a lie down is definitely in order.

The PDF Library can create a number of Barcode symbols, both 1D and the more information-dense 2D formats. Your business may already be using barcodes as part of its workflow, but there are a few tricks you may not have thought of.

The code we're going to look at here is QR-code, which is ISO/IEC 18004:2006 - a public domain 2D symbology originally developed by Denso-wave in Japan. It's very popular there owing to it's ability to store Japanese characters, its very high information density, and the fact that many Japanese mobile phones have the ability to read QR-codes. It's common to see them on posters, in magazines and even on business cards, so a potential customer can photograph the code to import the information directly into their phone.

Since 2006 China Mobile has standardized on QR-code as well, which means a potential userbase of 500m people. Although adoption in the west is a little slower, I expect we'll be seeing more of QR-code.

Encoding Contact Details

Our first example shows how to work with contact details. NTT Docomo have defined a structured QR-Code for storing contact details - the full specification is (ed: link no longer exists in 2016). Called a "mecard" it's very similar to the industry standard vCard. Here's a quick demonstration we ran up to test this:

import java.io.*;
import org.faceless.pdf2.*;

public class TestQRCode {
  public static void main(String[] args) throws Exception {
    StringBuffer sb = new StringBuffer();
    sb.append("MECARD:");
    sb.append("N:Bremford,Mike;");
    sb.append("T:+442073497053;");
    sb.append("EMAIL:mike@bfo.com;");
    sb.append("ADR:,,132-134 Lots Road,London,,SW10 0RJ,UK;");
    sb.append("URL:http://bfo.com;");

    PDF pdf = new PDF();
    PDFPage page = pdf.newPage("A4");
    BarCode code = BarCode.newQRCode(sb.toString(), 1, 2, 0);

    PDFCanvas canvas = code.getCanvas();
    float w = canvas.getWidth();
    float h = canvas.getHeight();
    page.drawCanvas(canvas, 100, 600, 100+w, 600+h);
    pdf.render(new FileOutputStream("out.pdf"));
  }
}

We've chosen a unit size of 1 for the barcode, which creates quite a large image (you can see the PDF it creates here). This is because we're scanning the codes using the free QR app (ed: link no longer exists in 2016) iPhone application, so we're dependent on the fairly rubbish iPhone camera. Fire up this application and focus the camera on the code, and you'll be prompted to add my contact details to your phonebook.

Web addresses

A far simpler thing to encode is a website address - simply encode the URL to create a barcode that will jump to that page when scanned. To add a URL as a bookmark, format your text like so
MEBKM:TITLE:BFO Homepage;URL:http://bfo.com;;
If your barcodes are going to be scanned by ChinaMobile customers, they use their own variation on the format which looks like this
\u0001\u0010BM:SUB:BFO Homepage;URL:http://bfo.com;;

Form Contents

Adobe Acrobat allows the creation of "barcode fields" which reflect the contents of the document form. You can do a similar thing here, to make your paper based workflow much more streamlined. Acrobat stores the field names and values as tab-delimetered text, but the approach you take is up to you.

Links

There's an excellent QR-code generator here which focuses mainly on NTT and ChinaMobile's implementations - a similar chinese-language site is here LINK NO LONGER EXISTS - 2016. There's a library for decoding QR-code here.