Sneaky Abstractions

Subscribe to my Feed, follow me on , recommend me on Working With Rails or see my code on GitHub

Introducing Barby

Posted on March 25, 2008 11:33 Tagged with barby, barcode, ruby, gem.

Everyone, please say hello to Barby. She’s really into barcodes.

Barby was born out of frustration with the lack of a decent Ruby library for generating barcodes. There are a few of them out there, but they’re either very sparse on documentation and structure, probably having been made for a very specific purpose, or they depend on GNU Barcode. I was using gbarcode for Ruby until I hit a case where it couldn’t do what I needed it to. My choice then was to either try and shoehorn it into doing what I wanted or create my own. With my C-fu being as good as non-existent, I chose the latter and created Barby, which I like to think is a younger, more persuadable and flexible barcode generator.

Barby has support for several commonly used barcode symbologies, and is easily extended. The current list is:

  • Code128
    • GS1128 (aka EAN/UCC-128)
  • EAN-13 (aka UPC-A)
    • Bookland
    • EAN-8
  • Code39

You can add support for more by subclassing Barby::Barcode. The only requirement is that is has an encoding method which returns a string representation of the barcode consisting of 1s and 0s, 1 being a “bar” and 0 “space”. This encoding is used when creating graphical or other representations of the barcode. This process is separated into “outputters”, so you can easily add new ones. For example, the image above was created with the RMagickOutputter using the following code:

require 'barby'
require 'barby/outputter/rmagick_outputter'

barcode = Barby::Code128B.new('IM-IN-UR-BARCODE')

File.open('test.png', 'w') do |f|
  f.write barcode.to_png
end

The outputter classes included right now are RMagickOutputter, PDFWriterOutputter and ASCIIOutputter. These need to be required before use, as some might have “heavy” dependencies such as RMagick.

To install,

gem install barby

Barby lives in the Github Dream House. API docs can be found at Rubyforge.

Ice Ice Baby