Skip to content

Instantly share code, notes, and snippets.

@rbur004
Last active October 1, 2016 22:54
Show Gist options
  • Save rbur004/f3615da42dc163de83d7e9e1f386892d to your computer and use it in GitHub Desktop.
Save rbur004/f3615da42dc163de83d7e9e1f386892d to your computer and use it in GitHub Desktop.
Chunky_png with Zbar
require 'chunky_png'
require 'zbar'
#Turns Colour PNG into 8bit BW Y800 string.
# @param image [ChunckyPNG::Image]
# @return [String] Y800 image, as a string
def png_to_y800(image)
y800 = ""
(0...image.height).each do |h|
(0...image.width).each do |w|
v = image[w,h]
y800 << (((ChunkyPNG::Color::r(v) * 0.21 +
ChunkyPNG::Color::g(v) * 0.72 +
ChunkyPNG::Color::b(v) * 0.07).round ) & 0xFF ).chr
end
end
return y800
end
png_image = ChunkyPNG::Image.from_file(IMG)
y800_image = png_to_y800(png_image)
zbar_image = ZBar::Image.new
zbar_image.set_data(ZBar::Format::Y800, y800_image, png_image.width, png_image.height)
puts zbar_image.process[0].data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment