Bar codes aren’t just for cereal boxes any more. 2-D barcodes like QR Codes are easily read by smart phones and are showing up everywhere from magazines to restaurant fronts to business cards. With some of the open-source libraries out there, it’s not hard to create your own QR Codes with java.
To generate a quick code for standard content, there are several online generators, like this. Here is a link to this site:
Urls, contacts, calendar events, even wifi network info are getting put into QR Codes. But you don’t have to stick to standard content or to provided generators. So how would you encode ‘hello world’ in a QR Code?
The good folks working on ZXing provide many tools for working with bar codes in java. The project does not seem to be in maven repositories, so to get started, download ZXing-1.6.zip from their site. I didn’t notice any built jars in there, but it’s easy to build from the source with ant or maven (build.xml and pom.xml files are provided).
Build the ‘core’ and ‘javase’ projects and put the jars into your classpath. Then create a string, use the zxing writer to encode it in a matrix, and save it to a png file:
Note that the string is encoded in ISO-8859-1. A lot of barcodes don’t support unicode. The resulting QR Code png file is:
Theoretically, thousands of characters can be encoded in QR Codes, but at least one source recommended not going above 800 characters or so. Here’s some text of about 700 characters:
Why would you encode a big blob of text in a barcode? Well, you probably wouldn’t. But there are plenty of useful scenarios for QR Codes and with more and more people buying barcode readers, aka smart phones, they’re only going to keep growing.