Topic: Generating barcodes with rails

Hi,
I have tried barcode generator with gbarcode and it didn't work. my rails server hung up while converting. I would like to know if anyone can use this.

I've also tried someone's rails code that generates ean13 barcode. although it does generate the ean code, it can't not be scanned.(my barcode scanner works fine on books)

anyway...I also found a gem called barcode 0.2 by Clinton R. Nixon. I installed it, but I don't know how to apply that to rails app. can anyone teach me how?

here is it's Rdoc

class code39

new(text)
Takes a string, uppercases it, and wraps it in asterisks. If any character is unavailable in Code 39 encoding, throws a vicious error.

[Source]

# File barcode/code39.rb, line 70
   def initialize(text)
      text = text.upcase
      text.split(//).each do |char|
         if not @@encoding.has_key?(char)
            raise "Unencodable string."
         end
      end
     
      @text = "*#{text}*"
   end

Public Instance methods
to_img(filename, width, height)
The overly complicated image generation code. Your barcode image width and height should be in pixels. The actual barcode will not be that width, instead it will be a multiple of your text string (plus two asterisks - @text) length * 14. White space will center the rest.

[Source]

# File barcode/code39.rb, line 99
   def to_img(filename, width, height)
      bar_code_width = width - (width % ( @text.length * 14 ) )
      letter_width = bar_code_width / @text.length
      small_bar_width = letter_width / 14
      large_bar_width = ((7.0 / 3.0) * small_bar_width.to_f).to_i
     
      canvas = Image.new(width, height)
      gc = Draw.new
      gc.fill('black')
      gc.stroke('black')
      gc.stroke_width(-1)
      # Necessary to prevent massive blurry image nonsense.
      gc.stroke_antialias(false)
     
      cur_x = (width - bar_code_width) / 2
     
      self.to_s.split(//).each do |char|
         if char.upcase == "B"
            gc.rectangle(cur_x, 0, cur_x + (char == "B" ? large_bar_width : small_bar_width), height)
         end
         cur_x += char.match(/[BW]/) ? large_bar_width : small_bar_width
      end
      gc.draw(canvas)
      canvas.write(filename)
   end

to_s()
Outputs a string that represents the encoding. Not necessarily useful externally, but who knows?

[Source]

# File barcode/code39.rb, line 84
   def to_s
      output = String.new()
      @text.split(//).each do |char|
         output += @@encoding[char] + " "
      end
      return output
   end

Re: Generating barcodes with rails

have u tried barcode-generator plugin to generate barcode images in your rails project

http://code.google.com/p/barcode-generator

Last edited by anuj (2007-07-23 11:59:07)

Re: Generating barcodes with rails

i've totally redone my plugin, which now uses gbarcode and rmagick. Now you can generate barcode images in any format (png, jpg etc..) for any barcode encoding (code 39, code 128, EAN etc..)

check it out at
http://code.google.com/p/barcode-generator/

Last edited by anuj (2007-07-23 12:00:48)

Re: Generating barcodes with rails

you migh want to have a look at this solution:

http://www.java4less.com/barcodes/1D/ruby/index.shtml