Barby

Barcodes for Ruby

Code 128 B

Barby is a barcode generator for Ruby.

It is highly flexible; 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/barcode/code_128'
require 'barby/outputter/png_outputter'

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

In this example, a Code128 barcode 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, code128.png. If you wanted to use a Code39 barcode instead, you'd just replace the barcode part:

require 'barby'
require 'barby/barcode/code_39'
require 'barby/outputter/png_outputter'

barcode = Barby::Code39.new('I am losing sleep over their racket', true)
File.open('code39.png', 'w'){|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/barcode/code_128'
require 'barby/outputter/rmagick_outputter'

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