Skip to content

trainline-eu/zxing.rb

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

72 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

# You can call ZXing#qrcode_decode if you only want to decode QR Code
ZXing.qrcode_decode '/Users/ecin/qrcode.png' #=> "QRcode string"

Configuration

You can set jruby server port in environment variables of your project, then you can make sure ZXing will only start one server instance on this port.

ZXING_PORT=4501

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

About

JRuby wrapper for ZXing 1D/2D barcode image processing library.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 100.0%