3 p. "QR code":http://en.wikipedia.org/wiki/QR_Code generation is well served in the Ruby community, but decoding seems to be stuck in the Java world. This is an attempt to bridge the gap by wrapping the "ZXing":http://code.google.com/p/zxing/ library with JRuby. ZXing conveniently decodes a plethora of barcodes. Their site has a complete list.
7 * JRuby (tested with 1.4.0)
8 * shoulda (for testing)
10 h2. Using the ZXing module/singleton.
16 # You can decode a URL...
17 ZXing.decode 'http://2d-code.co.uk/images/bbc-logo-in-qr-code.gif'
19 # ... or a file path...
20 ZXing.decode '/Users/ecin/qrcode.png'
22 # ... or a File object...
23 ZXing.decode File.open('qrcode.png')
25 # ... or anything that returns a URL or file path when #path or #to_s
29 def initialize(path); @path = path end
32 image = Image.new('qrcode.png')
35 # #decode returns nil if it can't decode the image.
36 ZXing.decode 'image_without_a_code.png'
39 # #decode! will raise an error if it can't decode the image.
40 ZXing.decode! 'image_without_a_code.png'
43 # Feel free to include ZXing to shorten the call.
50 h2. Including the Decodable module.
52 p. A Decodable module is included (pun intended) to ease using the library with objects that return the URL or file path to decode when #path or #to_s is called.
56 require 'zxing/decodable'
62 file = File.open('qrcode.png')