Permalink
Browse files

Simplifies PNG export, removes ChunkyPNG resizing

  • Loading branch information...
bjedrocha authored and Björn Blomqvist committed Apr 21, 2015
1 parent bf617f6 commit 4a36543008627b8549ead7fd10b8b35a191931b7
Showing with 56 additions and 15 deletions.
  1. +56 −15 lib/rqrcode/export/png.rb
View
@@ -8,10 +8,25 @@ module PNG
# Render the PNG from the Qrcode.
#
# Options:
# module_px_size - Image size, in pixels.
# There are two sizing algoritams.
#
# - Original that can result in blurry and hard to scan images
# - Google's Chart API inspired sizing that risizes the module size to fit within the given image size.
#
# The Googleis one will be used when no options are given or when the new size option is used.
#
# Options:
# fill - Background ChunkyPNG::Color, defaults to 'white'
# color - Foreground ChunkyPNG::Color, defaults to 'black'
#
# *Googleis*
# size - Total size of PNG in pixels. The module size is calculated so it fits. (defaults to 90)
# border_modules - Width of white border around in modules. (defaults to 4).
#
# -- DONT USE border_modules OPTION UNLESS YOU KNOW ABOUT THE QUIET ZONE NEEDS OF QR CODES --
#
# *Original*
# module_px_size - Image size, in pixels.
# border - Border thickness, in pixels
#
# It first creates an image where 1px = 1 module, then resizes.
@@ -24,30 +39,53 @@ def as_png(options = {})
:resize_exactly_to => false,
:fill => 'white',
:color => 'black',
:size => 120,
:border_modules => 4,
:file => false,
:module_px_size => 6
}
googleis = options.length == 0 || (options[:size] != nil)
options = default_img_options.merge(options) # reverse_merge
fill = ChunkyPNG::Color(options[:fill])
color = ChunkyPNG::Color(options[:color])
output_file = options[:file]
border = options[:border_modules]
total_border = border * 2
module_px_size = if options[:resize_gte_to]
(options[:resize_gte_to].to_f / (self.module_count + total_border).to_f).ceil.to_i
module_px_size = nil
border_px = nil
png = nil
if googleis
total_image_size = options[:size]
border_modules = options[:border_modules]
module_px_size = (total_image_size.to_f / (self.module_count + 2 * border_modules).to_f).floor.to_i
img_size = module_px_size * self.module_count
remaining = total_image_size - img_size
border_px = (remaining / 2.0).floor.to_i
png = ChunkyPNG::Image.new(total_image_size, total_image_size, fill)
else
options[:module_px_size]
end
border_px = border * module_px_size
total_border_px = border_px * 2
resize_to = options[:resize_exactly_to]
border = options[:border_modules]
total_border = border * 2
module_px_size = if options[:resize_gte_to]
(options[:resize_gte_to].to_f / (self.module_count + total_border).to_f).ceil.to_i
else
options[:module_px_size]
end
border_px = border * module_px_size
total_border_px = border_px * 2
resize_to = options[:resize_exactly_to]
img_size = module_px_size * self.module_count
total_img_size = img_size + total_border_px
img_size = module_px_size * self.module_count
total_img_size = img_size + total_border_px
png = ChunkyPNG::Image.new(total_img_size, total_img_size, fill)
png = ChunkyPNG::Image.new(total_img_size, total_img_size, fill)
end
self.modules.each_index do |x|
self.modules.each_index do |y|
@@ -60,8 +98,11 @@ def as_png(options = {})
end
end
end
if !googleis && resize_to
png = png.resize(resize_to, resize_to)
end
png = png.resize(resize_to, resize_to) if resize_to
if output_file
png.save(output_file,{ :color_mode => ChunkyPNG::COLOR_GRAYSCALE, :bit_depth =>1})

0 comments on commit 4a36543

Please sign in to comment.