Recently we have had two projects where we needed to generate barcodes based on the particular numbers that were stored by our Ruby on Rails application. To get things working we did the first thing that would come to anyone’s mind – search for a gem to do that. The problem is, there are not many gems that offer such funcionality. There few that we found were outdated (barcode-generator – last commit 3 years ago, rghost-barcode – last commit 3 years ago). But then we encountered barby – last commit 19 days ago.

What is barcode?

A barcode is a graphical representation of data (it could be numbers or words) shown as a combination of black and white stripes. There are many types of barcodes. These variations determine specific rules like size, number of characters or calculating checksum number (sourcewikipedia). However the most popular one is EAN13. You can find it on most products from your local store.

Generate my barcode!

I will show you how to generate and use barcodes in a rails application. I have created a basic app that you can find here. To use barby simply just add it to your gemfile.

gem 'barby'

There are a couple of ways to output the barcode. Some of them needs dependencies, but we’ll focus on them later. First we will try to generate a simple EAN13 barcode. To do this we’ll need some barby itself, plus some additional libraries. In the controller:

require 'barby'
require 'barby/barcode/EAN13'
require 'barby/outputter/ascii_outputter'

Besides barby, we need to specify which standard we want to use. Ean13 like I said before is present in most of the shop products we see every day. Moreover we need to specify and outputter. What is an outputter? It’s a kind of printer. Choosing an outputter can be understood as deciding how we are going to present our barcode. For starters, let’s use a simple ascii_outputter just to show the idea behind the generation.

I created a simple controller that will help u display barcodes. The corresponding action looks like this:

def home
  @barcode = Barby::EAN13.new('012345678912')
end

Now in our view, we can display the barcode. We need to add a css class to define a proper length in ascii to ensure it displays correctly.

.ascii=@barcode.to_ascii

Great success! We got our first barcode generated. Additionally it looks pretty geeky ;)

Unfortunately, code readers cannot read barcodes in ascii. We need to use a format that allows the kind lady working at our shop to ‘shoot’ it with the reader. There are many ways to do this thanks to a selection of barby outputters. First, let’s focus on showing the barcode in HTML format. Like before, we need to tell our app which outputter we want to use:

require 'barby/outputter/html_outputter'

Then in our controller action we need to get our code generated as EAN13 and pass it to the outputter:

def home
  @barcode = Barby::EAN13.new('000000000000')
  @barcode_for_html = Barby::HtmlOutputter.new(@barcode)
end

Then, we need to prepare the css for our barcode. The HTML outputter represents a barcode as a table. It has no style attached so if we do not add css, we won’t be able to see anything.

table.barby-barcode { border-spacing: 0; }
tr.barby-row {}
td.barby-cell { width: 3px; height: 100px; }
td.barby-cell.on { background: #000; }

And finally we can show the barcode in our view:

=raw @barcode_for_html.to_html

Do not forget to add raw before the variable to avoid rendering pure string in a view. Now we are getting somewhere.

This is a much more readable format, and even a phone application dediacted to reading barcodes, properly encoded our number. You may want to customize the html output – it can be easly done by css. Additionally you can write a number below the code. But if you do not want to style it up, and you just need to display it with little fuss, there are some helpful outputters. You can export your barcode directly to pdfs or images. On the website you can find all the avaliable formats of barcodes as well as a list outputters.

Conclusion

Barby is a great pack of tools that helps you create your own barcodes from numbers. However there is one thing that need to be said. Basic apps on mobile phones that read codes may vary from the professional code readers like on the picture below.

While working on a project, we tested the reading with a mobile phone app. Later on, we got this device and appeared to scan numbers in a different way. The difference was that the device calculated a checksum number at the end of the code and added it to the output. Basically it works like a keyboard. It reads the code and then hits enter at the end. So be sure to test yout barcodes with professional devices, especially when you use that kind of reader in your application.

 

Published: Jun 20th, 2014

Post tags:

Join our awesome team
Check offers

Work
with us

Tell us about your idea
and we will find a way
to make it happen.

Get estimate