Module: Qrio::ImageDumper

Included in:
Qr
Defined in:
lib/qrio/image_dumper.rb

Overview

this module contains the code to dump images of QR decoding progress. useful for debugging or those curious about the detection / decoding algorithms

Instance Method Summary collapse

Instance Method Details

#color(name) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# File 'lib/qrio/image_dumper.rb', line 86

def color(name)
  rgb = {
    :green   => [  0, 255,   0],
    :red     => [255,   0,   0],
    :magenta => [227,  91, 216],
    :cyan    => [  0, 255, 255],
  }[name]

  ChunkyPNG::Color.rgb(*rgb)
end

#save_image(filename, options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/qrio/image_dumper.rb', line 6

def save_image(filename, options={})
  if matrix = @extracted_matrix
    # extracted matrix is already cropped
    options[:crop] = false
    @features = {
      :candidates      => {},
      :matches         => @translated_matches,
      :finder_patterns => @sampling_grid.finder_patterns
    }
  else
    matrix = @input_matrix
    @features = {
      :candidates      => @candidates,
      :matches         => @matches,
      :finder_patterns => @finder_patterns
    }
  end

  save_to_image(matrix, filename, options)
end

#save_to_image(matrix, filename, options = {}) ⇒ Object



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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/qrio/image_dumper.rb', line 27

def save_to_image(matrix, filename, options={})
  png = ChunkyPNG::Image.new(
    matrix.width,
    matrix.height,
    ChunkyPNG::Color::WHITE
  )

  (0..(matrix.width - 1)).to_a.each do |x|
    (0..(matrix.height - 1)).to_a.each do |y|
      png[x, y] = ChunkyPNG::Color::BLACK if matrix[x, y]
    end
  end

  if options[:annotate].include?(:candidates)
    @features[:candidates][:horizontal].each do |hmatch|
      png.rect(*hmatch.to_coordinates, color(:green))
    end

    @features[:candidates][:vertical].each do |vmatch|
      png.rect(*vmatch.to_coordinates, color(:magenta))
    end
  end

  if options[:annotate].include?(:matches)
    @features[:matches][:horizontal].each do |hmatch|
      png.rect(*hmatch.to_coordinates, color(:green))
    end

    @features[:matches][:vertical].each do |vmatch|
      png.rect(*vmatch.to_coordinates, color(:magenta))
    end
  end

  if options[:annotate].include?(:finder_patterns)
    @features[:finder_patterns].each do |finder_pattern|
      png.rect(*finder_pattern.to_coordinates, color(:red))
    end
  end

  if options[:annotate].include?(:angles)
    @sampling_grid.angles[0, 100].each do |angle|
      png.line_xiaolin_wu(*angle.to_coordinates, color(:cyan))
    end
  end

  if options[:annotate].include?(:alignment_patterns)
    png.rect(*@alignment_pattern.to_coordinates, color(:magenta))
  end

  if options[:annotate].include?(:extracted_pixels)
    @sampling_grid.extracted_pixels do |x, y|
      png.circle(x, y, 1, color(:cyan))
    end
  end

  png = png.crop(*@qr_bounds.to_point_size) if options[:crop]
  png.save(filename, :fast_rgba)
end