qr4r 0.3.5 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2 @@
1
+ .bundle/
2
+ *~
data/Gemfile CHANGED
@@ -1,2 +1,2 @@
1
- source :rubygems
2
- gemspec
1
+ source 'https://rubygems.org'
2
+ gemspec
@@ -0,0 +1,20 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ qr4r (0.4.0)
5
+ mojo_magick
6
+ rqrcode
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ mojo_magick (0.5.4)
12
+ rake (10.1.0)
13
+ rqrcode (0.4.2)
14
+
15
+ PLATFORMS
16
+ ruby
17
+
18
+ DEPENDENCIES
19
+ qr4r!
20
+ rake
data/README.md CHANGED
@@ -44,6 +44,19 @@ Add a fat border
44
44
 
45
45
  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.
46
46
 
47
+ ## Wanna try it out?
48
+
49
+ Check out the Sinatra test app here: https://github.com/rcode5/qr4r_test_app
50
+
51
+
52
+ ## Versions/Changelog
53
+
54
+ #### 0.4.0
55
+
56
+ * Moved to new mojo_magick gem
57
+ * no longer ruby 1.8.7 compatible
58
+ * fixed issue with border coming out black
59
+
47
60
  ## Authors
48
61
 
49
62
  Original author: [Jon Rogers](http://github.com/bunnymatic) [at rcode5](http://www.rcode5.com)
@@ -0,0 +1,78 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'qr4r'
4
+ require 'optparse'
5
+ require 'ostruct'
6
+
7
+ class CmdlineOpts
8
+
9
+ @opts = nil
10
+
11
+ ALLOWED_FORMATS = %w(jpg jpeg gif tif tiff png)
12
+
13
+ attr_reader :options
14
+
15
+ def initialize(args)
16
+ @options = OpenStruct.new
17
+ @options.format = "gif"
18
+ @options.border = 0
19
+ @options.pixel_size = 10
20
+ @options.verbose = false
21
+
22
+ @opts = OptionParser.new do |opts|
23
+ opts.banner = "Usage: $0 [options] outfile the stuff to encode"
24
+
25
+ opts.separator ""
26
+
27
+ # Mandatory argument.
28
+ opts.on("-f", "--format FILE_FORMAT", ALLOWED_FORMATS, "Output qrcode image format (default: gif)", " (#{ALLOWED_FORMATS.join ', '})") do |fmt|
29
+ @options.format = fmt
30
+ end
31
+ opts.on("-b", "--border N",
32
+ "Render with a border of this size") do |border|
33
+ @options.border = border.to_i
34
+ end
35
+ opts.on("-p", "--pixelsize N",
36
+ "Size for each qrcode pixel") do |px|
37
+ @options.pixel_size = px.to_i
38
+ end
39
+ opts.on("-v", "--[no-]verbose", "Be verbose") do |v|
40
+ @options.verbose = V
41
+ end
42
+
43
+ # No argument, shows at tail. This will print an options summary.
44
+ # Try it and see!
45
+ opts.on_tail("-h", "--help", "Show this message") do
46
+ puts opts
47
+ exit
48
+ end
49
+ end
50
+
51
+ @opts.parse!(args)
52
+
53
+ end # parse()
54
+
55
+ def help
56
+ puts @opts
57
+ end
58
+
59
+ end
60
+
61
+ cmd_options = CmdlineOpts.new(ARGV)
62
+
63
+ if ARGV.length < 2
64
+ cmd_options.help
65
+ else
66
+ outfile = ARGV.shift
67
+ to_encode = ARGV.join ' '
68
+ options = cmd_options.options
69
+
70
+ if options.verbose
71
+ print "Encoding \"#{to_encode}\" to file #{outfile}"
72
+ print " with border #{options.border}" if options.border > 0
73
+ print " and pixel_size #{options.pixel_size}"
74
+ puts " and format #{options.format}"
75
+ end
76
+
77
+ Qr4r::encode(to_encode, outfile, cmd_options.options.marshal_dump)
78
+ end
@@ -25,58 +25,66 @@ module Qr4r
25
25
  # size = 9, max string length = 98
26
26
  # size = 10, max string length = 119
27
27
 
28
+ SIZE_RESTRICTIONS = [0, 7, 14, 24, 34, 44, 58, 64, 84, 98, 119]
29
+
28
30
  def self.encode(str, outfile, *rest)
29
31
  opts = rest[0] if rest && rest.length > 0
30
- opts = opts || {}
31
- if !opts[:size]
32
- opts.merge!({:size => compute_size(str)})
33
- end
34
- if !opts[:pixel_size]
35
- opts.merge!({:pixel_size => 3})
36
- end
37
- qr = RQRCode::QRCode.new(str, opts)
38
- data = []
39
- qr.modules.each_index do |x|
40
- qr.modules.each_index do |y|
41
- if qr.dark?(x,y)
42
- 3.times { data << 0 }
43
- else
44
- 3.times { data << 255 }
32
+ opts ||= {}
33
+ opts.merge!({:size => compute_size(str)}) unless opts[:size]
34
+ opts.merge!({:pixel_size => 3}) unless opts[:pixel_size]
35
+ qr, data = build_qr_code(str, opts)
36
+ create_image(qr,data,outfile,opts)
37
+ end
38
+
39
+ class << self
40
+ private
41
+ def build_qr_code(str,opts)
42
+ qr = RQRCode::QRCode.new(str, opts)
43
+ data = [].tap do |data|
44
+ qr.modules.each_index do |x|
45
+ qr.modules.each_index do |y|
46
+ if qr.dark?(x,y)
47
+ 3.times { data << 0 }
48
+ else
49
+ 3.times { data << 255 }
50
+ end
51
+ end
45
52
  end
46
53
  end
54
+ [qr, data]
47
55
  end
48
- MojoMagick::convert do |c|
49
- d = data.pack 'C'*data.size
50
- c.blob(d, :format => :rgb, :depth => 8, :size => ("%dx%d" % [qr.modules.size, qr.modules.size]))
51
- if opts[:pixel_size]
52
- wd = qr.modules.size * opts[:pixel_size].to_i
53
- c.scale "%dx%d" % [ wd, wd ]
54
- end
55
- if opts[:border]
56
- border = opts[:border].to_i
57
- c.bordercolor '"#ffffff"'
58
- c.border '%dx%d' % [ border, border ]
56
+
57
+ def create_image(qr, data, outfile, opts)
58
+ MojoMagick::convert do |c|
59
+ d = data.pack 'C'*data.size
60
+ c.blob(d, :format => :rgb, :depth => 8, :size => ("%dx%d" % [qr.modules.size, qr.modules.size]))
61
+ if opts[:pixel_size]
62
+ wd = qr.modules.size * opts[:pixel_size].to_i
63
+ c.scale "%dx%d" % [ wd, wd ]
64
+ end
65
+ if opts[:border]
66
+ border = opts[:border].to_i
67
+ c.bordercolor 'white'
68
+ c.border '%dx%d' % [ border, border ]
69
+ end
70
+ c.file outfile
59
71
  end
60
- c.file outfile
61
72
  end
62
- end
63
73
 
64
- private
65
- SIZE_RESTRICTIONS = [0, 7, 14, 24, 34, 44, 58, 64, 84, 98, 119]
66
74
 
67
- def self.compute_size(str)
68
- slen = str.size
69
- size = 4
70
- ii = 0
71
- while ii < SIZE_RESTRICTIONS.length do
72
- if slen < SIZE_RESTRICTIONS[ii]
73
- break
75
+ def compute_size(str)
76
+ slen = str.size
77
+ ii = 0
78
+ while ii < SIZE_RESTRICTIONS.length do
79
+ if slen < SIZE_RESTRICTIONS[ii]
80
+ break
81
+ end
82
+ ii+=1
74
83
  end
75
- ii+=1
76
- end
77
- if ii > 10
78
- raise "Your string is too big for this encoder. It should be less than #{SIZE_RESTRICTIONS.last} characters"
84
+ if ii > 10
85
+ raise "Your string is too big for this encoder. It should be less than #{SIZE_RESTRICTIONS.last} characters"
86
+ end
87
+ return ii
79
88
  end
80
- return ii
81
89
  end
82
90
  end
@@ -1,3 +1,3 @@
1
1
  module Qr4r
2
- VERSION = '0.3.5'
2
+ VERSION = '0.4.0'
3
3
  end
@@ -100,7 +100,7 @@ class Qr4rTest < Test::Unit::TestCase
100
100
  end
101
101
 
102
102
  def test_compute_size
103
- test_sizes = [ 7, 14, 24, 34, 44, 58, 64, 84, 98]
103
+ test_sizes = [ 7, 14, 24, 34, 44, 58, 64, 84, 98]
104
104
  test_sizes.each_with_index do |sz, idx|
105
105
  str = 'a'*(sz-1)
106
106
  assert Qr4r.send(:compute_size, str) == idx+1
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: qr4r
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.5
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-29 00:00:00.000000000 Z
12
+ date: 2013-12-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rqrcode
@@ -66,10 +66,13 @@ executables: []
66
66
  extensions: []
67
67
  extra_rdoc_files: []
68
68
  files:
69
+ - .gitignore
69
70
  - .rvmrc
70
71
  - Gemfile
72
+ - Gemfile.lock
71
73
  - README.md
72
74
  - Rakefile
75
+ - examples/generate_qr4r.rb
73
76
  - init.rb
74
77
  - lib/qr4r.rb
75
78
  - lib/qr4r.rb~
@@ -89,17 +92,23 @@ required_ruby_version: !ruby/object:Gem::Requirement
89
92
  - - ! '>='
90
93
  - !ruby/object:Gem::Version
91
94
  version: '0'
95
+ segments:
96
+ - 0
97
+ hash: -3751263750711033208
92
98
  required_rubygems_version: !ruby/object:Gem::Requirement
93
99
  none: false
94
100
  requirements:
95
101
  - - ! '>='
96
102
  - !ruby/object:Gem::Version
97
103
  version: '0'
104
+ segments:
105
+ - 0
106
+ hash: -3751263750711033208
98
107
  requirements: []
99
108
  rubyforge_project: qr4r
100
109
  rubygems_version: 1.8.23
101
110
  signing_key:
102
111
  specification_version: 3
103
- summary: qr4r-0.3.5
112
+ summary: qr4r-0.4.0
104
113
  test_files:
105
114
  - test/qr4r_test.rb