Class: VisualQrcode::Export

Inherits:
Object
  • Object
show all
Includes:
PixelTools
Defined in:
lib/visual_qrcode/export.rb

Constant Summary

Constants included from PixelTools

PixelTools::PIXEL_DEPTH

Instance Method Summary collapse

Methods included from PixelTools

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

Constructor Details

#initialize(pixels) ⇒ Export

Returns a new instance of Export.



11
12
13
# File 'lib/visual_qrcode/export.rb', line 11

def initialize(pixels)
  @pixels_handler = PixelsHandler.new(pixels: pixels)
end

Instance Method Details

#as_png(size: nil, margin: 6) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/visual_qrcode/export.rb', line 15

def as_png(size: nil, margin: 6)
  @pixels_handler.add_margin(margin, color: :white)
  image = MiniMagick::Image.get_image_from_pixels(pixels, dimensions, "RGBA", PIXEL_DEPTH, "png")

  image.resize "#{size}x#{size}" if size

  image
end

#as_text(dark: "x", light: " ") ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/visual_qrcode/export.rb', line 24

def as_text(dark: "x", light: " ")
  pixels.map do |row|
    row.map do |pixel|
      is_light = pixel.nil? || pixel.sum < max_depth * 3
      is_light ? light : dark
    end.join
  end.join("\n")
end

#dimensionsObject



37
38
39
# File 'lib/visual_qrcode/export.rb', line 37

def dimensions
  @pixels_handler.dimensions
end

#pixelsObject



33
34
35
# File 'lib/visual_qrcode/export.rb', line 33

def pixels
  @pixels_handler.pixels
end