Class: RQRCode::QRCode

Inherits:
Object
  • Object
show all
Defined in:
lib/rqrcode/qrcode/qr_code.rb

Overview

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 )

Constant Summary collapse

PAD0 =
0xEC
PAD1 =
0x11

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string, *args) ⇒ QRCode

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 )


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/rqrcode/qrcode/qr_code.rb', line 86

def initialize( string, *args )
  if !string.is_a? String
    raise QRCodeArgumentError, "The passed data is #{string.class}, not String"
  end

  options               = args.extract_options!
  level                 = (options[:level] || :h).to_sym
  size                  = options[:size] || 4

  if !QRERRORCORRECTLEVEL.has_key?(level)
    raise QRCodeArgumentError, "Unknown error correction level `#{level.inspect}`"
  end

  @data                 = string
  @error_correct_level  = QRERRORCORRECTLEVEL[level]
  @version              = size
  @module_count         = @version * 4 + QRPOSITIONPATTERNLENGTH
  @modules              = Array.new( @module_count )
  @data_list            = QR8bitByte.new( @data )
  @data_cache           = nil

  self.make
end

Instance Attribute Details

#module_countObject (readonly)

Returns the value of attribute module_count.



68
69
70
# File 'lib/rqrcode/qrcode/qr_code.rb', line 68

def module_count
  @module_count
end

#modulesObject (readonly)

Returns the value of attribute modules.



68
69
70
# File 'lib/rqrcode/qrcode/qr_code.rb', line 68

def modules
  @modules
end

Instance Method Details

#is_dark(row, col) ⇒ Object Also known as: dark?

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


118
119
120
121
122
123
# File 'lib/rqrcode/qrcode/qr_code.rb', line 118

def is_dark( row, col )
  if !row.between?(0, @module_count - 1) || !col.between?(0, @module_count - 1)
    raise QRCodeRunTimeError, "Invalid row/column pair: #{row}, #{col}"
  end
  @modules[row][col]
end

#to_s(*args) ⇒ Object

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


145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/rqrcode/qrcode/qr_code.rb', line 145

def to_s( *args )
  options                = args.extract_options!
  row                    = options[:true] || 'x' 
  col                    = options[:false] || ' ' 

  res = []

  @modules.each_index do |c|
    tmp = []
    @modules.each_index do |r|
      if is_dark(c,r)
        tmp << row 
      else
        tmp << col 
      end
    end 
    res << tmp.join
 end
  res.join("\n")
end