Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Fetching latest commit…

Cannot retrieve the latest commit at this time

..
Failed to load latest commit information.
lib
test
README.textile

README.textile

Decode QR Codes

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 library with JRuby. ZXing conveniently decodes a plethora of barcodes. Their site has a complete list.

Requirements

  • JRuby (tested with 1.4.0)
  • shoulda (for testing)

Using the ZXing module/singleton.

  
    require 'zxing'

    # You can decode a URL...
    ZXing.decode 'http://2d-code.co.uk/images/bbc-logo-in-qr-code.gif'

    # ... or a file path...
    ZXing.decode '/Users/ecin/qrcode.png'

    # ... or a File object...
    ZXing.decode File.open('qrcode.png')

    # ... or anything that returns a URL or file path when #path or #to_s 
    # is called on it.
    class Image
      attr_reader :path
      def initialize(path); @path = path end
    end

    image = Image.new('qrcode.png')
    ZXing.decode image
      
    # #decode returns nil if it can't decode the image.
    ZXing.decode 'image_without_a_code.png'
    # => nil

    # #decode! will raise an error if it can't decode the image.
    ZXing.decode! 'image_without_a_code.png'
    # => NativeException

    # Feel free to include ZXing to shorten the call.
    include ZXing

    decode 'qrcode.png'
  

Including the Decodable module.

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.

  
     require 'zxing/decodable'

     class File
       include Decodable
     end

     file = File.open('qrcode.png')
     file.decode
   
Something went wrong with that request. Please try again.