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, :mode => :alphanumeric )

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)
# mode   - the mode of the qrcode (defaults to alphanumeric or byte_8bit, depending on the input data):
   * :number
   * :alphanumeric
   * :byte_8bit
   * :kanji

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


185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/rqrcode/qrcode/qr_code.rb', line 185

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                  = QRMODE_NAME[(options[:mode] || '').to_sym]
  # If mode is not explicitely given choose mode according to data type
  mode ||= case
    when RQRCode::QRNumeric.valid_data?(@data)
      QRMODE_NAME[:number]
    when QRAlphanumeric.valid_data?(@data)
      QRMODE_NAME[:alphanumeric]
    else
      QRMODE_NAME[:byte_8bit]
  end

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

  if size > QRUtil.max_size
    raise QRCodeArgumentError, "Given size greater than maximum possible size of #{QRUtil.max_size}"
  end

  @error_correct_level  = QRERRORCORRECTLEVEL[level]
  @version              = size
  @module_count         = @version * 4 + QRPOSITIONPATTERNLENGTH
  @modules              = Array.new( @module_count )
  @data_list            =
    case mode
    when :mode_number
      QRNumeric.new( @data )
    when :mode_alpha_numk
      QRAlphanumeric.new( @data )
    else
      QR8bitByte.new( @data )
    end

  @data_cache           = nil
  self.make
end

Instance Attribute Details

#module_countObject (readonly)

Returns the value of attribute module_count



165
166
167
# File 'lib/rqrcode/qrcode/qr_code.rb', line 165

def module_count
  @module_count
end

#modulesObject (readonly)

Returns the value of attribute modules



165
166
167
# File 'lib/rqrcode/qrcode/qr_code.rb', line 165

def modules
  @modules
end

#versionObject (readonly)

Returns the value of attribute version



165
166
167
# File 'lib/rqrcode/qrcode/qr_code.rb', line 165

def version
  @version
end

Instance Method Details

#error_correction_levelObject

Return a symbol for current error connection level



293
294
295
# File 'lib/rqrcode/qrcode/qr_code.rb', line 293

def error_correction_level
  QRERRORCORRECTLEVEL.invert[@error_correct_level]
end

#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


243
244
245
246
247
248
# File 'lib/rqrcode/qrcode/qr_code.rb', line 243

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

#modeObject

Return a symbol in QRMODE.keys for current mode used



298
299
300
301
302
303
304
305
306
307
# File 'lib/rqrcode/qrcode/qr_code.rb', line 298

def mode
  case @data_list
  when QRNumeric
    :mode_number
  when QRAlphanumeric
    :mode_alpha_numk
  else
    :mode_8bit_byte
  end
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( :dark => 'E', :light => 'Q') =>
EEEEEEEQEQQEQEQQQEQEQQEEQQEEEEEEE
EQQQQQEQQEEEQQEEEEEEQEEEQQEQQQQQE
EQEEEQEQQEEEEEQEQQQQQQQEEQEQEEEQE


269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/rqrcode/qrcode/qr_code.rb', line 269

def to_s( *args )
  options                = args.extract_options!
  dark                   = options[:dark] || options[:true] || 'x'
  light                  = options[:light] || options[:false] || ' '
  quiet_zone_size        = options[:quiet_zone_size] || 0

  rows = []

  @modules.each do |row|
    cols = light * quiet_zone_size
    row.each do |col|
      cols += (col ? dark : light)
    end
    rows << cols
  end

  quiet_zone_size.times do
    rows.unshift(light * (rows.first.length / light.size))
    rows << light * (rows.first.length / light.size)
  end
  rows.join("\n")
end