Ansi QrCodes in Ruby

Linux lets you change the foreground and background colors of the terminal. You can use this to create QR codes.

#!/usr/bin/ruby -w
require 'rubygems'
require 'rqrcode'

margin = 1
qr = RQRCode::QRCode.new(ARGV[0], :size => 4, :level => :h )
res = ""

size = qr.modules.size + margin * 2
white = "33[5;37;47m  "
black = "33[0;34;40m  "
size.times do |x|
  size.times do |y|
    if x < margin || y < margin || x >= size - margin || y >= size - margin
      res += white
    elsif qr.is_dark(x - margin,y - margin)
      res += black
    else
      res += white
    end
  end
  res += "33[0m\n"
end
puts res


Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.