Barby

Barcodes for Ruby

Code 128 B

Barby is a Ruby barcode generator that doesn't rely on 3rd party libraries - it's all in Ruby. Of course, if you want it to do something useful - like generate images - you'll probably want to use an external, non-Ruby library for that. But the most central part (the barcode part) is pure Ruby.

Barby is structured to make it highly extensible; it's easy to add or change the parts you need to make it work for you. There are two main components: there is the part responsible for generating the barcodes themselves, including all logic such as checksums and conversion of characters into "bars and spaces". The other main part is the one responsible for generating graphical (or other) representations of these barcodes. Separating these two makes it easy to add to one without affecting the other.

Let me show you how this works:

require 'barby'
require 'barby/outputter/png_outputter'

barcode = Barby::Code128B.new('The noise of mankind has become too much')
File.open('code128b.png'){|f|
  f.write barcode.to_png(:height => 20, :margin => 5)
}
Code 128 B example

In this example, a Code128 barcode of type B is created. It then uses an "outputter" which uses the "png" gem to generate a PNG representation of the barcode and writes this to a file, code128b.png. If you wanted to use a Code39 barcode instead, you'd just replace the barcode part:

require 'barby'
require 'barby/outputter/png_outputter'

barcode = Barby::Code39.new('I am losing sleep over their racket', true)
File.open('code39.png'){|f|
  f.write barcode.to_png
}
Code 39 example

Or if you want to create a GIF instead, the RmagickOutputter has that functionality:

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

barcode = Barby::Code128B.new('Give the order that suruppu-disease shall break out')
File.open('code128B.gif'){|f|
  f.write barcode.to_gif
}
Code 128B example