qr4r-0.4.1/0000755000004100000410000000000013641343346012443 5ustar www-datawww-dataqr4r-0.4.1/Gemfile.lock0000644000004100000410000000043013641343346014662 0ustar www-datawww-dataPATH remote: . specs: qr4r (0.4.0) mojo_magick rqrcode GEM remote: https://rubygems.org/ specs: chunky_png (1.3.5) mojo_magick (0.5.6) rake (11.1.2) rqrcode (0.10.1) chunky_png (~> 1.0) PLATFORMS ruby DEPENDENCIES qr4r! rake qr4r-0.4.1/test/0000755000004100000410000000000013641343346013422 5ustar www-datawww-dataqr4r-0.4.1/test/qr4r_test.rb0000644000004100000410000000704513641343346015704 0ustar www-datawww-datarequire 'test/unit' require File::expand_path(File::join(File::dirname(__FILE__), '..', 'init')) require 'tempfile' class Qr4rTest < Test::Unit::TestCase def test_encode # do something f = Tempfile.new(['qr4r','.png']) Qr4r::encode('whatever yo', f.path) # assert that it worked assert File.exists?(f.path) r = MojoMagick::get_image_size(f.path) assert r[:height] == 25 * 3 assert r[:width] == 25 * 3 end def test_encode_with_size # do something f = Tempfile.new(['qr4r','.png']) Qr4r::encode('whatever yo', f.path, :size => 4) # assert that it worked assert File.exists?(f) r = MojoMagick::get_image_size(f.path) assert r[:height] == 33 * 3 assert r[:width] == 33 * 3 end def test_encode_with_size_and_border # do something f = Tempfile.new(['qr4r','.png']) Qr4r::encode('whatever yo', f.path, :size => 4, :border => 10) # assert that it worked assert File.exists?(f) r = MojoMagick::get_image_size(f.path) assert r[:height] == 33 * 3 + 20 assert r[:width] == 33 * 3 + 20 end def test_encode_with_pixel_size # do something f = Tempfile.new(['qr4r','.png']) Qr4r::encode('whatever yo', f.path, :pixel_size => 5) # assert that it worked assert File.exists?(f) r = MojoMagick::get_image_size(f.path) assert r[:height] == 25 * 5 assert r[:width] == 25 * 5 end def test_encode_with_pixel_size_as_string # do something f = Tempfile.new(['qr4r','.png']) Qr4r::encode('whatever yo', f.path, :pixel_size => '5') # assert that it worked assert File.exists?(f) r = MojoMagick::get_image_size(f.path) assert r[:height] == 25 * 5 assert r[:width] == 25 * 5 end def test_encode_with_size_and_level # do something f = Tempfile.new(['qr4r','.png']) Qr4r::encode('whatever yo', f.path, :size => 4, :level => :m) # assert that it worked assert File.exists?(f) r = MojoMagick::get_image_size(f.path) assert r[:height] == 33 * 3 assert r[:width] == 33 * 3 end def test_a_long_string_with_size_thats_too_small caught = false begin f = Tempfile.new(['qr4r','.png']) Qr4r::encode('this string should also be encodable. don\'t ya think', f.path, :size => 4) rescue caught = true end assert caught, 'Expected an error' end def test_a_long_string_with_size_thats_right f = Tempfile.new(['qr4r','.png']) Qr4r::encode('this string should also be encodable. don\'t ya think', f.path, :size => 10) assert File.exists?(f) r = MojoMagick::get_image_size(f.path) assert r[:height] == 57 * 3 assert r[:width] == 57 * 3 end def test_a_long_string_without_size f = Tempfile.new(['qr4r','.png']) Qr4r::encode('this string should also be encodable. don\'t ya think', f.path) assert File.exists?(f) r = MojoMagick::get_image_size(f.path) assert r[:height] = 41 * 3 assert r[:width] = 41 * 3 end def test_compute_size test_sizes = [ 7, 14, 24, 34, 44, 58, 64, 84, 98] test_sizes.each_with_index do |sz, idx| str = 'a'*(sz-1) assert Qr4r.send(:compute_size, str) == idx+1 str = 'a'*(sz) assert Qr4r.send(:compute_size, str) == idx+2 end end def test_compute_size_too_big str = 'a'*120 caught = false begin Qr4r.send(:compute_size, str) rescue Exception => ex caught = true end assert caught, "Expected exception" end end qr4r-0.4.1/README.md0000644000004100000410000000472513641343346013732 0ustar www-datawww-data# QR Encoding via Ruby - with PNG output Leveraging [rqrcode](http://whomwah.github.com/rqrcode/) and [mojo_magick](http://github.com/rcode5/mojo_magick), we've built a very thin gem that generates QR codes in a png file # Include in your project In your 'Gemfile', add gem 'qr4r' In your code, add require 'qr4r' NOTE: you'll need to have ImageMagick installed wherever you plan to run this. The gem depends on [mojo_magick](http://github.com/rcode5/mojo_magick) which uses ImageMagick commandline operations to build the final PNG image. To use it: Qr4r::encode(input_string, output_file_path, options) *input_string* and *output_file_path* should be strings. Options should a list of hash options keyed by symbols. Possible options are: * :pixel_size - specify the size of each 'black' dot in the qrcode. Default = 3 * :border - specify the number of pixels to use for a white border around the outside. Default = 0 To encode the string 'qr codes are the new hotness' like this: require 'qr4r' s = 'qr codes are the new hotness' fname = s.gsub(/\s+/,"_") + ".qr.png" Qr4r::encode(s, fname) Make a bigger QRCode s = 'big qr codes are the new hotness' fname = s.gsub(/\s+/,"_") + ".qr.png" Qr4r::encode(s, fname, :pixel_size => 5) Add a fat border s = 'big qr codes are the new hotness with a border' fname = s.gsub(/\s+/,"_") + ".qr.png" Qr4r::encode(s, fname, :border => 20) You can also checkout the [examples](https://github.com/rcode5/qr4r/tree/master/examples) folder for a little sample commandline script. NOTE: The current implementation or rQRCode (and therefore this wrapper library) supports encoding up to 119 characters. Beyond that, you'll need something a bit more sophisticated. ## Wanna try it out? Check out the Sinatra test app here: https://github.com/rcode5/qr4r_test_app ## Versions/Changelog #### 0.4.0 * Moved to new mojo_magick gem * no longer ruby 1.8.7 compatible * fixed issue with border coming out black ## Authors Original author: [Jon Rogers](http://github.com/bunnymatic) [at rcode5](http://www.rcode5.com) Thanks to [Duncan Robertson](http://whomwah.github.com/rqrcode/) for writing rQRCode ## Contributing * Fork the project * Write a test for your new feature/method * Send a pull request * Don't bump the version or modify the gemspec. I'll do that when I merge in your mods and release a new version. ## Copyright MIT Licence (http://www.opensource.org/licenses/mit-license.html) qr4r-0.4.1/qr4r.gemspec0000644000004100000410000000146013641343346014701 0ustar www-datawww-data$:.push File.expand_path("../lib", __FILE__) require "qr4r/version" Gem::Specification.new do |s| s.name = "qr4r" s.version = Qr4r::VERSION s.platform = Gem::Platform::RUBY s.authors = ["Jon Rogers"] s.email = ["jon@rcode5.com"] s.homepage = "http://github.com/rcode5/qr4r" s.summary = "qr4r-#{Qr4r::VERSION}" s.description = %q{QR PNG Generator for Ruby. Leveraging RQRCode and MojoMagick modules} s.rubyforge_project = "qr4r" s.files = `git ls-files`.split("\n") s.test_files = `git ls-files -- {test,features}/*`.split("\n") s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } s.require_paths = ["lib"] s.add_dependency('rqrcode') s.add_dependency('mojo_magick') s.add_development_dependency('rake') end qr4r-0.4.1/init.rb0000644000004100000410000000011613641343346013731 0ustar www-datawww-datarequire File::expand_path(File::join(File::dirname(__FILE__), 'lib', 'qr4r')) qr4r-0.4.1/.gitignore0000644000004100000410000000001413641343346014426 0ustar www-datawww-data.bundle/ *~ qr4r-0.4.1/examples/0000755000004100000410000000000013641343346014261 5ustar www-datawww-dataqr4r-0.4.1/examples/generate_qr4r.rb0000755000004100000410000000347113641343346017360 0ustar www-datawww-data#!/usr/bin/env ruby require 'qr4r' require 'optparse' require 'ostruct' class CmdlineOpts @opts = nil ALLOWED_FORMATS = %w(jpg jpeg gif tif tiff png) attr_reader :options def initialize(args) @options = OpenStruct.new @options.format = "gif" @options.border = 0 @options.pixel_size = 10 @options.verbose = false @opts = OptionParser.new do |opts| opts.banner = "Usage: $0 [options] outfile the stuff to encode" opts.separator "" # Mandatory argument. opts.on("-f", "--format FILE_FORMAT", ALLOWED_FORMATS, "Output qrcode image format (default: gif)", " (#{ALLOWED_FORMATS.join ', '})") do |fmt| @options.format = fmt end opts.on("-b", "--border N", "Render with a border of this size") do |border| @options.border = border.to_i end opts.on("-p", "--pixelsize N", "Size for each qrcode pixel") do |px| @options.pixel_size = px.to_i end opts.on("-v", "--[no-]verbose", "Be verbose") do |v| @options.verbose = V end # No argument, shows at tail. This will print an options summary. # Try it and see! opts.on_tail("-h", "--help", "Show this message") do puts opts exit end end @opts.parse!(args) end # parse() def help puts @opts end end cmd_options = CmdlineOpts.new(ARGV) if ARGV.length < 2 cmd_options.help else outfile = ARGV.shift to_encode = ARGV.join ' ' options = cmd_options.options if options.verbose print "Encoding \"#{to_encode}\" to file #{outfile}" print " with border #{options.border}" if options.border > 0 print " and pixel_size #{options.pixel_size}" puts " and format #{options.format}" end Qr4r::encode(to_encode, outfile, cmd_options.options.marshal_dump) end qr4r-0.4.1/.rvmrc0000644000004100000410000000007713641343346013601 0ustar www-datawww-datarvm use --create 1.9.2-p290@qr4r #rvm use --create 1.8.7-p334 qr4r-0.4.1/Rakefile0000644000004100000410000000051013641343346014104 0ustar www-datawww-datarequire 'rubygems' task :default => :test task 'default' => :test desc "Default: run tests" task :test do require 'rake/runtest' files = Dir.glob(File.join(File.dirname(__FILE__), 'test/*_test.rb')) files.each do |f| Rake.run_tests f end end task :build do `rm qr4r-*.gem` puts `gem build qr4r.gemspec` end qr4r-0.4.1/lib/0000755000004100000410000000000013641343346013211 5ustar www-datawww-dataqr4r-0.4.1/lib/qr4r.rb~0000644000004100000410000000102613641343346014623 0ustar www-datawww-datarequire 'rqrcode' require 'mojo_magick' module QR4R def self.encode(str, outfile) qr = RQRCode::QRCode.new(str) data = [] qr.modules.each_index do |x| qr.modules.each_index do |y| if qr.dark?(x,y) 3.times { data << 0 } else 3.times { data << 255 } end end end MojoMagick::convert do |c| c.blob (data.pack 'C'*data.length), :format => :rgb, :depth => 8, :size => "%dx%d" % [qr.modules.size, qr.modules.size] c.file outfile end end end qr4r-0.4.1/lib/qr4r.rb0000644000004100000410000000540413641343346014431 0ustar www-datawww-datarequire 'rqrcode' require 'mojo_magick' module Qr4r # params we use # pixel_size - size of each dot, default = 3 # params we pass to QRCode include :size and :level # size - the size of the qrcode (default 4) # level - the error correction level, can be: # * Level :l 7% of code can be restored # * Level :m 15% of code can be restored # * Level :q 25% of code can be restored # * Level :h 30% of code can be restored # note: if size is not included and 4 appears to be too small for the included string, we'll make it bigger # if you include size, we'll use it, which may lead to an error if the string is too long # Limitations are as follows: # size = 1, max string length = 7 # size = 2, max string length = 14 # size = 3, max string length = 24 # size = 4, max string length = 34 # size = 5, max string length = 44 # size = 6, max string length = 58 # size = 7, max string length = 64 # size = 8, max string length = 84 # size = 9, max string length = 98 # size = 10, max string length = 119 SIZE_RESTRICTIONS = [0, 7, 14, 24, 34, 44, 58, 64, 84, 98, 119] def self.encode(str, outfile, *rest) opts = rest[0] if rest && rest.length > 0 opts ||= {} opts.merge!({:size => compute_size(str)}) unless opts[:size] opts.merge!({:pixel_size => 3}) unless opts[:pixel_size] qr, data = build_qr_code(str, opts) create_image(qr,data,outfile,opts) end class << self private def build_qr_code(str,opts) qr = RQRCode::QRCode.new(str, opts) data = [].tap do |data| qr.modules.each_index do |x| qr.modules.each_index do |y| if qr.dark?(x,y) 3.times { data << 0 } else 3.times { data << 255 } end end end end [qr, data] end def create_image(qr, data, outfile, opts) MojoMagick::convert do |c| d = data.pack 'C'*data.size c.blob(d, :format => :rgb, :depth => 8, :size => ("%dx%d" % [qr.modules.size, qr.modules.size])) if opts[:pixel_size] wd = qr.modules.size * opts[:pixel_size].to_i c.scale "%dx%d" % [ wd, wd ] end if opts[:border] border = opts[:border].to_i c.bordercolor 'white' c.border '%dx%d' % [ border, border ] end c.file outfile end end def compute_size(str) slen = str.size ii = 0 while ii < SIZE_RESTRICTIONS.length do if slen < SIZE_RESTRICTIONS[ii] break end ii+=1 end if ii > 10 raise "Your string is too big for this encoder. It should be less than #{SIZE_RESTRICTIONS.last} characters" end return ii end end end qr4r-0.4.1/lib/qr4r/0000755000004100000410000000000013641343346014101 5ustar www-datawww-dataqr4r-0.4.1/lib/qr4r/version.rb0000644000004100000410000000004413641343346016111 0ustar www-datawww-datamodule Qr4r VERSION = '0.4.1' end qr4r-0.4.1/lib/qr4r/version.rb~0000644000004100000410000000004413641343346016307 0ustar www-datawww-datamodule Qr4r VERSION = '0.4.1' end qr4r-0.4.1/Gemfile0000644000004100000410000000004613641343346013736 0ustar www-datawww-datasource 'https://rubygems.org' gemspec