qr4r 0.3.5 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/Gemfile +2 -2
- data/Gemfile.lock +20 -0
- data/README.md +13 -0
- data/examples/generate_qr4r.rb +78 -0
- data/lib/qr4r.rb +50 -42
- data/lib/qr4r/version.rb +1 -1
- data/test/qr4r_test.rb +1 -1
- metadata +12 -3
data/.gitignore
ADDED
data/Gemfile
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
source
|
2
|
-
gemspec
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
gemspec
|
data/Gemfile.lock
ADDED
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
|
data/lib/qr4r.rb
CHANGED
@@ -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
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
qr.
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
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
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
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
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
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
|
76
|
-
|
77
|
-
|
78
|
-
|
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
|
data/lib/qr4r/version.rb
CHANGED
data/test/qr4r_test.rb
CHANGED
@@ -100,7 +100,7 @@ class Qr4rTest < Test::Unit::TestCase
|
|
100
100
|
end
|
101
101
|
|
102
102
|
def test_compute_size
|
103
|
-
test_sizes =
|
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.
|
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:
|
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.
|
112
|
+
summary: qr4r-0.4.0
|
104
113
|
test_files:
|
105
114
|
- test/qr4r_test.rb
|