Created
December 30, 2011 23:57
-
-
Save andrew/1542116 to your computer and use it in GitHub Desktop.
QR png generator
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 'rubygems' | |
require 'rqrcode_png' | |
QR_CHAR_SIZE_VS_SIZE = [7, 14, 24, 34, 44, 58, 64, 84, 98, 119, 137, 155, 177, 194] | |
def minimum_qr_size_from_string(string) | |
QR_CHAR_SIZE_VS_SIZE.each_with_index do |size, index| | |
return (index + 1) if string.size < size | |
end | |
# If it's particularly big, we'll try and create codes until it accepts | |
i = QR_CHAR_SIZE_VS_SIZE.size | |
begin | |
i += 1 | |
RQRCode::QRCode.new(string, :size => i) | |
return i | |
rescue RQRCode::QRCodeRunTimeError | |
retry | |
end | |
end | |
def qr_png(string) | |
size = minimum_qr_size_from_string(string) | |
qr = RQRCode::QRCode.new(string, :size => size, :level => :h ) | |
png = qr.to_img | |
png.resize(200, 200).save("qr.png") | |
end | |
qr_png('Hello world') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment