Class: VisualQrcode::Qrcode

Inherits:
Object
  • Object
show all
Includes:
ModuleFiller, PixelTools
Defined in:
lib/visual_qrcode/qrcode.rb

Constant Summary collapse

DEFAULT_PADDING_MODULES =
7

Constants included from ModuleFiller

ModuleFiller::PIXELS_PER_MODULE

Constants included from PixelTools

PixelTools::PIXEL_DEPTH

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ModuleFiller

#fill_vqr_module

Methods included from PixelTools

#max_depth, #pixel_of, #pixel_of_color, #transparent_pixel, #white_pixel

Constructor Details

#initialize(content, image_path, size: nil, padding_modules: nil, minimum_qr_size: nil) ⇒ Qrcode

Returns a new instance of Qrcode.



18
19
20
21
22
23
24
25
26
27
# File 'lib/visual_qrcode/qrcode.rb', line 18

def initialize(content, image_path, size: nil, padding_modules: nil, minimum_qr_size: nil)
  @content = content
  @size = size
  @padding_modules = padding_modules || DEFAULT_PADDING_MODULES
  @minimum_qr_size = minimum_qr_size || 6

  initialize_basic_qr_code_and_minimum_qr_size(content)
  @pixels_handler = VisualQrcode::PixelsHandler.new(image_path: image_path)
  @common_patterns = @basic_qrcode.instance_variable_get(:@common_patterns)
end

Instance Attribute Details

#basic_qrcodeObject (readonly)

Returns the value of attribute basic_qrcode.



16
17
18
# File 'lib/visual_qrcode/qrcode.rb', line 16

def basic_qrcode
  @basic_qrcode
end

#contentObject (readonly)

Returns the value of attribute content.



16
17
18
# File 'lib/visual_qrcode/qrcode.rb', line 16

def content
  @content
end

#pixels_handlerObject (readonly)

Returns the value of attribute pixels_handler.



16
17
18
# File 'lib/visual_qrcode/qrcode.rb', line 16

def pixels_handler
  @pixels_handler
end

#vqr_pixelsObject (readonly)

Returns the value of attribute vqr_pixels.



16
17
18
# File 'lib/visual_qrcode/qrcode.rb', line 16

def vqr_pixels
  @vqr_pixels
end

Instance Method Details

#as_png(margin: default_margin) ⇒ Object



29
30
31
32
# File 'lib/visual_qrcode/qrcode.rb', line 29

def as_png(margin: default_margin)
  make
  VisualQrcode::Export.new(@vqr_pixels).as_png(size: @size, margin: margin)
end

#fill_vqr_pixelsObject



50
51
52
53
54
55
56
# File 'lib/visual_qrcode/qrcode.rb', line 50

def fill_vqr_pixels
  @basic_qrcode.modules.each_with_index do |module_row, x_index|
    module_row.each_index do |y_index|
      fill_vqr_module(x_index, y_index)
    end
  end
end

#intit_vqr_pixelsObject



40
41
42
43
# File 'lib/visual_qrcode/qrcode.rb', line 40

def intit_vqr_pixels
  @vqr_length = min_length * size_multiplier
  @vqr_pixels = Array.new(@vqr_length) { Array.new(@vqr_length) }
end

#makeObject



34
35
36
37
38
# File 'lib/visual_qrcode/qrcode.rb', line 34

def make
  intit_vqr_pixels
  resize_image
  fill_vqr_pixels
end

#resize_imageObject



45
46
47
48
# File 'lib/visual_qrcode/qrcode.rb', line 45

def resize_image
  padding_size = @padding_modules * module_size
  @pixels_handler.resize_with_padding(@vqr_length, padding_size)
end