Skip to content

Instantly share code, notes, and snippets.

@trevorcreech
Created October 7, 2009 18:42
Show Gist options
  • Save trevorcreech/204304 to your computer and use it in GitHub Desktop.
Save trevorcreech/204304 to your computer and use it in GitHub Desktop.
# Barcode parser for hackaday
require 'rubygems'
require 'RMagick'
codes = {"332111"=>"[", "211313"=>"G", "231131"=>"R", "214121"=>"z", "241112"=>"n", "241211"=>"k", "213131"=>"U", "123122"=>"0", "123221"=>"1", "122132"=>"-", "122231"=>".", "412121"=>"{", "413111"=>"m", "322112"=>"<", "322211"=>"=", "112232"=>",", "321122"=>"9", "321221"=>":", "112133"=>"J", "112331"=>"K", "231212"=>"+", "231113"=>"H", "231311"=>"I", "213212"=>"5", "213113"=>"S", "213311"=>"T", "122411"=>"h", "122114"=>"g", "122213"=>"&", "122312"=>"'", "311222"=>"8", "311123"=>"V", "311321"=>"W", "111341"=>"}", "111143"=>"|", "111242"=>"p", "112412"=>"f", "112214"=>"e", "112313"=>"D", "132131"=>"L", "212222"=>" ", "212321"=>"?", "142211"=>"j", "142112"=>"i", "121421"=>"b", "331121"=>"X", "131141"=>"~", "212141"=>"y", "121241"=>"r", "121142"=>"q", "232121"=>"@", "314111"=>"\\", "111323"=>"A", "111224"=>"_", "111422"=>"`", "223211"=>"2", "223112"=>"6", "124211"=>"u", "124112"=>"t", "141221"=>"d", "141122"=>"c", "132212"=>"(", "132113"=>"E", "132311"=>"F", "431111"=>"^", "313121"=>"P", "312131"=>"7", "212123"=>">", "222122"=>"!", "222221"=>"\"", "221132"=>"3", "221231"=>"4", "114212"=>"s", "121223"=>"#", "121322"=>"$", "121124"=>"a", "131222"=>"%", "131123"=>"B", "131321"=>"C", "113222"=>"/", "113123"=>"M", "113321"=>"N", "421211"=>"x", "421112"=>"w", "411212"=>"v", "211331"=>"Q", "312212"=>";", "312113"=>"Y", "312311"=>"Z", "134111"=>"o", "221114"=>"l", "133121"=>"O", "221213"=>")", "221312"=>"*", "221411"=>"]"}
barcode = []
image = Magick::Image.read('http://hackadaycom.files.wordpress.com/2009/10/barcode_challenge.jpg').first
pixels = image.get_pixels(0,image.rows / 2,image.columns, 1)
count = 0
last_black = false
pixels.each do |p|
magnitude = p.red + p.green + p.blue
is_black = magnitude < (65535*3)/2
if is_black == last_black
count += 1
else
barcode << count
last_black = is_black
count = 1
end
end
barcode.shift #drop first chunk of white
barcode.each_slice(6).each do |chunk|
char = codes[chunk.join]
print char if char
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment