Skip to content

Instantly share code, notes, and snippets.

@l3kn
Created December 19, 2018 21:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save l3kn/2c38ad3cd9cc73b72fe0bc800f9ce19f to your computer and use it in GitHub Desktop.
Save l3kn/2c38ad3cd9cc73b72fe0bc800f9ce19f to your computer and use it in GitHub Desktop.
qr.cr
require "qrencode"
require "stumpy_png"
include StumpyPNG
qr = QRencode::QRcode.new("this is my input string")
margin = 20
size_with_margin = 300
size = size_with_margin - (margin * 2)
scale = size / qr.width
leftover = size - (scale * qr.width)
add_to_each_nth = qr.width / leftover
puts "Size: #{size} Scale: #{scale} Final width: #{size_with_margin}"
canvas = Canvas.new(size_with_margin, size_with_margin) do |x, y|
if margin <= x < (size + margin) && margin <= y < (size + margin)
x -= margin
y -= margin
block_x = x // scale
block_y = y // scale
used_leftovers_x = [block_x / add_to_each_nth, leftover].min
used_leftovers_y = [block_y / add_to_each_nth, leftover].min
x -= used_leftovers_x
y -= used_leftovers_y
x = x // scale
y = y // scale
if QRencode::Util.black?(qr.data[x + qr.width * y])
RGBA::BLACK
else
RGBA::WHITE
end
else
RGBA::BLUE
end
end
StumpyPNG.write(canvas, "test.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment