Senior & Expert RoR Developers Discussion Forum by Nyros Technologies

HIRE Ruby on Rails Expert Developers Programmers Coders From India
Ruby on Rails PHP .Net Developers Community, Nyros Technologies, Kakinada
 
Log in  or IF not a member please REGISTER
Username:
Password:   


Keyword
Log in | Profile 

Generating Barcode

 
Post new topic   Reply to topic    Senior & Expert RoR Developers Discussion Forum by Nyros Technologies Index -> Advanced programming
View previous topic :: View next topic  
Author Message
mbchowdari



Joined: 08 May 2008
Posts: 52

PostPosted: Wed Jul 01, 2009 12:37 pm    Post subject: Generating Barcode Reply with quote

Generating barcode:

In Your application you have to place the barcode_generator.rb at app/helpers


Note: this will not work without rmagick (Ruby ImageMagick interface) and gbarcode (GNU barcode)

barcode_generator.rb

class BarcodeGenerator

# Uses subprocesses because
# 1. ImageMagick/RMagick leaks memory,
# and doesn't work in a long-running process. The fork makes it safe.
# 2. The output from the Gbarcode and ImageMagick is often longer than the pipe buffer,
# so we have to empty the buffer from another subprocess
def BarcodeGenerator.get_barcode_image(barcode_string)
return BarcodeGenerator.get_subprocess_output do
barcode_generator = BarcodeGenerator.new
$stdout.write(barcode_generator.get_barcode_image(barcode_string))
end
end

def initialize
# we do the imports here to protect long-running processes (like mongrel) from ImageMagick's memory leaks
require 'RMagick'
require 'gbarcode'
end

def get_barcode_image(string_to_encode)
if string_to_encode.nil?
string_to_encode = "No string specified"
end
string_to_encode = remove_rails_file_extension(string_to_encode)
eps_barcode = get_barcode_eps(string_to_encode)
gif_barcode = convert_eps_to_gif(eps_barcode)
return gif_barcode
end

def remove_rails_file_extension(string_to_encode)
if string_to_encode[-4..-1] == ".png"
string_to_encode = string_to_encode[0..-5]
end
return string_to_encode
end

def get_barcode_eps(string_to_encode)
barcode_object = Gbarcode.barcode_create(string_to_encode)
Gbarcode.barcode_encode(barcode_object, Gbarcode::BARCODE_128)
return BarcodeGenerator.get_subprocess_output do
Gbarcode.barcode_print(barcode_object, $stdout, Gbarcode::BARCODE_OUT_EPS)
end
end

def convert_eps_to_gif(eps_image)
base64_eps_image = Base64.encode64(eps_image)
im = Magick::Image::read_inline(base64_eps_image).first
im.format = "GIF"
return BarcodeGenerator.get_subprocess_output do
im.write($stdout)
end
end

# execute a block's code in a subprocess, returning any output
def BarcodeGenerator.get_subprocess_output()
data = ""
IO.popen('-', 'r+') do |child_filehandle|
if child_filehandle
begin
data = child_filehandle.read
ensure
child_filehandle.close_write
end
else
yield
end
end
return data
end

end

In your controller,

string_to_encode = block_img || "ruby"
@barcode_image = BarcodeGenerator.get_barcode_image(string_to_encode)
@filename = "#{string_to_encode}.gif"
File.open("<image path>", "w") { |f| f.write(@barcode_image)}



Thank you,
Chowdari
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    Senior & Expert RoR Developers Discussion Forum by Nyros Technologies Index -> Advanced programming
Page 1 of 1

 latest topics 
 Topics   Replies   Author   Views   Last Post 
No new posts Acts_as_tsearch plugin
0 swaminadhan 14 Sat Mar 13, 2010 9:10 am
swaminadhan View latest post
No new posts Built in classes in ruby
0 ktulasi 20 Thu Mar 11, 2010 1:09 pm
ktulasi View latest post
No new posts User defined classes and types of variables.
2 swaminadhan 38 Wed Mar 10, 2010 4:40 am
swaminadhan View latest post
No new posts Background & Introduction of ruby
0 Divya 43 Mon Mar 08, 2010 12:59 pm
Divya View latest post
No new posts i have lost the data in schema.rb file in rails application
0 Raja 52 Thu Mar 04, 2010 4:46 am
Raja View latest post
No new posts AlertPay
0 Raghu 33 Mon Mar 01, 2010 11:36 am
Raghu View latest post
No new posts have a problem while working with facebooker plugin
1 krishnakanth 44 Mon Mar 01, 2010 9:31 am
sharma I View latest post
No new posts Having issues while trying to run the application
1 sharma I 42 Mon Mar 01, 2010 8:56 am
swaminadhan View latest post
No new posts getting twitter tweets using twitter gem
0 sharma I 38 Mon Mar 01, 2010 8:21 am
sharma I View latest post
No new posts JavaScript functionality with Event handling class
0 umamahesh_nyros 27 Mon Mar 01, 2010 7:25 am
umamahesh_nyros View latest post




Hire an expert Ruby on Rails developer / coder / programmer or development team from India now!!

Other Forums : PHP   ::   .Net   |   Free unlimited HTML CSS templates download

Nyros Technologies   |   Kakinada City Portal   |   Developers Blog   |   About Ruby on Rails Experts   |   More