Class RQRCode::QRCode
In: lib/rqrcode/qrcode/qr_code.rb
Parent: Object
RuntimeError QRCodeRunTimeError ArgumentError QRCodeArgumentError QRBitBuffer QR8bitByte QRMath QRRSBlock QRUtil QRPolynomial QRCode lib/rqrcode/qrcode/qr_bit_buffer.rb lib/rqrcode/qrcode/qr_8bit_byte.rb lib/rqrcode/qrcode/qr_math.rb lib/rqrcode/qrcode/qr_rs_block.rb lib/rqrcode/qrcode/qr_util.rb lib/rqrcode/qrcode/qr_polynomial.rb lib/rqrcode/qrcode/qr_code.rb RQRCode dot/m_13_0.png

Creation

QRCode objects expect only one required constructor parameter and an optional hash of any other. Here‘s a few examples:

 qr = RQRCode::QRCode.new('hello world')
 qr = RQRCode::QRCode.new('hello world', :size => 1, :level => :m )

Methods

is_dark   new   to_s  

Constants

PAD0 = 0xEC
PAD1 = 0x11

Attributes

module_count  [R] 
modules  [R] 

Public Class methods

Expects a string to be parsed in, other args are optional

  # string - the string you wish to encode
  # 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 (default :h)

  qr = RQRCode::QRCode.new('hello world', :size => 1, :level => :m )

[Source]

    # File lib/rqrcode/qrcode/qr_code.rb, line 72
72:     def initialize( *args )
73:       raise QRCodeArgumentError unless args.first.kind_of?( String )
74: 
75:       @data                 = args.shift
76:       options               = args.extract_options!
77:       level                 = options[:level] || :h 
78: 
79:       raise QRCodeArgumentError unless %w(l m q h).include?(level.to_s) 
80: 
81:       @error_correct_level  = QRERRORCORRECTLEVEL[ level.to_sym ] 
82:       @type_number          = options[:size] || 4
83:       @module_count         = @type_number * 4 + 17
84:       @modules              = Array.new( @module_count )
85:       @data_list            = QR8bitByte.new( @data )
86:       @data_cache           = nil
87: 
88:       self.make
89:     end

Public Instance methods

is_dark is called with a col and row parameter. This will return true or false based on whether that coordinate exists in the matrix returned. It would normally be called while iterating through modules. A simple example would be:

 instance.is_dark( 10, 10 ) => true

[Source]

     # File lib/rqrcode/qrcode/qr_code.rb, line 99
 99:     def is_dark( row, col )
100:       if row < 0 || @module_count <= row || col < 0 || @module_count <= col
101:         raise QRCodeRunTimeError, "#{row},#{col}"
102:       end
103:       @modules[row][col]
104:     end

This is a public method that returns the QR Code you have generated as a string. It will not be able to be read in this format by a QR Code reader, but will give you an idea if the final outout. It takes two optional args +:true+ and +:false+ which are there for you to choose how the output looks. Here‘s an example of it‘s use:

 instance.to_s =>
 xxxxxxx x  x x   x x  xx  xxxxxxx
 x     x  xxx  xxxxxx xxx  x     x
 x xxx x  xxxxx x       xx x xxx x

 instance._to_s( :true => 'E', :false => 'Q') =>
 EEEEEEEQEQQEQEQQQEQEQQEEQQEEEEEEE
 EQQQQQEQQEEEQQEEEEEEQEEEQQEQQQQQE
 EQEEEQEQQEEEEEQEQQQQQQQEEQEQEEEQE

[Source]

     # File lib/rqrcode/qrcode/qr_code.rb, line 124
124:     def to_s( *args )
125:       options                = args.extract_options!
126:       row                    = options[:true] || 'x' 
127:       col                    = options[:false] || ' ' 
128: 
129:       res = []
130: 
131:       @modules.each_index do |c|
132:         tmp = []
133:         @modules.each_index do |r|
134:           if is_dark(c,r)
135:             tmp << row 
136:           else
137:             tmp << col 
138:           end
139:         end 
140:         res << tmp.join
141:      end
142:       res.join("\n")
143:     end

[Validate]