qr4r – Generate QRCode PNGs in Ruby

One of our ongoing projects has been using QRCodes as page identifiers on promo materials. Up to this point, we’ve been using online QRCode generators (of which there are plenty: QR-Code Generator, Create QR Code, Google Url Shortener to name a few).

We were tired of cutting and pasting urls. Could we build QRCodes for urls directly in the app? Some of the services mentioned above have APIs, which we could use to generate QRCode images. But instead of adding 3rd party web API calls to my app, I wanted to see how hard it’d be to own that QRCode generation logic.

A quick search revealed a few projects on github that all seem to start with rQRCode as a base. This gem generates the QRCode data, but not a png; it should be a great place to start. I also found a couple libraries that use rQRCode plus Chunky_PNG to construct PNGs. But, since we’ve done a bunch of work with ImageMagick and mojo_magick, I thought I might leverage those tools instead.

After releasing mojo_magick on rubygems, I built the qr4r gem. It uses rQRCode to generate the QRCode data, then some simple ImageMagick commands via mojo_magick to write out the image.

How can you use this awesomeness, you ask?

Add qr4r gem to your Gemfile
gem "qr4r"
Update your bundle
bundle install
Fire up a console and …

bundle exec irb
ruby> require 'qr4r'
ruby> Qr4r::encode('this is a qr code, yo', 'my_qrcode.png', :border => 10)

you get this:

Go get your QRCode on!

Docs/Code On Github: qr4r