rqrcode 0.3.4 → 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.
- data/.gitignore +1 -0
- data/CHANGELOG +5 -0
- data/Gemfile +2 -0
- data/{COPYING → LICENSE} +0 -0
- data/README.org +108 -0
- data/Rakefile +14 -0
- data/lib/rqrcode/qrcode/qr_code.rb +60 -37
- data/lib/rqrcode/qrcode/qr_util.rb +104 -90
- data/lib/rqrcode/version.rb +3 -0
- data/rqrcode.gemspec +30 -0
- data/test/{test_data.rb → data.rb} +0 -0
- data/test/{runtest.rb → test_rqrcode.rb} +1 -1
- metadata +63 -25
- data/README +0 -96
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
/pkg/
|
data/CHANGELOG
CHANGED
data/Gemfile
ADDED
data/{COPYING → LICENSE}
RENAMED
File without changes
|
data/README.org
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
* rQRCode, Encode QRCodes
|
2
|
+
|
3
|
+
** Overview
|
4
|
+
|
5
|
+
rQRCode is a library for encoding QR Codes in Ruby. It has a simple interface with all the standard qrcode options. It was adapted from the Javascript library by Kazuhiko Arase.
|
6
|
+
|
7
|
+
Let's clear up some rQRCode stuff.
|
8
|
+
|
9
|
+
- rQRCode is a *standalone library*. It requires no other libraries. Just Ruby!
|
10
|
+
- It is an encoding library. You can't decode QR codes with it.
|
11
|
+
- The interface is simple and assumes you just want to encode a string into a QR code
|
12
|
+
- QR code is trademarked by Denso Wave inc
|
13
|
+
|
14
|
+
** Resources
|
15
|
+
|
16
|
+
- wikipedia:: http://en.wikipedia.org/wiki/QR_Code
|
17
|
+
- Denso-Wave website:: http://www.denso-wave.com/qrcode/index-e.html
|
18
|
+
- kaywa:: http://qrcode.kaywa.com
|
19
|
+
|
20
|
+
** Installing
|
21
|
+
|
22
|
+
You may get the latest stable version from Rubygems.
|
23
|
+
|
24
|
+
: $ gem install rqrcode
|
25
|
+
|
26
|
+
You can also get the source from http://github.com/whomwah/rqrcode
|
27
|
+
|
28
|
+
: $ git clone git://github.com/whomwah/rqrcode.git
|
29
|
+
|
30
|
+
** Loading rQRCode Itself
|
31
|
+
|
32
|
+
You have installed the gem already, yeah?
|
33
|
+
|
34
|
+
: require 'rubygems'
|
35
|
+
: require 'rqrcode'
|
36
|
+
|
37
|
+
** Simple QRCode generation to screen
|
38
|
+
|
39
|
+
: qr = RQRCode::QRCode.new( 'my string to generate', :size => 4, :level => :h )
|
40
|
+
: puts qr.to_s
|
41
|
+
: #
|
42
|
+
: # Prints:
|
43
|
+
: # xxxxxxx x x x x x xx xxxxxxx
|
44
|
+
: # x x xxx xxxxxx xxx x x
|
45
|
+
: # x xxx x xxxxx x xx x xxx x
|
46
|
+
: # ... etc
|
47
|
+
|
48
|
+
** Simple QRCode generation to template (RubyOnRails)
|
49
|
+
|
50
|
+
: # Controller
|
51
|
+
: @qr = RQRCode::QRCode.new( 'my string to generate', :size => 4, :level => :h )
|
52
|
+
:
|
53
|
+
: # View: (minimal styling added)
|
54
|
+
: <style type="text/css">
|
55
|
+
: table {
|
56
|
+
: border-width: 0;
|
57
|
+
: border-style: none;
|
58
|
+
: border-color: #0000ff;
|
59
|
+
: border-collapse: collapse;
|
60
|
+
: }
|
61
|
+
: td {
|
62
|
+
: border-width: 0;
|
63
|
+
: border-style: none;
|
64
|
+
: border-color: #0000ff;
|
65
|
+
: border-collapse: collapse;
|
66
|
+
: padding: 0;
|
67
|
+
: margin: 0;
|
68
|
+
: width: 10px;
|
69
|
+
: height: 10px;
|
70
|
+
: }
|
71
|
+
: td.black { background-color: #000; }
|
72
|
+
: td.white { background-color: #fff; }
|
73
|
+
: </style>
|
74
|
+
:
|
75
|
+
: <table>
|
76
|
+
: <% @qr.modules.each_index do |x| %>
|
77
|
+
: <tr>
|
78
|
+
: <% @qr.modules.each_index do |y| %>
|
79
|
+
: <% if @qr.dark?(x,y) %>
|
80
|
+
: <td class="black"/>
|
81
|
+
: <% else %>
|
82
|
+
: <td class="white"/>
|
83
|
+
: <% end %>
|
84
|
+
: <% end %>
|
85
|
+
: </tr>
|
86
|
+
: <% end %>
|
87
|
+
: </table>
|
88
|
+
|
89
|
+
** Authors
|
90
|
+
|
91
|
+
Original author: Duncan Robertson
|
92
|
+
|
93
|
+
Special thanks to the following people for submitting patches:
|
94
|
+
|
95
|
+
- Gioele Barabucci
|
96
|
+
- Rob la Lau
|
97
|
+
- Chris Mowforth
|
98
|
+
- Tore Darell
|
99
|
+
- Vladislav Gorodetskiy
|
100
|
+
|
101
|
+
** Contributing
|
102
|
+
- Fork the project
|
103
|
+
- Send a pull request
|
104
|
+
- Don't touch the .gemspec, I'll do that when I release a new version
|
105
|
+
|
106
|
+
** Copyright
|
107
|
+
|
108
|
+
MIT Licence (http://www.opensource.org/licenses/mit-license.html)
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'rake/testtask'
|
2
|
+
require 'bundler'
|
3
|
+
|
4
|
+
Bundler::GemHelper.install_tasks
|
5
|
+
|
6
|
+
desc "Run tests"
|
7
|
+
Rake::TestTask.new do |t|
|
8
|
+
t.libs << "lib"
|
9
|
+
t.libs << "test"
|
10
|
+
t.test_files = FileList['test/test_*.rb']
|
11
|
+
t.verbose = true
|
12
|
+
end
|
13
|
+
|
14
|
+
task :default => [:test]
|
@@ -36,6 +36,17 @@ module RQRCode #:nodoc:
|
|
36
36
|
:pattern111 => 7
|
37
37
|
}
|
38
38
|
|
39
|
+
QRMASKCOMPUTATIONS = [
|
40
|
+
Proc.new { |i,j| (i + j) % 2 == 0 },
|
41
|
+
Proc.new { |i,j| i % 2 == 0 },
|
42
|
+
Proc.new { |i,j| j % 3 == 0 },
|
43
|
+
Proc.new { |i,j| (i + j) % 3 == 0 },
|
44
|
+
Proc.new { |i,j| ((i / 2).floor + (j / 3).floor) % 2 == 0 },
|
45
|
+
Proc.new { |i,j| (i * j) % 2 + (i * j) % 3 == 0 },
|
46
|
+
Proc.new { |i,j| ((i * j) % 2 + (i * j) % 3) % 2 == 0 },
|
47
|
+
Proc.new { |i,j| ((i * j) % 3 + (i + j) % 2) % 2 == 0 },
|
48
|
+
]
|
49
|
+
|
39
50
|
# StandardErrors
|
40
51
|
|
41
52
|
class QRCodeArgumentError < ArgumentError; end
|
@@ -69,17 +80,22 @@ module RQRCode #:nodoc:
|
|
69
80
|
# qr = RQRCode::QRCode.new('hello world', :size => 1, :level => :m )
|
70
81
|
#
|
71
82
|
|
72
|
-
def initialize( *args )
|
73
|
-
|
83
|
+
def initialize( string, *args )
|
84
|
+
if !string.is_a? String
|
85
|
+
raise QRCodeArgumentError, "The passed data is #{string.class}, not String"
|
86
|
+
end
|
74
87
|
|
75
|
-
@data = args.shift
|
76
88
|
options = args.extract_options!
|
77
|
-
level = options[:level] || :h
|
89
|
+
level = (options[:level] || :h).to_sym
|
90
|
+
size = options[:size] || 4
|
78
91
|
|
79
|
-
|
92
|
+
if !QRERRORCORRECTLEVEL.has_key?(level)
|
93
|
+
raise QRCodeArgumentError, "Unknown error correction level `#{level.inspect}`"
|
94
|
+
end
|
80
95
|
|
81
|
-
@
|
82
|
-
@
|
96
|
+
@data = string
|
97
|
+
@error_correct_level = QRERRORCORRECTLEVEL[level]
|
98
|
+
@type_number = size
|
83
99
|
@module_count = @type_number * 4 + 17
|
84
100
|
@modules = Array.new( @module_count )
|
85
101
|
@data_list = QR8bitByte.new( @data )
|
@@ -98,7 +114,7 @@ module RQRCode #:nodoc:
|
|
98
114
|
|
99
115
|
def is_dark( row, col )
|
100
116
|
if row < 0 || @module_count <= row || col < 0 || @module_count <= col
|
101
|
-
raise QRCodeRunTimeError, "#{row}
|
117
|
+
raise QRCodeRunTimeError, "Invalid row/column pair: #{row}, #{col}"
|
102
118
|
end
|
103
119
|
@modules[row][col]
|
104
120
|
end
|
@@ -198,7 +214,7 @@ module RQRCode #:nodoc:
|
|
198
214
|
|
199
215
|
( 0...8 ).each do |i|
|
200
216
|
make_impl( true, i )
|
201
|
-
lost_point = QRUtil.
|
217
|
+
lost_point = QRUtil.get_lost_points(self.modules)
|
202
218
|
|
203
219
|
if i == 0 || min_lost_point > lost_point
|
204
220
|
min_lost_point = lost_point
|
@@ -322,6 +338,14 @@ module RQRCode #:nodoc:
|
|
322
338
|
end
|
323
339
|
end
|
324
340
|
|
341
|
+
def QRCode.count_max_data_bits(rs_blocks)
|
342
|
+
max_data_bytes = rs_blocks.reduce(0) do |sum, rs_block|
|
343
|
+
sum + rs_block.data_count
|
344
|
+
end
|
345
|
+
|
346
|
+
return max_data_bytes * 8
|
347
|
+
end
|
348
|
+
|
325
349
|
def QRCode.create_data( type_number, error_correct_level, data_list ) #:nodoc:
|
326
350
|
rs_blocks = QRRSBlock.get_rs_blocks( type_number, error_correct_level )
|
327
351
|
buffer = QRBitBuffer.new
|
@@ -333,17 +357,14 @@ module RQRCode #:nodoc:
|
|
333
357
|
)
|
334
358
|
data.write( buffer )
|
335
359
|
|
336
|
-
|
337
|
-
( 0...rs_blocks.size ).each do |i|
|
338
|
-
total_data_count = total_data_count + rs_blocks[i].data_count
|
339
|
-
end
|
360
|
+
max_data_bits = QRCode.count_max_data_bits(rs_blocks)
|
340
361
|
|
341
|
-
if buffer.get_length_in_bits >
|
362
|
+
if buffer.get_length_in_bits > max_data_bits
|
342
363
|
raise QRCodeRunTimeError,
|
343
|
-
"code length overflow. (#{buffer.get_length_in_bits}>#{
|
364
|
+
"code length overflow. (#{buffer.get_length_in_bits}>#{max_data_bits})"
|
344
365
|
end
|
345
366
|
|
346
|
-
if buffer.get_length_in_bits + 4 <=
|
367
|
+
if buffer.get_length_in_bits + 4 <= max_data_bits
|
347
368
|
buffer.put( 0, 4 )
|
348
369
|
end
|
349
370
|
|
@@ -352,9 +373,9 @@ module RQRCode #:nodoc:
|
|
352
373
|
end
|
353
374
|
|
354
375
|
while true
|
355
|
-
break if buffer.get_length_in_bits >=
|
376
|
+
break if buffer.get_length_in_bits >= max_data_bits
|
356
377
|
buffer.put( QRCode::PAD0, 8 )
|
357
|
-
break if buffer.get_length_in_bits >=
|
378
|
+
break if buffer.get_length_in_bits >= max_data_bits
|
358
379
|
buffer.put( QRCode::PAD1, 8 )
|
359
380
|
end
|
360
381
|
|
@@ -369,50 +390,52 @@ module RQRCode #:nodoc:
|
|
369
390
|
dcdata = Array.new( rs_blocks.size )
|
370
391
|
ecdata = Array.new( rs_blocks.size )
|
371
392
|
|
372
|
-
|
373
|
-
dc_count =
|
374
|
-
ec_count =
|
393
|
+
rs_blocks.each_with_index do |rs_block, r|
|
394
|
+
dc_count = rs_block.data_count
|
395
|
+
ec_count = rs_block.total_count - dc_count
|
375
396
|
max_dc_count = [ max_dc_count, dc_count ].max
|
376
397
|
max_ec_count = [ max_ec_count, ec_count ].max
|
377
|
-
dcdata[r] = Array.new( dc_count )
|
378
398
|
|
379
|
-
|
380
|
-
|
399
|
+
dcdata_block = Array.new(dc_count)
|
400
|
+
dcdata_block.size.times do |i|
|
401
|
+
dcdata_block[i] = 0xff & buffer.buffer[ i + offset ]
|
381
402
|
end
|
403
|
+
dcdata[r] = dcdata_block
|
382
404
|
|
383
405
|
offset = offset + dc_count
|
384
406
|
rs_poly = QRUtil.get_error_correct_polynomial( ec_count )
|
385
407
|
raw_poly = QRPolynomial.new( dcdata[r], rs_poly.get_length - 1 )
|
386
408
|
mod_poly = raw_poly.mod( rs_poly )
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
409
|
+
|
410
|
+
ecdata_block = Array.new(rs_poly.get_length - 1)
|
411
|
+
ecdata_block.size.times do |i|
|
412
|
+
mod_index = i + mod_poly.get_length - ecdata_block.size
|
413
|
+
ecdata_block[i] = mod_index >= 0 ? mod_poly.get( mod_index ) : 0
|
391
414
|
end
|
415
|
+
ecdata[r] = ecdata_block
|
392
416
|
end
|
393
417
|
|
394
|
-
total_code_count = 0
|
395
|
-
|
396
|
-
total_code_count = total_code_count + rs_blocks[i].total_count
|
418
|
+
total_code_count = rs_blocks.reduce(0) do |sum, rs_block|
|
419
|
+
sum + rs_block.total_count
|
397
420
|
end
|
398
421
|
|
399
422
|
data = Array.new( total_code_count )
|
400
423
|
index = 0
|
401
424
|
|
402
|
-
|
403
|
-
|
425
|
+
max_dc_count.times do |i|
|
426
|
+
rs_blocks.size.times do |r|
|
404
427
|
if i < dcdata[r].size
|
428
|
+
data[index] = dcdata[r][i]
|
405
429
|
index += 1
|
406
|
-
data[index-1] = dcdata[r][i]
|
407
430
|
end
|
408
431
|
end
|
409
432
|
end
|
410
433
|
|
411
|
-
|
412
|
-
|
434
|
+
max_ec_count.times do |i|
|
435
|
+
rs_blocks.size.times do |r|
|
413
436
|
if i < ecdata[r].size
|
437
|
+
data[index] = ecdata[r][i]
|
414
438
|
index += 1
|
415
|
-
data[index-1] = ecdata[r][i]
|
416
439
|
end
|
417
440
|
end
|
418
441
|
end
|
@@ -61,6 +61,17 @@ module RQRCode #:nodoc:
|
|
61
61
|
G18 = 1 << 12 | 1 << 11 | 1 << 10 | 1 << 9 | 1 << 8 | 1 << 5 | 1 << 2 | 1 << 0
|
62
62
|
G15_MASK = 1 << 14 | 1 << 12 | 1 << 10 | 1 << 4 | 1 << 1
|
63
63
|
|
64
|
+
DEMERIT_POINTS_1 = 3
|
65
|
+
DEMERIT_POINTS_2 = 3
|
66
|
+
DEMERIT_POINTS_3 = 40
|
67
|
+
DEMERIT_POINTS_4 = 10
|
68
|
+
|
69
|
+
BITS_FOR_MODE = {
|
70
|
+
QRMODE[:mode_number] => [10, 12, 14],
|
71
|
+
QRMODE[:mode_alpha_num] => [9, 11, 13],
|
72
|
+
QRMODE[:mode_8bit_byte] => [8, 16, 16],
|
73
|
+
QRMODE[:mode_kanji] => [8, 10, 12],
|
74
|
+
}
|
64
75
|
|
65
76
|
def QRUtil.get_bch_type_info( data )
|
66
77
|
d = data << 10
|
@@ -98,26 +109,11 @@ module RQRCode #:nodoc:
|
|
98
109
|
|
99
110
|
|
100
111
|
def QRUtil.get_mask( mask_pattern, i, j )
|
101
|
-
|
102
|
-
when QRMASKPATTERN[:pattern000]
|
103
|
-
(i + j) % 2 == 0
|
104
|
-
when QRMASKPATTERN[:pattern001]
|
105
|
-
i % 2 == 0
|
106
|
-
when QRMASKPATTERN[:pattern010]
|
107
|
-
j % 3 == 0
|
108
|
-
when QRMASKPATTERN[:pattern011]
|
109
|
-
(i + j) % 3 == 0
|
110
|
-
when QRMASKPATTERN[:pattern100]
|
111
|
-
((i / 2).floor + (j / 3).floor ) % 2 == 0
|
112
|
-
when QRMASKPATTERN[:pattern101]
|
113
|
-
(i * j) % 2 + (i * j) % 3 == 0
|
114
|
-
when QRMASKPATTERN[:pattern110]
|
115
|
-
( (i * j) % 2 + (i * j) % 3) % 2 == 0
|
116
|
-
when QRMASKPATTERN[:pattern111]
|
117
|
-
( (i * j) % 3 + (i + j) % 2) % 2 == 0
|
118
|
-
else
|
112
|
+
if mask_pattern > QRMASKCOMPUTATIONS.size
|
119
113
|
raise QRCodeRunTimeError, "bad mask_pattern: #{mask_pattern}"
|
120
114
|
end
|
115
|
+
|
116
|
+
return QRMASKCOMPUTATIONS[mask_pattern].call(i, j)
|
121
117
|
end
|
122
118
|
|
123
119
|
|
@@ -132,58 +128,49 @@ module RQRCode #:nodoc:
|
|
132
128
|
end
|
133
129
|
|
134
130
|
|
135
|
-
def QRUtil.get_length_in_bits(
|
136
|
-
if
|
131
|
+
def QRUtil.get_length_in_bits(mode, type)
|
132
|
+
if !QRMODE.value?(mode)
|
133
|
+
raise QRCodeRunTimeError, "Unknown mode: #{mode}"
|
134
|
+
end
|
137
135
|
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
when QRMODE[:mode_alpha_num] then 9
|
142
|
-
when QRMODE[:mode_8bit_byte] then 8
|
143
|
-
when QRMODE[:mode_kanji] then 8
|
144
|
-
else
|
145
|
-
raise QRCodeRunTimeError, "mode: #{mode}"
|
146
|
-
end
|
136
|
+
if type > 40
|
137
|
+
raise QRCodeRunTimeError, "Unknown type: #{type}"
|
138
|
+
end
|
147
139
|
|
148
|
-
|
140
|
+
if 1 <= type && type <= 9
|
141
|
+
# 1 - 9
|
142
|
+
macro_type = 0
|
143
|
+
elsif type <= 26
|
144
|
+
# 10 - 26
|
145
|
+
macro_type = 1
|
146
|
+
elsif type <= 40
|
147
|
+
# 27 - 40
|
148
|
+
macro_type = 2
|
149
|
+
end
|
149
150
|
|
150
|
-
|
151
|
-
|
152
|
-
when QRMODE[:mode_number] then 12
|
153
|
-
when QRMODE[:mode_alpha_num] then 11
|
154
|
-
when QRMODE[:mode_8bit_byte] then 16
|
155
|
-
when QRMODE[:mode_kanji] then 10
|
156
|
-
else
|
157
|
-
raise QRCodeRunTimeError, "mode: #{mode}"
|
158
|
-
end
|
151
|
+
return BITS_FOR_MODE[mode][macro_type]
|
152
|
+
end
|
159
153
|
|
160
|
-
|
154
|
+
def QRUtil.get_lost_points(modules)
|
155
|
+
demerit_points = 0
|
161
156
|
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
when QRMODE[:mode_8bit_byte] then 16
|
167
|
-
when QRMODE[:mode_kanji] then 12
|
168
|
-
else
|
169
|
-
raise QRCodeRunTimeError, "mode: #{mode}"
|
170
|
-
end
|
157
|
+
demerit_points += QRUtil.demerit_points_1_same_color(modules)
|
158
|
+
demerit_points += QRUtil.demerit_points_2_full_blocks(modules)
|
159
|
+
demerit_points += QRUtil.demerit_points_3_dangerous_patterns(modules)
|
160
|
+
demerit_points += QRUtil.demerit_points_4_dark_ratio(modules)
|
171
161
|
|
172
|
-
|
173
|
-
raise QRCodeRunTimeError, "type: #{type}"
|
174
|
-
end
|
162
|
+
return demerit_points
|
175
163
|
end
|
176
164
|
|
177
|
-
|
178
|
-
|
179
|
-
module_count =
|
180
|
-
lost_point = 0
|
165
|
+
def QRUtil.demerit_points_1_same_color(modules)
|
166
|
+
demerit_points = 0
|
167
|
+
module_count = modules.size
|
181
168
|
|
182
169
|
# level1
|
183
|
-
(
|
184
|
-
(
|
170
|
+
(0...module_count).each do |row|
|
171
|
+
(0...module_count).each do |col|
|
185
172
|
same_count = 0
|
186
|
-
dark =
|
173
|
+
dark = modules[row][col]
|
187
174
|
|
188
175
|
( -1..1 ).each do |r|
|
189
176
|
next if row + r < 0 || module_count <= row + r
|
@@ -191,63 +178,90 @@ module RQRCode #:nodoc:
|
|
191
178
|
( -1..1 ).each do |c|
|
192
179
|
next if col + c < 0 || module_count <= col + c
|
193
180
|
next if r == 0 && c == 0
|
194
|
-
if dark ==
|
181
|
+
if dark == modules[row + r][col + c]
|
195
182
|
same_count += 1
|
196
183
|
end
|
197
184
|
end
|
198
185
|
end
|
199
186
|
|
200
187
|
if same_count > 5
|
201
|
-
|
202
|
-
end
|
188
|
+
demerit_points += (DEMERIT_POINTS_1 + same_count - 5)
|
189
|
+
end
|
203
190
|
end
|
204
191
|
end
|
205
192
|
|
193
|
+
return demerit_points
|
194
|
+
end
|
195
|
+
|
196
|
+
def QRUtil.demerit_points_2_full_blocks(modules)
|
197
|
+
demerit_points = 0
|
198
|
+
module_count = modules.size
|
199
|
+
|
206
200
|
# level 2
|
207
|
-
(
|
208
|
-
(
|
201
|
+
(0...(module_count - 1)).each do |row|
|
202
|
+
(0...(module_count - 1)).each do |col|
|
209
203
|
count = 0
|
210
|
-
count
|
211
|
-
count
|
212
|
-
count
|
213
|
-
count
|
214
|
-
|
215
|
-
|
204
|
+
count += 1 if modules[row][col]
|
205
|
+
count += 1 if modules[row + 1][col]
|
206
|
+
count += 1 if modules[row][col + 1]
|
207
|
+
count += 1 if modules[row + 1][col + 1]
|
208
|
+
if (count == 0 || count == 4)
|
209
|
+
demerit_points += DEMERIT_POINTS_2
|
210
|
+
end
|
211
|
+
end
|
216
212
|
end
|
217
213
|
|
214
|
+
return demerit_points
|
215
|
+
end
|
216
|
+
|
217
|
+
def QRUtil.demerit_points_3_dangerous_patterns(modules)
|
218
|
+
demerit_points = 0
|
219
|
+
module_count = modules.size
|
220
|
+
|
218
221
|
# level 3
|
219
|
-
|
220
|
-
(
|
221
|
-
if
|
222
|
-
|
222
|
+
modules.each do |row|
|
223
|
+
(module_count - 6).times do |col_idx|
|
224
|
+
if row[col_idx] &&
|
225
|
+
!row[col_idx + 1] &&
|
226
|
+
row[col_idx + 2] &&
|
227
|
+
row[col_idx + 3] &&
|
228
|
+
row[col_idx + 4] &&
|
229
|
+
!row[col_idx + 5] &&
|
230
|
+
row[col_idx + 6]
|
231
|
+
demerit_points += DEMERIT_POINTS_3
|
223
232
|
end
|
224
233
|
end
|
225
234
|
end
|
226
235
|
|
227
|
-
(
|
228
|
-
(
|
229
|
-
if
|
230
|
-
|
236
|
+
(0...module_count).each do |col|
|
237
|
+
(0...(module_count - 6)).each do |row|
|
238
|
+
if modules[row][col] &&
|
239
|
+
!modules[row + 1][col] &&
|
240
|
+
modules[row + 2][col] &&
|
241
|
+
modules[row + 3][col] &&
|
242
|
+
modules[row + 4][col] &&
|
243
|
+
!modules[row + 5][col] &&
|
244
|
+
modules[row + 6][col]
|
245
|
+
demerit_points += DEMERIT_POINTS_3
|
231
246
|
end
|
232
247
|
end
|
233
248
|
end
|
234
249
|
|
235
|
-
|
236
|
-
|
250
|
+
return demerit_points
|
251
|
+
end
|
237
252
|
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
end
|
243
|
-
end
|
253
|
+
def QRUtil.demerit_points_4_dark_ratio(modules)
|
254
|
+
# level 4
|
255
|
+
dark_count = modules.reduce(0) do |sum, col|
|
256
|
+
sum + col.count(true)
|
244
257
|
end
|
245
258
|
|
246
|
-
ratio =
|
247
|
-
|
259
|
+
ratio = dark_count / (modules.size * modules.size)
|
260
|
+
ratio_delta = (100 * ratio - 50).abs / 5
|
248
261
|
|
249
|
-
|
250
|
-
|
262
|
+
demerit_points = ratio_delta * DEMERIT_POINTS_4
|
263
|
+
return demerit_points
|
264
|
+
end
|
251
265
|
|
252
266
|
end
|
253
267
|
|
data/rqrcode.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "rqrcode/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "rqrcode"
|
7
|
+
s.version = RQRCode::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Duncan Robertson"]
|
10
|
+
s.email = ["duncan@whomwah.com"]
|
11
|
+
s.homepage = "http://whomwah.github.com/rqrcode/"
|
12
|
+
s.summary = "A library to encode QR Codes"
|
13
|
+
s.description = <<EOF
|
14
|
+
rQRCode is a library for encoding QR Codes. The simple
|
15
|
+
interface allows you to create QR Code data structures
|
16
|
+
ready to be displayed in the way you choose.
|
17
|
+
EOF
|
18
|
+
|
19
|
+
s.required_rubygems_version = Gem::Requirement.new('>= 1.3.6')
|
20
|
+
|
21
|
+
s.add_development_dependency "rake"
|
22
|
+
s.add_development_dependency("bundler", ">= 1.0.0")
|
23
|
+
|
24
|
+
s.has_rdoc = true
|
25
|
+
s.extra_rdoc_files = ["README.org", "CHANGELOG", "LICENSE"]
|
26
|
+
s.files = `git ls-files`.split("\n")
|
27
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
28
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
29
|
+
s.require_paths = ["lib"]
|
30
|
+
end
|
File without changes
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rqrcode
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 15
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
- 3
|
9
8
|
- 4
|
10
|
-
|
9
|
+
- 0
|
10
|
+
version: 0.4.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Duncan Robertson
|
@@ -15,30 +15,68 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-08-14 00:00:00 +01:00
|
19
19
|
default_executable:
|
20
|
-
dependencies:
|
21
|
-
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: rake
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :development
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: bundler
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: 23
|
44
|
+
segments:
|
45
|
+
- 1
|
46
|
+
- 0
|
47
|
+
- 0
|
48
|
+
version: 1.0.0
|
49
|
+
type: :development
|
50
|
+
version_requirements: *id002
|
22
51
|
description: |
|
23
52
|
rQRCode is a library for encoding QR Codes. The simple
|
24
53
|
interface allows you to create QR Code data structures
|
25
54
|
ready to be displayed in the way you choose.
|
26
55
|
|
27
|
-
email:
|
56
|
+
email:
|
57
|
+
- duncan@whomwah.com
|
28
58
|
executables: []
|
29
59
|
|
30
60
|
extensions: []
|
31
61
|
|
32
62
|
extra_rdoc_files:
|
33
|
-
- README
|
63
|
+
- README.org
|
34
64
|
- CHANGELOG
|
35
|
-
-
|
65
|
+
- LICENSE
|
36
66
|
files:
|
37
|
-
-
|
67
|
+
- .gitignore
|
68
|
+
- CHANGELOG
|
69
|
+
- Gemfile
|
70
|
+
- LICENSE
|
71
|
+
- README.org
|
72
|
+
- Rakefile
|
73
|
+
- lib/rqrcode.rb
|
74
|
+
- lib/rqrcode/core_ext.rb
|
38
75
|
- lib/rqrcode/core_ext/array.rb
|
39
|
-
- lib/rqrcode/core_ext/
|
76
|
+
- lib/rqrcode/core_ext/array/behavior.rb
|
40
77
|
- lib/rqrcode/core_ext/integer.rb
|
41
|
-
- lib/rqrcode/core_ext.rb
|
78
|
+
- lib/rqrcode/core_ext/integer/bitwise.rb
|
79
|
+
- lib/rqrcode/qrcode.rb
|
42
80
|
- lib/rqrcode/qrcode/qr_8bit_byte.rb
|
43
81
|
- lib/rqrcode/qrcode/qr_bit_buffer.rb
|
44
82
|
- lib/rqrcode/qrcode/qr_code.rb
|
@@ -46,13 +84,10 @@ files:
|
|
46
84
|
- lib/rqrcode/qrcode/qr_polynomial.rb
|
47
85
|
- lib/rqrcode/qrcode/qr_rs_block.rb
|
48
86
|
- lib/rqrcode/qrcode/qr_util.rb
|
49
|
-
- lib/rqrcode/
|
50
|
-
-
|
51
|
-
- test/
|
52
|
-
- test/
|
53
|
-
- README
|
54
|
-
- CHANGELOG
|
55
|
-
- COPYING
|
87
|
+
- lib/rqrcode/version.rb
|
88
|
+
- rqrcode.gemspec
|
89
|
+
- test/data.rb
|
90
|
+
- test/test_rqrcode.rb
|
56
91
|
has_rdoc: true
|
57
92
|
homepage: http://whomwah.github.com/rqrcode/
|
58
93
|
licenses: []
|
@@ -76,16 +111,19 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
76
111
|
requirements:
|
77
112
|
- - ">="
|
78
113
|
- !ruby/object:Gem::Version
|
79
|
-
hash:
|
114
|
+
hash: 23
|
80
115
|
segments:
|
81
|
-
-
|
82
|
-
|
116
|
+
- 1
|
117
|
+
- 3
|
118
|
+
- 6
|
119
|
+
version: 1.3.6
|
83
120
|
requirements: []
|
84
121
|
|
85
|
-
rubyforge_project:
|
86
|
-
rubygems_version: 1.
|
122
|
+
rubyforge_project:
|
123
|
+
rubygems_version: 1.6.2
|
87
124
|
signing_key:
|
88
125
|
specification_version: 3
|
89
126
|
summary: A library to encode QR Codes
|
90
127
|
test_files:
|
91
|
-
- test/
|
128
|
+
- test/data.rb
|
129
|
+
- test/test_rqrcode.rb
|
data/README
DELETED
@@ -1,96 +0,0 @@
|
|
1
|
-
== rQRCode, Encode QRCodes
|
2
|
-
|
3
|
-
rQRCode is a library for encoding QR Codes in Ruby. It has a simple interface with all the standard qrcode options. It was adapted from the Javascript library by Kazuhiko Arase.
|
4
|
-
|
5
|
-
== An Overview
|
6
|
-
|
7
|
-
Let's clear up some rQRCode stuff.
|
8
|
-
|
9
|
-
# rQRCode is a *standalone library*. It requires no other libraries. Just Ruby!
|
10
|
-
# It is an encoding library. You can't decode QR codes with it.
|
11
|
-
# The interface is simple and assumes you just want to encode a string into a QR code
|
12
|
-
# QR code is trademarked by Denso Wave inc
|
13
|
-
|
14
|
-
== Resources
|
15
|
-
|
16
|
-
wikipedia:: http://en.wikipedia.org/wiki/QR_Code
|
17
|
-
Denso-Wave website:: http://www.denso-wave.com/qrcode/index-e.html
|
18
|
-
kaywa:: http://qrcode.kaywa.com
|
19
|
-
|
20
|
-
|
21
|
-
== Installing
|
22
|
-
|
23
|
-
You may get the latest stable version from Rubyforge.
|
24
|
-
|
25
|
-
$ gem install rqrcode
|
26
|
-
|
27
|
-
You can also get the source from http://github.com/whomwah/rqrcode/tree/master
|
28
|
-
|
29
|
-
$ git clone git://github.com/whomwah/rqrcode.git
|
30
|
-
|
31
|
-
|
32
|
-
=== Loading rQRCode Itself
|
33
|
-
|
34
|
-
You have installed the gem already, yeah?
|
35
|
-
|
36
|
-
require 'rubygems'
|
37
|
-
require 'rqrcode'
|
38
|
-
|
39
|
-
=== Simple QRCode generation to screen
|
40
|
-
|
41
|
-
qr = RQRCode::QRCode.new( 'my string to generate', :size => 4, :level => :h )
|
42
|
-
puts qr.to_s
|
43
|
-
#
|
44
|
-
# Prints:
|
45
|
-
# xxxxxxx x x x x x xx xxxxxxx
|
46
|
-
# x x xxx xxxxxx xxx x x
|
47
|
-
# x xxx x xxxxx x xx x xxx x
|
48
|
-
# ... etc
|
49
|
-
|
50
|
-
=== Simple QRCode generation to view (RubyOnRails)
|
51
|
-
|
52
|
-
<b>Controller:</b>
|
53
|
-
@qr = RQRCode::QRCode.new( 'my string to generate', :size => 4, :level => :h )
|
54
|
-
|
55
|
-
<b>View: (minimal styling added)</b>
|
56
|
-
<style type="text/css">
|
57
|
-
table {
|
58
|
-
border-width: 0;
|
59
|
-
border-style: none;
|
60
|
-
border-color: #0000ff;
|
61
|
-
border-collapse: collapse;
|
62
|
-
}
|
63
|
-
td {
|
64
|
-
border-width: 0;
|
65
|
-
border-style: none;
|
66
|
-
border-color: #0000ff;
|
67
|
-
border-collapse: collapse;
|
68
|
-
padding: 0;
|
69
|
-
margin: 0;
|
70
|
-
width: 10px;
|
71
|
-
height: 10px;
|
72
|
-
}
|
73
|
-
td.black { background-color: #000; }
|
74
|
-
td.white { background-color: #fff; }
|
75
|
-
</style>
|
76
|
-
|
77
|
-
<table>
|
78
|
-
<% @qr.modules.each_index do |x| %>
|
79
|
-
<tr>
|
80
|
-
<% @qr.modules.each_index do |y| %>
|
81
|
-
<% if @qr.dark?(x,y) %>
|
82
|
-
<td class="black"/>
|
83
|
-
<% else %>
|
84
|
-
<td class="white"/>
|
85
|
-
<% end %>
|
86
|
-
<% end %>
|
87
|
-
</tr>
|
88
|
-
<% end %>
|
89
|
-
</table>
|
90
|
-
|
91
|
-
== Contact
|
92
|
-
|
93
|
-
Author:: Duncan Robertson
|
94
|
-
Email:: duncan@whomwah.com
|
95
|
-
Home Page:: http://whomwah.com
|
96
|
-
License:: MIT Licence (http://www.opensource.org/licenses/mit-license.html)
|