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:

    #!/usr/bin/perl
    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 plot.pl it could be used to generate a URL for our Perl Tips site with:

    ./plot.pl -o tips.png http://perltraining.com.au/tips/

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

http://search.cpan.org/perldoc?Imager::QRCode

ZXing home ZXing

http://code.google.com/p/zxing/

Imager documentation Imager - image manipulation in Perl at its best

http://search.cpan.org/perldoc?Imager

QRCode on Wikipeida QR Code overview

http://en.wikipedia.org/wiki/QR_code

[ Perl tips index ]
[ Subscribe to Perl tips ]


Upcoming courses

Location Course Course Date Duration Early Bird Date
Melbourne Programming Perl Mon 16 Aug 2010 5 days
Sydney Programming Perl Mon 6 Sep 2010 5 days Tue 3 Aug 2010
Canberra Programming Perl Mon 20 Sep 2010 5 days Tue 24 Aug 2010
Melbourne Enterprise Perl Mon 11 Oct 2010 5 days Fri 24 Sep 2010
Sydney Enterprise Perl Mon 25 Oct 2010 5 days Tue 21 Sep 2010
Canberra Enterprise Perl Mon 8 Nov 2010 5 days Tue 5 Oct 2010

For future dates, please see our training calendar.


This Perl tip and associated text is copyright Perl Training Australia. You may freely distribute this text so long as it is distributed in full with this Copyright noticed attached.

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

Email: contact@perltraining.com.au
Phone: 03 9354 6001 (Australia)
International: +61 3 9354 6001

Valid XHTML 1.0 Valid CSS