Skip to content

Instantly share code, notes, and snippets.

Created August 15, 2011 13:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/1146650 to your computer and use it in GitHub Desktop.
Save anonymous/1146650 to your computer and use it in GitHub Desktop.
Using Zbar barcode reader command line with Ruby
def self.read_barcode_from_image path
# Call the barcode reader - and get a response of multiple lines
ls = `/usr/bin/zbarimg '#{path}'`.lines
# Handle a variety of unexpected responses that we have seen and debugged quickly over time
ls = [ls] if ls.is_a?(String)
barcodes = []
return barcodes if ls.nil? || ls == 0
# Read the responses and create an array of barcode types, values and potential URLs
for l in ls
pos = l.index(':')
s = [l[0..pos-1], l[pos+1..-1].chomp]
url_string = s[1].dup
url_string.gsub!(/((https?:\/\/)([-\w]+\.[-\w\.]+)+\w(:\d+)?(\/([-\w\/_\.]*(\?\S+)?)?)*)/, '<a href="\1">\1</a>')
includes_url = !s[1].index(/((https?:\/\/)([-\w]+\.[-\w\.]+)+\w(:\d+)?(\/([-\w\/_\.]*(\?\S+)?)?)*)/).nil?
barcodes << (s << includes_url << url_string )
end
return barcodes
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment