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 )

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 )


106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/rqrcode/qrcode/qr_code.rb', line 106

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

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

  @data                 = string

  mode                  = QRAlphanumeric.valid_data?( @data ) ? :mode_alpha_numk : :mode_8bit_byte

  max_size_array        = QRMAXDIGITS[level][mode]
  size                  = options[:size] || smallest_size_for(string, max_size_array)

  @error_correct_level  = QRERRORCORRECTLEVEL[level]
  @version              = size
  @module_count         = @version * 4 + QRPOSITIONPATTERNLENGTH
  @modules              = Array.new( @module_count )
  @data_list            = (mode == :mode_alpha_numk) ? QRAlphanumeric.new( @data ) : QR8bitByte.new( @data )
  @data_cache           = nil
  self.make
end

Instance Attribute Details

#error_correction_levelObject (readonly)

Returns the value of attribute error_correction_level.



91
92
93
# File 'lib/rqrcode/qrcode/qr_code.rb', line 91

def error_correction_level
  @error_correction_level
end

#module_countObject (readonly)

Returns the value of attribute module_count.



91
92
93
# File 'lib/rqrcode/qrcode/qr_code.rb', line 91

def module_count
  @module_count
end

#modulesObject (readonly)

Returns the value of attribute modules.



91
92
93
# File 'lib/rqrcode/qrcode/qr_code.rb', line 91

def modules
  @modules
end

#versionObject (readonly)

Returns the value of attribute version.



91
92
93
# File 'lib/rqrcode/qrcode/qr_code.rb', line 91

def version
  @version
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


142
143
144
145
146
147
# File 'lib/rqrcode/qrcode/qr_code.rb', line 142

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


169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/rqrcode/qrcode/qr_code.rb', line 169

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