Skip to content

Instantly share code, notes, and snippets.

@andrew
Created December 30, 2011 23: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 andrew/1542116 to your computer and use it in GitHub Desktop.
Save andrew/1542116 to your computer and use it in GitHub Desktop.
QR png generator
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