Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Produce GIF or PNG barcode images from a Ruby on Rails application using RMagick and Gbarcode (See related posts)

This is a Ruby on Rails controller that produces PNG barcode images using RMagick (http://rmagick.rubyforge.org/) and Gbarcode (http://gbarcode.rubyforge.org/), the Ruby Gnu Barcode ( http://www.gnu.org/software/barcode/barcode.html ) wrapper. You will need to install RMagick and Gbarcode.

On Mac OS X, you can use the Locomotive RMagick bundle (http://locomotive.raaum.org/bundles/index.html) if you install the gbarcode gem into it first, for instance:

   1  
   2  % export GEM_HOME=/Applications/Locomotive2/Bundles/rmagickRailsMar2007_i386.locobundle/framework/lib/ruby/gems/1.8/
   3  % gem install gbarcode


To use it, save this code as barcode_controller.rb in the apps/controllers directory of your Rails application. Generate the barcodes using a URL like this:

http://localhost:3000/barcode/get_barcode_image?string=foo

This example uses the BARCODE_128 encoding; you can use different barcode encodings by adjusting the code. To produce PNG images, make sure you have have the PNG libraries installed on your system, and that your ImageMagick is compiled with PNG support, and then change 'image/gif' to 'image/png' and im.format = "GIF" to im.format = "PNG".

   1  
   2  # $RAILS_ROOT/apps/controllers/barcode_controller.rb
   3  class BarcodeController < ApplicationController
   4  
   5    def get_barcode_image
   6       string_to_encode = params[:string]
   7       barcode_image = BarcodeGenerator.get_barcode_image(string_to_encode)
   8       send_data(barcode_image, :type     => 'image/gif',
   9                                    :disposition => 'inline')
  10    end
  11  end


   1  
   2  # $RAILS_ROOT/app/helpers/barcode_generator.rb
   3  #
   4  # note: this will not work without rmagick (Ruby ImageMagick interface) and gbarcode (GNU barcode) 
   5  # gems installed. rmagick needs ImageMagick plus dependencies.
   6  
   7  class BarcodeGenerator
   8  
   9    # Uses subprocesses because 
  10    # 1. ImageMagick/RMagick leaks memory,
  11    #    and doesn't work in a long-running process. The fork makes it safe.
  12    # 2. The output from the Gbarcode and ImageMagick is often longer than the pipe buffer,
  13    #    so we have to empty the buffer from another subprocess
  14    def BarcodeGenerator.get_barcode_image(barcode_string)
  15      return BarcodeGenerator.get_subprocess_output do
  16                     barcode_generator = BarcodeGenerator.new
  17                     $stdout.write(barcode_generator.get_barcode_image(barcode_string))
  18      end
  19    end
  20  
  21    def initialize
  22      # we do the imports here to protect long-running processes (like mongrel) from ImageMagick's memory leaks
  23      require 'RMagick'
  24      require 'gbarcode'
  25    end
  26  
  27    def get_barcode_image(string_to_encode)
  28      if string_to_encode.nil?
  29        string_to_encode = "No string specified"
  30      end
  31      string_to_encode = remove_rails_file_extension(string_to_encode)
  32      eps_barcode = get_barcode_eps(string_to_encode)
  33      gif_barcode = convert_eps_to_gif(eps_barcode)
  34      return gif_barcode
  35    end
  36  
  37    def remove_rails_file_extension(string_to_encode)
  38      if string_to_encode[-4..-1] == ".png"
  39        string_to_encode = string_to_encode[0..-5]
  40      end
  41      return string_to_encode
  42    end
  43  
  44    def get_barcode_eps(string_to_encode)
  45      barcode_object = Gbarcode.barcode_create(string_to_encode)
  46      Gbarcode.barcode_encode(barcode_object, Gbarcode::BARCODE_128)
  47      return BarcodeGenerator.get_subprocess_output do
  48          Gbarcode.barcode_print(barcode_object, $stdout, Gbarcode::BARCODE_OUT_EPS)    
  49      end
  50    end
  51    
  52    def convert_eps_to_gif(eps_image)
  53      base64_eps_image = Base64.encode64(eps_image)
  54      im = Magick::Image::read_inline(base64_eps_image).first
  55      im.format = "GIF"
  56      return BarcodeGenerator.get_subprocess_output do
  57         im.write($stdout) 
  58      end
  59    end
  60  
  61    # execute a block's code in a subprocess, returning any output
  62    def BarcodeGenerator.get_subprocess_output()
  63      data = ""
  64      IO.popen('-', 'r+') do |child_filehandle|
  65        if child_filehandle
  66          begin
  67            data = child_filehandle.read
  68          ensure
  69            child_filehandle.close_write
  70          end
  71        else
  72          yield
  73        end
  74      end
  75      return data
  76    end
  77  
  78  end    

You need to create an account or log in to post comments to this site.


Click here to browse all 5545 code snippets

Related Posts