class RQRCode::QRCode
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 )
Attributes
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) # 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 )
# 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
Private Class Methods
# File lib/rqrcode/qrcode/qr_code.rb, line 497 def QRCode.count_max_data_bits(rs_blocks) max_data_bytes = rs_blocks.reduce(0) do |sum, rs_block| sum + rs_block.data_count end return max_data_bytes * 8 end
Public Instance Methods
Return a symbol for current error connection level
# File lib/rqrcode/qrcode/qr_code.rb, line 293 def error_correction_level QRERRORCORRECTLEVEL.invert[@error_correct_level] end
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
# 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
Return a symbol in QRMODE.keys for current mode used
# 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
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
# 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
Private Instance Methods
# File lib/rqrcode/qrcode/qr_code.rb, line 318 def prepare_common_patterns @modules.map! { |row| Array.new(@module_count) } place_position_probe_pattern(0, 0) place_position_probe_pattern(@module_count - 7, 0) place_position_probe_pattern(0, @module_count - 7) place_position_adjust_pattern place_timing_pattern @common_patterns = @modules.map(&:clone) end