Class: QRCoder::QRCode

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

Constant Summary collapse

IMAGE_FORMATS =
[:jpg , :png, :gif, :tif, :bmp]

Class Method Summary collapse

Class Method Details

.image(text, output, options) ⇒ Object

Create qr image or images

Parameters

  • text String – String for qrcode

  • output String – String for path where to save files

  • options Hash – Hash with options

Options

  • :format - Array or symbol. Allowed formats are :png, :bmp, :svg, :jpg, :gif and :tif

  • :filename - String for filename without extension. Extension will be added automaticly.

  • :size - This controls how big the QR Code will be. Smallest size will be chosen by default. Set to maintain consistent size.

  • :level - The error correction level, can be:

    • Level :l 7% of code can be restored

    • Level :m 15% of code can be restored

    • Level :q 25% of code can be restored

    • Level :h 30% of code can be restored (default :h)

  • :offset - Padding around the QR Code (e.g. 10)

  • :unit - How many pixels per module (e.g. 11)

  • :fill - Background color (e.g “ffffff” or :white)

  • :color - Foreground color for the code (e.g. “000000” or :black)

Example

QRCode.image("some text", "/home/user/", :format => [:png, :svg], :filename => "simple_test" 
QRCode.image("some text", "/home/user/", :format => :png, :filename => "simple_test" , :unit => 12

Raises:



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
# File 'lib/qrcoder.rb', line 45

def image text, output, options
  formats = []
  if options[:format].kind_of? Array
    formats = options[:format]
  else
    formats << options[:format] || :png
  end
  filename = options[:filename] || text
  svg      = self.svg(text, options)

  err_f = formats - IMAGE_FORMATS - [:svg]
  raise QRCodeError.new("#{err_f.join(', ')} is not supported file formats") unless(err_f.empty?)

  begin 
    formats.each do |format|
      if IMAGE_FORMATS.include?(format)
        image = MiniMagick::Image.read(svg) { |i| i.format "svg" }
        image.format format.to_s
        image.write "#{output}/#{filename}.#{format}"
      else
        data = svg
        file = File.new("#{output}/#{filename}.#{format}", "w")
        file.write(data)
        file.close
      end
    end
  rescue 
    raise QRCodeError.new "File saving error"
  end
end

.svg(text, options = {}) ⇒ Object

Create qr code svg output response

  • text String – String for qrcode

  • options Hash – Hash with options

Options

  • :format - Array or symbol. Allowed formats are :png, :bmp, :svg, :jpg, :gif and :tif

  • :filename - String for filename without extension. Extension will be added automaticly.

  • :size - This controls how big the QR Code will be. Smallest size will be chosen by default. Set to maintain consistent size.

  • :level - The error correction level, can be:

    • Level :l 7% of code can be restored

    • Level :m 15% of code can be restored

    • Level :q 25% of code can be restored

    • Level :h 30% of code can be restored (default :h)

  • :offset - Padding around the QR Code (e.g. 10)

  • :unit - How many pixels per module (e.g. 11)

  • :fill - Background color (e.g “ffffff” or :white)

  • :color - Foreground color for the code (e.g. “000000” or :black)



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

def svg text, options ={}
  size   = options[:size]  || QRCoder.minimum_qr_size_from_string(text)
  level  = options[:level] || :h
  qrcode = RQRCode::QRCode.new(text, :size => size, :level => level)
  QRCoder::Renderers::SVG::render(qrcode, options)
end