Project

zxing

No commit activity in last 3 years
No release in over 3 years
Need to decode 1D/2D bars and don't mind using JRuby to help with the task? ZXing is the wrapper for you!
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 0.9.2
~> 2.5.0

Runtime

 Project Readme

ZXing¶ ↑

Decode QR codes (and other 1D/2D barcode formats)

QRcode 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.

Installation¶ ↑

gem install zxing

Requirements¶ ↑

  • JRuby (tested with 1.5.6), OR

  • MRI (1.8.7 and 1.9.2) with jruby-jars gem

Usage¶ ↑

require 'zxing'

# Pass a path to a file, and it will return the characters encoded within
# the barcode image.  For example, if a QRCode image has the text "QRcode
# string" embedded:
ZXing.decode '/Users/ecin/qrcode.png' #=> "QRcode string"

# ZXing#decode_all returns an array of encoded values.  For example if an
# image has a barcode encoded with the value "test123" and another barcode
# encoded with the value "test456":
ZXing.decode_all 'image_with_multiple_barcodes.png' #=> ["test456", "test123"]

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

# or an instance of File
ZXing.decode File.new('qrcode.png')

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

# ZXing#decode returns nil if it can't decode the image.
ZXing.decode 'image_without_a_code.png' #=> nil

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

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