Advertisement
Guest User

Untitled

a guest
Oct 1st, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. require 'chunky_png'
  2. require 'zbar'
  3.  
  4. #Turns Colour PNG into 8bit BW Y800 string.
  5. # @param image [ChunckyPNG::Image]
  6. # @return [String] Y800 image, as a string
  7. def png_to_y800(image)
  8. y800 = ""
  9. (0...image.width).each do |w|
  10. (0...image.height).each do |h|
  11. v = image[h,w]
  12. y800 << (((ChunkyPNG::Color::r(v) * 0.21 +
  13. ChunkyPNG::Color::g(v) * 0.72 +
  14. ChunkyPNG::Color::b(v) * 0.07).round ) & 0xFF ).chr
  15. end
  16. end
  17. return y800
  18. end
  19.  
  20. png_image = ChunkyPNG::Image.from_file(IMG)
  21. y800_image = png_to_y800(png_image)
  22. zbar_image = ZBar::Image.new
  23. zbar_image.set_data(ZBar::Format::Y800, y800_image, png_image.width, png_image.height)
  24. puts zbar_image.process[0].data
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement