QR Barcode Generation with Ruby

Barcodes are the all-important middlemen of the modern retail experience, yet they remain almost completely invisible to consumers. Of course, the moment the flow breaks down and the code becomes unscannable, both purchaser and cashier quickly decide the whole thing is annoying and obsolete.

Nevertheless, barcodes are invaluable for passing around information. Although we usually associate them with products, they can actually encode any kind of data. Naturally enough, the more information you want to encode, the more dense the code graphic has to be, and the more you have to start worrying about the potential for slow and/or incorrect scanning. Most barcodes are the one-dimensional type you see on products and ISBNs, whose implementation dates back to 1949; they are widely supported but also prone to error. QR ("Quick Response") codes, created in 1994, are much more robust - they include error-correction (so you can smudge or obscure the code and it will still work), and are designed to be scanned very quickly.


Ruby users are fortunate to have several open-source options for generating QR codes. One well-known gem is rqrcode, which takes any string and converts it to an RQRCode::QRCode object. This object can then provide the encoded information using one of several methods, the simplest of which, to_s, just returns a string output that will give you a general idea of the look of the encoding.

require 'rubygems'
require 'rqrcode'

qr = RQRCode::QRCode.new( 'some data' )
puts qr.to_s  # Returns "xxxxxxx x x   xxx x xx x ... etc."

There are a few options for QRCode generation that you should be aware of - mainly, you may have to provide a :size depending on how much information you're trying to encode. If you try to create a QRCode with a very long string, for example, you may get a "code length overflow" error. In that case, you will have to specify a larger size (the default is 5):

RQRCode::QRCode.new( 'too long!' * 10 )
# => Code length overflow error!

RQRCode::QRCode.new( 'too long!' * 10, :size => 8 )
# => OK

You can also specify different levels of error correcting if need be (see the gem documentation for more info).

Of course, the codes aren't very useful as text - we need to be able to scan them! On the web, one option is to use CSS to style the text into an image-like view; the rqrcode docs include a sample stylesheet that does just that. But it's more likely that you'll need to make images from the codes, and that's where the rqrcode-rmagick plugin comes in. Just include it in your rails project (or manually require the library in your non-rails project), and it will mix image generation directly into the RQRCode::QRCode class - granted that you have rmagick included and working (a necessary evil).

From there, it's as simple as calling save_as to generate an image:

qr = RQRCode::QRCode.new('some data')
qr.save_as 'some_data_qr.png'

Optionally, you can call draw to create an in-memory RMagick image object.

Once your QR images are available for display, all that's left is to scan them in somewhere else and link it to your application! We'll leave the implementation of that up to you, but don't worry - there are lots of open source QR scanning utilities out there, including zxing, a library with ports for nearly every platform (including iPhone).

Now get encoding!