Fun with QR Codes and Perl

[ Perl tips index ]
[ Subscribe to Perl tips ]

Fun with QR Codes and Perl

These days it's hard to find a mobile phone that doesn't contain a camera of some sort. While great for taking conventional pictures, modern phones also have sufficient resolution, processing power, and programmability to read barcodes.

The most commonly seen barcode intended for mobile devices is the QR Code. This is an open format, two dimensional barcode with additional calibration features and error-correction, allowing data to still be retrieved from imperfect images.

QR Codes can be used to store text, URLs, contact information, or any other data. In this tip, we'll demonstrate how to create your own QR codes with Perl.

Choice of modules

Searching for 'QR' on the CPAN reveals a large number of modules to generate QR Codes. In these examples, we'll be using Imager::QRCode. We picked this module in particular because it's very easy to start using it, and because it leverages the existing and extremely well-written Imager library for Perl

To use Imager::QRCode, you must also have the libqrencode library installed.

Hello World Generating a barcode

Creating a QR code is easy! It's literally two lines of Perl!

    use Imager::QRCode;
    Imager::QRCode->new->plot('Hello World')->write(file => 'hello.png');

Because generating barcodes is so easy, it's simple to write a command-line utility:

    use strict;
    use warnings;
    use Getopt::Std;
    use Imager::QRCode;
    my %opts = (
        o => 'default.png',         # Output filename
        e => 'M',                   # Error correction level
        s => '5',                   # Pixel size
    getopt('o:e:s:', \%opts);
    my $qr = Imager::QRCode->new(
        size =>  $opts{s},
        level => $opts{e},
    $qr->plot("@ARGV")->write( file => $opts{o} );

Subscribe to Perl Tips Assuming the above was saved into a file named it could be used to generate a URL for our Perl Tips site with:

    ./ -o tips.png

Note that Imager is smart enough to produce output files based upon their extension, so using tips.jpg would produce a jpeg, and tips.gif would produce a GIF.

The -s switch controls the size of each pixel.

The -e switch allows control over error correction in the QR Code, and is one of: L (low), M (medium), Q (quality), or H (high), corresponding to 7%, 15%, 25% and 30% redundancy of information. Higher redundancy barcodes can still be read even if part of the image is damaged or obscured, but require more pixels to encode.

Reading a barcode

Reading a barcode from a smart-phone is also easy. On an Android based device, the Barcode Scanner can be found in the Applications/Shopping category. On the iPhone, the Barcodes application is available in the App Store.

For general scanning of barcodes, the ZXing (pronounced "zebra crossing") library is an open source, cross-platform, multi-platform scanning library written in Java.

More information

QRCode documentation Imager::QRCode documentation

ZXing home ZXing

Imager documentation Imager - image manipulation in Perl at its best

QRCode on Wikipeida QR Code overview

[ Perl tips index ]
[ Subscribe to Perl tips ]


If you have any questions please don't hesitate to contact us:

Phone: 03 9354 6001 (Australia)
International: +61 3 9354 6001

Valid XHTML 1.0 Valid CSS