Encoding QR Codes using Ruby

I've recently been working on a project at the University of Bath (I'm a Software Developer in their e-Learning Team) that aims to increase mobile/handheld access to online resources by promoting the use of QR Codes.

Obviously, the first thing I needed was a QR Code generator and I set about creating one using rQRCode (a Ruby library for encoding QR Codes, developed by Duncan Robertson) and RMagick (a Ruby interface to the ImageMagick image processing libraries).

To output the QR Code as an image, I created a class, QRImage, that extends Magick::Image and then used the following code to output the image to a file:

# QR Code parameters
text = 'http://mattroper.co.uk/'
size = 3
level = 'h'
 
# Produce the QR Code
require 'rubygems'
require 'rqrcode'
qr = RQRCode::QRCode.new(text,  :size => size, :level => level)
 
# Create the image and double its size
require 'qr_image'
img = QRImage.new(qr).sample(2)
 
# Write image to disk
img.write('/home/matt/Desktop/qr.png') { quality = 100 }

Tags: , , , ,

One Response to “Encoding QR Codes using Ruby”

  1. sho Says:

    Ha, found your code too late - I’d already gone and written my own!

    One thing I noticed is that your code doesn’t seem to include a “quiet zone”, which is supposed to be a 4-module-width border around the data in the image. In practise, though, you can probably get away with a border of 1, especially if it’s black on white like it is 99% of the time.

    Anyway, thanks for the work : )

Leave a Reply