class PrettyQrcode

Attributes

image[RW]
map[RW]

Public Class Methods

new(data, image_path, size: 4, level: :h) click to toggle source
# 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

Public Instance Methods

draw_back() click to toggle source
# 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_base() click to toggle source
# 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_image() click to toggle source
# 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_qr() click to toggle source
# 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_qr() click to toggle source
# File lib/pretty_qrcode.rb, line 44
def generate_qr
  @qr = RQRCode::QRCode.new(@data, :size => @size, :level => @level )
end
make() click to toggle source
# File lib/pretty_qrcode.rb, line 21
def make
  read
  resize
  monochrome
  generate_qr
  draw_image
  draw_qr
  draw_base
  draw_back
end
monochrome() click to toggle source
# File lib/pretty_qrcode.rb, line 40
def monochrome
  @image.image_type= Magick::BilevelType
end
print() click to toggle source
read() click to toggle source
# File lib/pretty_qrcode.rb, line 32
def read
  @image = ImageList.new @image_path
end
resize() click to toggle source
# File lib/pretty_qrcode.rb, line 36
def resize
  @image = @image.resize @map_size, @map_size
end
save(file) click to toggle source
# File lib/pretty_qrcode.rb, line 103
def save file
  @image.write file
end