Module: RQRCode::Export::PNG

Defined in:
lib/rqrcode/export/png.rb

Instance Method Summary collapse

Instance Method Details

#as_png(options = {}) ⇒ Object

Render the PNG from the Qrcode.

Options: module_px_size - Image size, in pixels. fill - Background ChunkyPNG::Color, defaults to ‘white’ color - Foreground ChunkyPNG::Color, defaults to ‘black’ border - Border thickness, in pixels

It first creates an image where 1px = 1 module, then resizes. Defaults to 90x90 pixels, customizable by option.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rqrcode/export/png.rb', line 20

def as_png(options = {})

  default_img_options = {
    :resize_gte_to => false,
    :resize_exactly_to => false,
    :fill => 'white',
    :color => 'black',
    :border_modules => 4,
    :file => false,
    :module_px_size => 6
  }
  options = default_img_options.merge(options) # reverse_merge

  fill   = ChunkyPNG::Color(options[:fill])
  color  = ChunkyPNG::Color(options[:color])
  output_file = options[:file]
  border = options[:border_modules]
  total_border = border * 2
  module_px_size = if options[:resize_gte_to]
    (options[:resize_gte_to].to_f / (self.module_count + total_border).to_f).ceil.to_i
  else
    options[:module_px_size]
  end
  border_px = border *  module_px_size
  total_border_px = border_px * 2
  resize_to = options[:resize_exactly_to]

  img_size = module_px_size * self.module_count
  total_img_size = img_size + total_border_px

  png = ChunkyPNG::Image.new(total_img_size, total_img_size, fill)

  self.modules.each_index do |x|
    self.modules.each_index do |y|
      if self.dark?(x, y)
        (0...module_px_size).each do |i|
          (0...module_px_size).each do |j|
            png[(y * module_px_size) + border_px + j , (x * module_px_size) + border_px + i] = color
          end
        end
      end
    end
  end

  png = png.resize(resize_to, resize_to) if resize_to

  if output_file
    png.save(output_file,{ :color_mode => ChunkyPNG::COLOR_GRAYSCALE, :bit_depth =>1})
  end
  png
end