class RQRCodeCore::QRCode

Creation

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

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

Attributes

module_count[R]
modules[R]
version[R]

Public Class Methods

new(data, *args) click to toggle source

Expects a string or array (for multi-segment encoding) to be parsed in, other args are optional

# data - the string, QRSegment or array of Hashes (with data:, mode: keys) you wish to encode
# size - the size (Integer) of the QR Code (defaults to smallest size needed to encode the data)
# max_size - the max_size (Integer) of the QR Code (default RQRCodeCore::QRUtil.max_size)
# 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 QR Code (defaults to alphanumeric or byte_8bit, depending on the input data, only used when data is a string):
   * :number
   * :alphanumeric
   * :byte_8bit
   * :kanji

qr = RQRCodeCore::QRCode.new('hello world', size: 1, level: :m, mode: :alphanumeric)
segment_qr = QRCodeCore::QRCode.new({ data: 'foo', mode: :byte_8bit })
multi_qr = RQRCodeCore::QRCode.new([{ data: 'foo', mode: :byte_8bit }, { data: 'bar1', mode: :alphanumeric }])
# File lib/rqrcode_core/qrcode/qr_code.rb, line 106
def initialize(data, *args)
  options = extract_options!(args)

  level = (options[:level] || :h).to_sym
  max_size = options[:max_size] || QRUtil.max_size

  @data = case data
  when String
    QRSegment.new(data: data, mode: options[:mode])
  when Array
    raise QRCodeArgumentError, "Array must contain Hashes with :data and :mode keys" unless data.all? { |seg| seg.is_a?(Hash) && %i[data mode].all? { |s| seg.key? s } }
    data.map { |seg| QRSegment.new(**seg) }
  when QRSegment
    data
  else
    raise QRCodeArgumentError, "data must be a String, QRSegment, or an Array"
  end
  @error_correct_level = QRERRORCORRECTLEVEL[level]

  unless @error_correct_level
    raise QRCodeArgumentError, "Unknown error correction level `#{level.inspect}`"
  end

  size = options[:size] || minimum_version(limit: max_size)

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

  @version = size
  @module_count = @version * 4 + QRPOSITIONPATTERNLENGTH
  @modules = Array.new(@module_count)
  @data_list = multi_segment? ? QRMulti.new(@data) : @data.writer
  @data_cache = nil
  make
end

Public Instance Methods

checked?(row, col) click to toggle source

checked? 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.checked?( 10, 10 ) => true
# File lib/rqrcode_core/qrcode/qr_code.rb, line 151
def checked?(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
Also aliased as: dark?
dark?(row, col)
Alias for: checked?
error_correction_level() click to toggle source

Return a symbol for current error connection level

# File lib/rqrcode_core/qrcode/qr_code.rb, line 213
def error_correction_level
  QRERRORCORRECTLEVEL.invert[@error_correct_level]
end
inspect() click to toggle source

Public overide as default inspect is very verbose

RQRCodeCore::QRCode.new('my string to generate', size: 4, level: :h)
=> QRCodeCore: @data='my string to generate', @error_correct_level=2, @version=4, @module_count=33
# File lib/rqrcode_core/qrcode/qr_code.rb, line 208
def inspect
  "QRCodeCore: @data='#{@data}', @error_correct_level=#{@error_correct_level}, @version=#{@version}, @module_count=#{@module_count}"
end
mode() click to toggle source

Return a symbol in QRMODE.keys for current mode used

# File lib/rqrcode_core/qrcode/qr_code.rb, line 223
def mode
  case @data_list
  when QRNumeric
    :mode_number
  when QRAlphanumeric
    :mode_alpha_numk
  else
    :mode_8bit_byte
  end
end
multi_segment?() click to toggle source

Return true if this QR Code includes multiple encoded segments

# File lib/rqrcode_core/qrcode/qr_code.rb, line 218
def multi_segment?
  @data.is_a?(Array)
end
to_s(*args) click to toggle source

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 :dark and :light 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
# File lib/rqrcode_core/qrcode/qr_code.rb, line 179
def to_s(*args)
  options = extract_options!(args)
  dark = options[:dark] || "x"
  light = options[:light] || " "
  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

Private Instance Methods

minimum_version(limit: QRUtil.max_size, version: 1) click to toggle source
# File lib/rqrcode_core/qrcode/qr_code.rb, line 407
def minimum_version(limit: QRUtil.max_size, version: 1)
  raise QRCodeRunTimeError, "Data length exceed maximum capacity of version #{limit}" if version > limit

  max_size_bits = QRMAXBITS[error_correction_level][version - 1]

  size_bits = multi_segment? ? @data.sum { |seg| seg.size(version) } : @data.size(version)

  return version if size_bits < max_size_bits

  minimum_version(limit: limit, version: version + 1)
end