Skip to content

Instantly share code, notes, and snippets.

@Martin91
Created January 27, 2015 09:09
Show Gist options
  • Select an option

  • Save Martin91/54fd41295112211033ea to your computer and use it in GitHub Desktop.

Select an option

Save Martin91/54fd41295112211033ea to your computer and use it in GitHub Desktop.
determine the size for qrcode
# size - seems to follow this logic
# codes copied from:
# => https://raw.githubusercontent.com/samvincent/rqrcode-rails3/master/lib/rqrcode-rails3/size_calculator.rb
#
# # | input | modules
# | size | created
#-------|-------|--------
# 1 | 7 | 21
# 2 | 14 | 25 (+4)
# 3 | 24 | 29 -
# 4 | 34 | 33 -
# 5 | 44 | 37 -
# 6 | 58 | 41 -
# 7 | 64 | 45 -
# 8 | 84 | 49 -
# 9 | 98 | 53 -
# 10 | 119 | 57 -
# 11 | 137 | 61 -
# 12 | 155 | 65 -
# 13 | 177 | 69 -
# 14 | 194 | 73 -
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment