Skip to content

Commit

Permalink
complete revamp of barcode generation, using gbarcode library. this a…
Browse files Browse the repository at this point in the history
…llows for barcode generation in most of the prevelant formats.

also provides for more control over the barcode image and its formatting.
  • Loading branch information
anujluthra committed Jul 23, 2007
1 parent 178455d commit 6b41ddd
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 57 deletions.
56 changes: 32 additions & 24 deletions barcode_generator/README
Original file line number Diff line number Diff line change
@@ -1,35 +1,43 @@
BarcodeGenerator
================
Author : Anuj Luthra
Barcode generator makes generating/displaying bar-codes for certain alphanumeric ids a piece of cake. It uses free true-type barcode (code39) fonts and RMagick to create barcode images.
Barcode generator makes generating/displaying bar-codes for certain
alphanumeric ids a piece of cake.
It uses Gbarcode for encoding barcode data and then rmagick to generate
images out of it for displaying in views.

This way we can generate any barcode type which Gbarcode -> Gnome Barcode project
supports.

USAGE:

its as simple as saying
<%= barcode 'FJJ4JD'%>
and it will generate a barcode for FJJ4JD and include it in the view.
the images are stored in /public/images/barcodes subdir.

Installation:
install from svn : http://barcode-generator.googlecode.com/svn/trunk/
and then run the rake task (rake barcode_setup) to create required dirs and installing the fonts ttf files in public dir.

Barcode font (cod39) is a freeware and has been source from : http://www.barcodesinc.com/free-barcode-font/
according to its author Matthew Welch :
----------------------------------------------------------------------
Free 3 of 9, like all of my fonts, is free. You can use it for most
personal or business uses you'd like, and I ask for no money. I
would, however, like to hear from you. If you use my fonts for
something please send me a postcard or e-mail letting me know how
you used it. Send me a copy if you can or let me know where I can
find your work.
This will generate a barcode for FJJ4JD in BARCODE_39 format with default width
and height and include it in the view.
the images are stored in /public/images/barcodes subdir.

You may use this font for graphical or printed work, but you may not
sell it or include it in a collection of fonts (on CD or otherwise)
being sold. You can redistribute this font as long as you charge
nothing to receive it. If you redistribute it include this text file
with it as is (without modifications).
Options Options Options ..

If you use this font for commercial purposes please credit me in
at least some little way.
----------------------------------------------------------------------
to customize your barcodes, you can optionally pass following information in your views
- encoding_format (Gbarcode constants for eg. Gbarcode::BARCODE_128 etc..)
- width
- height
- scaling_factor
- xoff
- yoff
- margin

in this case your view will look like :
<%= barcode 'ANUJ', :height => 100,
:width => 400,
:margin => 100,
:xoff => 20,
:yoff => 40
%>

Installation:
install from svn : http://barcode-generator.googlecode.com/svn/trunk/
make sure that you install gems for rmagick and gbarcode first!!!
and then run the rake task : rake barcode_setup
2 changes: 2 additions & 0 deletions barcode_generator/init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
require 'rubygems'
gem 'rmagick'
require 'RMagick'
require 'base64'
require 'gbarcode'
require 'config'
require 'barcode_generator'
44 changes: 34 additions & 10 deletions barcode_generator/lib/barcode_generator.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,47 @@
# BarcodeGenerator
# uses Gbarcode for encoding barcode data and then rmagick to generate
# images out of it for displaying in views.

# i was using clumsy file.new and file.close for eps generation, but
# Adam Feuer had published a much elegant way by using IO.pipe for doing
# same thing. (never occured to me !! :-P )

# this way we can generate any barcode type which Gbarcode -> Gnome Barcode project
# supports.

# Extending <tt>ActionView::Base</tt> to support rendering themes

module ActionView

# Extending <tt>ActionView::Base</tt> to support rendering themes
class Base
def barcode id
def barcode(id, options = {:encoding_format => DEFAULT_ENCODING })
id.upcase!
normalized_id = ASTERISKIZE ? "*#{id}*" : id
canvas = Magick::ImageList.new
canvas.new_image(BARCODE_COLUMNS, BARCODE_ROWS)
text = Magick::Draw.new
text.font = ("#{RAILS_ROOT}/public/fonts/FRE3OF9X.TTF")
text.pointsize = BARCODE_POINTSIZE
text.fill = BARCODE_COLOR
text.gravity = Magick::CenterGravity
text.annotate(canvas, 0,0,2,2, normalized_id)
canvas.write("#{RAILS_ROOT}/public/images/barcodes/#{id}.png")

#generate the barcode object
options[:encoding_format] = DEFAULT_ENCODING unless options[:encoding_format]
bc = Gbarcode.barcode_create(normalized_id)
bc.width = options[:width] if options[:width]
bc.height = options[:height] if options[:height]
bc.scalef = options[:scaling_factor] if options[:scaling_factor]
bc.xoff = options[:xoff] if options[:xoff]
bc.yoff = options[:yoff] if options[:yoff]
bc.margin = options[:margin] if options[:margin]

#encode the barcode object in desired format
Gbarcode.barcode_encode(bc, options[:encoding_format])
read_pipe, write_pipe = IO.pipe
Gbarcode.barcode_print(bc, write_pipe, Gbarcode::BARCODE_OUT_EPS)
write_pipe.close()
#convert the eps to png image with help of rmagick
eps = read_pipe.read()
im = Magick::Image::read_inline(Base64.b64encode(eps)).first
im.format = DEFAULT_FORMAT
#block ensures that the file is closed after write operation
File.open("#{RAILS_ROOT}/public/images/barcodes/#{id}.png",'w') do |image|
im.write(image)
end
return image_tag("barcodes/#{id}.png")
end
end
Expand Down
7 changes: 2 additions & 5 deletions barcode_generator/lib/config.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@

BARCODE_COLUMNS = 200
BARCODE_ROWS = 100
BARCODE_POINTSIZE = 72
BARCODE_COLOR = 'black'
ASTERISKIZE = true
DEFAULT_ENCODING = Gbarcode::BARCODE_39
DEFAULT_FORMAT = 'PNG'
11 changes: 1 addition & 10 deletions barcode_generator/tasks/barcode_generator_tasks.rake
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
# desc "Explaining what the task does"
task :barcode_setup do
require "ftools"
Dir.mkdir("#{RAILS_ROOT}/public/images/barcodes", 0700)
dirc = "#{RAILS_ROOT}/public/fonts/"
Dir.mkdir( dirc , 0700 )
#opens/creates files
font_file = 'FRE3OF9X.TTF'
source = "#{RAILS_ROOT}/vendor/plugins/barcode_generator/fonts/#{font_file}"
target = "#{dirc}#{font_file}"

File.copy(source, target)
puts "fonts file installed in public/fonts dir"
puts "CREATED BARCODE DIR IN PUBLIC/IMAGES"
end
10 changes: 2 additions & 8 deletions barcode_generator/test/barcode_generator_test.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
require 'test/unit'

class BarcodeGeneratorTest < Test::Unit::TestCase
# Replace this with your real tests.

def test_can_read_font_file
assert File.exists?("#{RAILS_ROOT}/public/fonts/FRE3OF9X.TTF"), 'could not locate font file. Please run the rake task barcode_setup'
end

def test_can_create_barcode_image
image_path = ActionView::Base.barcode('test')
assert File.exists?("#{RAILS_ROOT}/public/images/#{image_path}"), 'barcode not generated'
end
assert(true)
end
end

0 comments on commit 6b41ddd

Please sign in to comment.