Module: Qr4r

Defined in:
lib/qr4r.rb,
lib/qr4r/version.rb

Constant Summary collapse

VERSION =
'0.3.4'

Class Method Summary collapse

Class Method Details

.encode(str, outfile, *rest) ⇒ Object

params we use

pixel_size  - size of each dot, default = 3

params we pass to QRCode include :size and :level

size   - the size of the qrcode (default 4)
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

note: if size is not included and 4 appears to be too small for the included string, we’ll make it bigger

if you include size, we'll use it, which may lead to an error if the string is too long

Limitations are as follows:

size = 1, max string length = 7
size = 2, max string length = 14
size = 3, max string length = 24
size = 4, max string length = 34
size = 5, max string length = 44
size = 6, max string length = 58
size = 7, max string length = 64
size = 8, max string length = 84
size = 9, max string length = 98
size = 10, max string length = 119


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/qr4r.rb', line 28

def self.encode(str, outfile, *rest)
  opts = rest[0] if rest && rest.length > 0
  opts = opts || {} 
  if !opts[:size]
    opts.merge!({:size => compute_size(str)})
  end
  if !opts[:pixel_size]
    opts.merge!({:pixel_size => 3})
  end
  qr = RQRCode::QRCode.new(str, opts)
  data = []
  qr.modules.each_index do |x|
    qr.modules.each_index do |y|
      if qr.dark?(x,y)
        3.times { data << 0 }
      else
        3.times { data << 255 }
      end
    end
  end
  MojoMagick::convert do |c|
    d = data.pack 'C'*data.size
    c.blob(d, :format => :rgb, :depth => 8, :size => ("%dx%d" % [qr.modules.size, qr.modules.size]))
    if opts[:pixel_size]
      wd = qr.modules.size * opts[:pixel_size].to_i
      c.scale "%dx%d" % [ wd, wd ]
    end
    if opts[:border]
      border = opts[:border].to_i
      c.bordercolor '"#ffffff"'
      c.border '%dx%d' % [ border, border ]
    end
    c.file outfile
  end
end