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_11_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:       @error_correct_level  = QRERRORCORRECTLEVEL[ level.to_sym ] 
79:       @type_number          = options[:size] || 4
80:       @module_count         = @type_number * 4 + 17
81:       @modules              = nil
82:       @data_cache           = nil
83:       @data_list            = QR8bitByte.new( @data )
84: 
85:       self.make
86:     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 96
 96:     def is_dark( row, col )
 97:       if row < 0 || @module_count <= row || col < 0 || @module_count <= col
 98:         raise QRCodeRunTimeError, "#{row},#{col}"
 99:       end
100:       @modules[row][col]
101:     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 121
121:     def to_s( *args )
122:       options                = args.extract_options!
123:       row                    = options[:true] || 'x' 
124:       col                    = options[:false] || ' ' 
125: 
126:       res = []
127: 
128:       @modules.each_index do |c|
129:         tmp = []
130:         @modules.each_index do |r|
131:           if is_dark(c,r)
132:             tmp << row 
133:           else
134:             tmp << col 
135:           end
136:         end 
137:         res << tmp.join
138:      end
139:       res.join("\n")
140:     end

[Validate]