├── .gitignore ├── install.rb ├── lib ├── config.rb ├── imagemagick_wrapper.rb └── barcode_generator.rb ├── init.rb ├── Rakefile ├── test └── barcode_generator_test.rb └── README.markdown /.gitignore: -------------------------------------------------------------------------------- 1 | nbproject/* 2 | -------------------------------------------------------------------------------- /install.rb: -------------------------------------------------------------------------------- 1 | # Install hook code here 2 | 3 | -------------------------------------------------------------------------------- /lib/config.rb: -------------------------------------------------------------------------------- 1 | DEFAULT_ENCODING = Gbarcode::BARCODE_39 | Gbarcode::BARCODE_NO_CHECKSUM 2 | DEFAULT_FORMAT = 'png' -------------------------------------------------------------------------------- /init.rb: -------------------------------------------------------------------------------- 1 | # Include hook code here 2 | require 'rubygems' 3 | require 'gbarcode' 4 | require 'config' 5 | require 'barcode_generator' 6 | -------------------------------------------------------------------------------- /lib/imagemagick_wrapper.rb: -------------------------------------------------------------------------------- 1 | module ImageMagickWrapper 2 | # call imagemagick library on commandline thus bypassing RMagick 3 | # memory leak hasseles :) 4 | def convert_to_png(src, out, resolution=nil, antialias=nil) 5 | #more options : convert +antialias -density 150 eps png 6 | options = [] 7 | if resolution.present? 8 | options << "-density #{resolution}" 9 | elsif antialias == 1 10 | options << "+antialias" 11 | end 12 | 13 | system("convert #{options.collect.join(' ')} #{src} #{out}") 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require 'rake' 2 | require 'rake/testtask' 3 | require 'rake/rdoctask' 4 | 5 | desc 'Default: run unit tests.' 6 | task :default => :test 7 | 8 | desc 'Test the barcode_generator plugin.' 9 | Rake::TestTask.new(:test) do |t| 10 | t.libs << 'lib' 11 | t.pattern = 'test/**/*_test.rb' 12 | t.verbose = true 13 | end 14 | 15 | desc 'Generate documentation for the barcode_generator plugin.' 16 | Rake::RDocTask.new(:rdoc) do |rdoc| 17 | rdoc.rdoc_dir = 'rdoc' 18 | rdoc.title = 'BarcodeGenerator' 19 | rdoc.options << '--line-numbers' << '--inline-source' 20 | rdoc.rdoc_files.include('README') 21 | rdoc.rdoc_files.include('lib/**/*.rb') 22 | end 23 | -------------------------------------------------------------------------------- /test/barcode_generator_test.rb: -------------------------------------------------------------------------------- 1 | require 'test/unit' 2 | require 'rubygems' 3 | require 'action_controller' 4 | require 'action_controller/test_process' 5 | require 'active_support' 6 | require 'action_view/helpers' 7 | require 'action_view/test_case' 8 | require File.dirname(__FILE__) + '/../lib/barcode_generator' 9 | 10 | class BarcodeGeneratorTest < ActionView::TestCase 11 | 12 | def setup 13 | ActionController::Routing::Routes.draw do |map| 14 | map.connect ':controller/:action/:id' 15 | end 16 | end 17 | 18 | def test_can_create_barcode_image 19 | assert(true) 20 | end 21 | 22 | def test_invalid_options_not_allowed 23 | assert_raises(ArgumentError) { 24 | av_instance = ActionView::Base.new 25 | av_instance.barcode("id", { :noexistent_key => true }) 26 | } 27 | end 28 | 29 | 30 | end 31 | -------------------------------------------------------------------------------- /README.markdown: -------------------------------------------------------------------------------- 1 | BarcodeGenerator 2 | ================ 3 | ![anuj barcoded](http://farm2.static.flickr.com/1378/1125817164_6f57adb24a_o.png "sample barcode image") 4 | 5 | ### Author : Anuj Luthra 6 | 7 | Barcode generator makes generating/displaying bar-codes for certain 8 | alphanumeric ids a piece of cake. 9 | It uses Gbarcode for encoding barcode data and then rmagick to generate 10 | images out of it for displaying in views. 11 | 12 | This way we can generate any barcode type which Gbarcode -> Gnome Barcode project 13 | supports. 14 | 15 | ## Ruby1.9 note 16 | 17 | please use ruby1.9 compatible gbarcode at [github repository by andreas](https://github.com/ahx/gbarcode) . Good work mate. 18 | 19 | ### USAGE: 20 | its as simple as saying 21 | `<%= barcode 'FJJ4JD'%> ` 22 | 23 | This will generate a barcode for FJJ4JD in BARCODE_39 format with default width 24 | and height and include it in the view. 25 | the images are stored in /public/system/barcodes subdir. 26 | 27 | ### Options Options Options: 28 | To customize your barcodes, you can optionally pass following information in your views 29 | 30 | + encoding_format (Gbarcode constants for eg. Gbarcode::BARCODE_128 etc..) 31 | + width 32 | + height 33 | + scaling_factor 34 | + xoff 35 | + yoff 36 | + margin 37 | + no_ascii (accepts boolean true or false, prevents the ascii string from printing at the bottom of the barcode) 38 | 39 | in this case your view will look like : 40 | 41 | 42 | <%= barcode 'ANUJ', :height => 100, 43 | :width => 400, 44 | :margin => 100, 45 | :xoff => 20, 46 | :yoff => 40 47 | %> 48 | 49 | 50 | ### Installation: 51 | install from git : script/install plugin git://github.com/anujluthra/barcode-generator.git 52 | 53 | make sure that you install 54 | 55 | 1. gem for gbarcode 56 | 2. install native ImageMagick library 57 | 58 | ### Supported Barcode Formats: 59 | Gbarcode as of now allows us to generate barcodes in following formats: 60 | BARCODE_EAN 61 | BARCODE_UPC 62 | BARCODE_ISBN 63 | BARCODE_128B 64 | BARCODE_128C 65 | BARCODE_128 66 | BARCODE_128RAW 67 | BARCODE_39 68 | BARCODE_I25 69 | BARCODE_CBR 70 | BARCODE_MSI 71 | BARCODE_PLS 72 | BARCODE_93 73 | BARCODE_ANY 74 | BARCODE_NO_CHECKSUM 75 | 76 | for more information on Gbarcode visit [rubyforge home of gbarcode](http://gbarcode.rubyforge.org/rdoc/index.html) 77 | 78 | -------------------------------------------------------------------------------- /lib/barcode_generator.rb: -------------------------------------------------------------------------------- 1 | # BarcodeGenerator 2 | # uses Gbarcode for encoding barcode data and then rmagick to generate 3 | # images out of it for displaying in views. 4 | 5 | # i was using clumsy file.new and file.close for eps generation, but 6 | # Adam Feuer had published a much elegant way by using IO.pipe for doing 7 | # same thing. (never occured to me !! :-P ) 8 | 9 | # this way we can generate any barcode type which Gbarcode -> Gnome Barcode project 10 | # supports. 11 | 12 | # Extending ActionView::Base to support rendering themes 13 | require 'imagemagick_wrapper' 14 | module ActionView 15 | # Extending ActionView::Base to support rendering themes 16 | class Base 17 | include ImageMagickWrapper 18 | 19 | 20 | VALID_BARCODE_OPTIONS = [:encoding_format, :output_format, :width, :height, :scaling_factor, :xoff, :yoff, :margin, :resolution, :antialias ] 21 | 22 | def barcode(id, options = {:encoding_format => DEFAULT_ENCODING }) 23 | 24 | options.assert_valid_keys(VALID_BARCODE_OPTIONS) 25 | output_format = options[:output_format] ? options[:output_format] : DEFAULT_FORMAT 26 | 27 | id.upcase! 28 | # This gives us a partitioned path so as not to have thousands 29 | # of files in the same directory. Also, put the files in 30 | # public system since capistrano symlinks this path across 31 | # deployments by default 32 | path = Rails.root.join('public', 'system', 'barcodes', *Digest::MD5.hexdigest(id).first(9).scan(/.../)) 33 | FileUtils.mkdir_p(path) 34 | eps = "#{path}/#{id}.eps" 35 | out = "#{path}/#{id}.#{output_format}" 36 | 37 | #dont generate a barcode again, if already generated 38 | unless File.exists?(out) 39 | #generate the barcode object with all supplied options 40 | options[:encoding_format] = DEFAULT_ENCODING unless options[:encoding_format] 41 | bc = Gbarcode.barcode_create(id) 42 | bc.width = options[:width] if options[:width] 43 | bc.height = options[:height] if options[:height] 44 | bc.scalef = options[:scaling_factor] if options[:scaling_factor] 45 | bc.xoff = options[:xoff] if options[:xoff] 46 | bc.yoff = options[:yoff] if options[:yoff] 47 | bc.margin = options[:margin] if options[:margin] 48 | Gbarcode.barcode_encode(bc, options[:encoding_format]) 49 | 50 | if options[:no_ascii] 51 | print_options = Gbarcode::BARCODE_OUT_EPS|Gbarcode::BARCODE_NO_ASCII 52 | else 53 | print_options = Gbarcode::BARCODE_OUT_EPS 54 | end 55 | 56 | #encode the barcode object in desired format 57 | File.open(eps,'wb') do |eps_img| 58 | Gbarcode.barcode_print(bc, eps_img, print_options) 59 | eps_img.close 60 | convert_to_png(eps, out, options[:resolution], options[:antialias]) 61 | end 62 | 63 | #delete the eps image, no need to accummulate cruft 64 | File.delete(eps) 65 | end 66 | #send the html image tag 67 | image_tag(out.gsub(/.*public\/system/, '/system')) 68 | end 69 | 70 | end 71 | end 72 | --------------------------------------------------------------------------------