Class: PrettyQrcode

Inherits:
Object
  • Object
show all
Defined in:
lib/pretty_qrcode.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, image_path, size: 4, level: :h) ⇒ PrettyQrcode

Returns a new instance of PrettyQrcode.



10
11
12
13
14
15
16
17
18
19
# File 'lib/pretty_qrcode.rb', line 10

def initialize data, image_path, size: 4, level: :h
  @data = data
  @image_path = image_path
  @size = size
  @level = level

  @map_size = (17 + @size * 4 ) * 3
  @map = Array.new @map_size
  @map.each_index {|i|@map[i] = Array.new @map_size}
end

Instance Attribute Details

#imageObject

Returns the value of attribute image.



8
9
10
# File 'lib/pretty_qrcode.rb', line 8

def image
  @image
end

#mapObject

Returns the value of attribute map.



8
9
10
# File 'lib/pretty_qrcode.rb', line 8

def map
  @map
end

Instance Method Details

#draw_backObject



95
96
97
98
99
100
101
# File 'lib/pretty_qrcode.rb', line 95

def draw_back
  @map.each_index do |x|
    @map[x].each_index do |y|
      @image.import_pixels y, x, 1, 1, 'I', [@map[x][y] ? 65535 : 0]
    end
  end
end

#draw_baseObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/pretty_qrcode.rb', line 66

def draw_base
  common = @qr.instance_variable_get("@common_patterns")
  @qr.modules.each_index do |x|
    @qr.modules.each_index do |y|
      if common[x][y].nil?
        next
      elsif common[x][y]
        (0..2).each do |i|
          (0..2).each do |j|
            @map[x*3+i][y*3+j] = false
          end
        end
      else
        (0..2).each do |i|
          (0..2).each do |j|
            @map[x*3+i][y*3+j] = true
          end
        end
      end
    end
  end
end

#draw_imageObject



48
49
50
51
52
# File 'lib/pretty_qrcode.rb', line 48

def draw_image
  @image.each_pixel do |pixel, c, r|
    @map[r][c] = pixel.intensity > 32767 ? true : false
  end
end

#draw_qrObject



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/pretty_qrcode.rb', line 54

def draw_qr
  @qr.modules.each_index do |x|
    @qr.modules.each_index do |y|
      if @qr.dark?(x,y)
        @map[x*3+1][y*3+1] = false
      else
        @map[x*3+1][y*3+1] = true
      end
    end
  end
end

#generate_qrObject



44
45
46
# File 'lib/pretty_qrcode.rb', line 44

def generate_qr
  @qr = RQRCode::QRCode.new(@data, :size => @size, :level => @level )
end

#makeObject



21
22
23
24
25
26
27
28
29
30
# File 'lib/pretty_qrcode.rb', line 21

def make
  read
  resize
  monochrome
  generate_qr
  draw_image
  draw_qr
  draw_base
  draw_back
end

#monochromeObject



40
41
42
# File 'lib/pretty_qrcode.rb', line 40

def monochrome
  @image.image_type= Magick::BilevelType
end


89
90
91
92
93
# File 'lib/pretty_qrcode.rb', line 89

def print
  @map.map do |line|
    line.map {|p|p ? ' ' : 'X'}.join
  end.join("\n")
end

#readObject



32
33
34
# File 'lib/pretty_qrcode.rb', line 32

def read
  @image = ImageList.new @image_path
end

#resizeObject



36
37
38
# File 'lib/pretty_qrcode.rb', line 36

def resize
  @image = @image.resize @map_size, @map_size
end

#save(file) ⇒ Object



103
104
105
# File 'lib/pretty_qrcode.rb', line 103

def save file
  @image.write file
end