Sha256: db3b5bceddc0c849f01adfb5a43d08bdea26f91cae4467049194e573ba51568b

Contents?: true

Size: 1.2 KB

Versions: 2

Compression:

Stored size: 1.2 KB

Contents

require 'enumerator'
require 'png'

module QRTools
  class QRCode
    class << self
      def decode *args, &block
        Decoder.decode *args, &block
      end

      def encode *args, &block
        Encoder.encode *args, &block
      end
    end

    def to_matrix
      codes = []
      data.unpack("C#{width ** 2}").each_slice(width) { |row|
        codes << row.map { |x| x & 0x1 == 1 }
      }
      codes
    end

    def to_s
      to_matrix.map { |row|
        row.map { |bit| bit ? '#' : ' ' }.join
      }.join("\n")
    end

    def to_png scale = 4, border = 10
      # Add 2 extra rows / columns for white borders
      size = (width + border) * scale
      canvas = PNG::Canvas.new(size, size, PNG::Color::White)

      scaled_matrix = []
      to_matrix.each do |row|
        scaled_row = row.map { |bit|
          [bit] * scale
        }.flatten
        scaled_matrix += ([scaled_row] * scale)
      end

      factor = border / 2 * scale
      scaled_matrix.each_with_index { |row, i|
        x = i + factor
        row.each_with_index { |bit, j|
          y = j + factor
          canvas[x, y] = bit ? PNG::Color::Black : PNG::Color::White 
        }
      }
      PNG.new(canvas).to_blob
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
qrtools-1.0.0 lib/qrtools/qrcode.rb
qrtools-1.0.1 lib/qrtools/qrcode.rb