Awkward and Proud

QR Codes in Ruby

QR codes seem to be everywhere these days. I’ve seen them in advertisements on the train, on the back of magazines, and, most recently, on someone’s wedding invitation. A lot of folks are using QR codes as a quick way to share URLs. But that’s not all that they are good for. We recently had a client that needed to authorize paper tickets, and a QR-encoded token turned out to be a great solution.

QR Code Generation in Ruby

From my experience, there are two solid options for QR code generation in Ruby:

  • rQRCode Pure ruby QR code generator
  • qrencoder A Ruby wrapper around libqrencode

rQRCode

  • Pure ruby
  • Portable: will run on many Ruby platforms
  • Few dependencies
  • Relatively slow

QREncoder

  • Very, very fast
  • Uses a C library (libqrencode) that works wherever make does
  • Likely not to run on JRuby

QR Code Decoding in Ruby

The QR Decoding landscape varies even more than its QR encoding counterpart. There are several ooptions to choose from, and the best one for you will likely depend your environment and stack. The top three libraries that I’ve found are:

  • qrio Pure ruby QR Code decoder
  • zxing.rb JRuby wrapper for ZXing 1D/2D barcode image processing library.
  • qrdecoder A ruby wrapper for the C++ port of ZXing

Qrio

  • Pure ruby
  • Source is readable for ruby developers
  • Has intermediate hooks for displaying pattern detection
  • Only reads in PNG formatted images
  • Slower than the other options

Zxing.rb

  • Uses the well-maintained Zxing (Zebra Crossing) Java barcode decoding library
  • Works with both JRuby and MRI (via jruby-jars gem)
  • MRI versions of ruby will notice a slow startup as a forked JRuby server is started

QRDecoder

  • Wrapper for libzxing, a C++ port of the ZXing Java library
  • By far the fastest option for MRI
  • The C++ port of ZXing is less maintained than its big brother
  • You must build libzxing from source (a homebrew package is pending)

The Bottom Line

Wherein I offer “My Two Cents”. If you’re on a stack that can install libraries and you’re using an MRI ruby (1.8 or 1.9), use qrencoder and qrdecoder. They are both very fast and awesome. Additionally, their APIs complement each other. If you’re on Heroku, use rQRCode and qrio. They’re both pure ruby and play nice with an environment where you can’t install you’re own libraries. If you’re on JRuby, use Zxing.rb for decoding, and try both rQRCode and qrencoder. I haven’t tried qrencoder with JRuby–or any compiled gems for that matter–so your mileage may vary.

N.B. In the interest of full disclosure, I have contributed to both qrencoder and zxing.rb, and maintain qrdecoder, and tend to favor them. Nevertheless, the pure ruby options are solid and well worth a look.

Regardless of what your needs are, there are lots of options. Which one fits your needs best will largely depend on your stack.