Apparently, we'll be seeing a lot of QR codes at YAPC::NA this year.
I don't have a smart phone, myself, but I can use zbarcam to read them with my netbook's webcam if the need arises. And just as zbarcam scans quick response codes (all sorts of bar codes, actually) from video sources, zbarimg scans files. Both of these are provided by ZBar, which we Debian-type folks (#!, Ubuntu, &c.) can install with
$ sudo apt-get install zbar-tools
If we point it at the image above, we get the URL out of it.
$ zbarimg yapc.png QR-Code:http://www.yapcna.org scanned 1 barcode symbols from 1 images in 0.04 seconds
So, how do we do that from Perl? Well, there is Barcode::ZBar, but--- holy cow--- is it ponderous!
#!/usr/bin/perl # http://sourceforge.net/apps/mediawiki/zbar/index.php?title=HOWTO:_Scan_images_using_the_API use v5.12; use warnings; use Barcode::ZBar; use Image::Magick; my $file = shift // 'oylenshpeegul.png'; # Use perlmagick to get the raw image data and put it in a # Barcode::ZBar::Image object. my $image = Barcode::ZBar::Image->new; { my $magick = Image::Magick->new; $magick->Read($file) and die; my $raw = $magick->ImageToBlob(magick => 'GRAY', depth => 8); $image->set_format('Y800'); $image->set_size($magick->Get(qw(columns rows))); $image->set_data($raw); } # Scan the image. # # my $scanner = Barcode::ZBar::ImageScanner->new; # $scanner->scan_image($image); # # The results are not returned nor are they in the ImageScanner object, # they're in the image object! # # So the ImageScanner object isn't really necessary...we're just # calling a subroutine. # Barcode::ZBar::ImageScanner->new->scan_image($image); foreach my $symbol ($image->get_symbols) { say $symbol->get_data if $symbol->get_type eq 'QR-Code'; }
Yes, this works
$ ./useBarcodeZBar.pl yapc.png http://www.yapcna.org
but--- gah!--- I wouldn't use those Perl bindings. In practice, I think I would just call zbarimg with qx
#!/usr/bin/env perl # simple alternative to useBarcodeZBar.pl use v5.14; use warnings; my $file = shift // 'oylenshpeegul.png'; my @zbar = `zbarimg $file 2>/dev/null`; die $? if $?; foreach (@zbar) { print if s/\AQR-Code://; }
Sometimes backticks are not a bad thing.
$ ./usezbarimg.pl yapc.png http://www.yapcna.org
Another alternative is Image::DecodeQR, which uses libdecodeqr to do the heavy lifting.
#!/usr/bin/env perl use v5.14; use warnings; use Image::DecodeQR; my $file = shift // 'oylenshpeegul.png'; my $string = Image::DecodeQR::decode($file); say $string;
Now that's more like it!
$ ./useImageDecodeQR.pl yapc.png http://www.yapcna.org
Heck, that's even suitable for one-liners!
$ perl -MImage::DecodeQR -E 'say Image::DecodeQR::decode(shift)' yapc.png http://www.yapcna.org
It might be interesting to see how ZBar and libdecodeqr fare when faced with sharpies and stickers and whatever else people do to their YAPC::NA badges.
Great summary of ways to read QR codes in Perl, but if you're needing to read QR codes in via your netbook webcam rather than just typing the URL on the keyboard, someone needs to be smacked hard for QR-abuse.
QR code URLs are a handy shortcut to save typing on phone keypads, NOT some mystic glyph to gate entry to only those who have smart phones.
Posted by: Illusori | 01/08/2012 at 12:12 AM
I agree. Indeed, I almost added "(but I'm guessing the need won't arise)" when I was typing that. However, all the chatter on the yapc mailing list suggests I may end up wanting to scan the various alterations people come up with, just to see how they work.
Posted by: oylenshpeegul | 01/08/2012 at 05:33 AM
Awesome post.Learn how to incorporate QR codes in your web apps to deliver quick information directly to your users' mobile device http://blog.caspio.com/web_apps/4-ways-to-use-qr-codes-in-your-web-apps/
Posted by: Account Deleted | 04/08/2012 at 08:35 PM