Posts Tagged ‘qrcodes’

Encoding QR Codes using Ruby

Tuesday, April 1st, 2008

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 }