Created
December 19, 2018 21:57
-
-
Save l3kn/2c38ad3cd9cc73b72fe0bc800f9ce19f to your computer and use it in GitHub Desktop.
qr.cr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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