qrcode-generator 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/qrcode-generator +77 -73
- data/lib/qrcode-generator.rb +3 -3
- data/lib/qrcode-generator/initer.rb +43 -43
- data/lib/qrcode-generator/runner.rb +130 -130
- data/lib/qrcode-generator/version.rb +5 -5
- data/lib/workspace/links.txt +20 -20
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1411dca3b267e8fbac4620ff52cec07538e5f87b
|
4
|
+
data.tar.gz: ff9eb98b3f4d59c40526a23d98b36aa7b45ebcb1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0a8d87be9bf4d107080d96e57b198b2a05702a716d425a07f0c4fd5a54eb707f3b8024827880fcf2896fe9f92f3642560c1f566ebda1c7cea830a56305ea78d3
|
7
|
+
data.tar.gz: 361b0e7451638d8b4b3fd25a1efb5ab749089d27607b33c46c00fbafade7aebb8d899cc121c4ffc3abaf7d3171a8d76077141b33f17dd5428afa51412039aa26
|
data/bin/qrcode-generator
CHANGED
@@ -1,73 +1,77 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
the
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
options[:
|
27
|
-
options[:
|
28
|
-
options[:
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
options[:
|
47
|
-
end
|
48
|
-
|
49
|
-
opts.on("--
|
50
|
-
options[:
|
51
|
-
end
|
52
|
-
|
53
|
-
opts.on("--
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
Encoding.default_internal='utf-8'
|
4
|
+
Encoding.default_external='utf-8'
|
5
|
+
|
6
|
+
$:.unshift File.expand_path('../lib', __dir__)
|
7
|
+
|
8
|
+
require 'qrcode-generator'
|
9
|
+
|
10
|
+
require 'optparse'
|
11
|
+
|
12
|
+
ARGV << '--help' if ARGV.empty?
|
13
|
+
|
14
|
+
options = {}
|
15
|
+
OptionParser.new do |opts|
|
16
|
+
opts.banner = %Q{
|
17
|
+
usage: qrcode-generator <command> [options]
|
18
|
+
|
19
|
+
the commands are:
|
20
|
+
init \tinit workspace in current directory
|
21
|
+
run \tstart generator qrcodes
|
22
|
+
|
23
|
+
the options are:
|
24
|
+
}
|
25
|
+
|
26
|
+
options[:width] = 400
|
27
|
+
options[:size] = 4
|
28
|
+
options[:border] = 1
|
29
|
+
options[:level] = :h
|
30
|
+
options[:logo] = true
|
31
|
+
options[:logo_denominator] = 3
|
32
|
+
options[:tag] = nil
|
33
|
+
|
34
|
+
opts.on("--width=400", "chnage qrcode image width") do |n|
|
35
|
+
width = n.to_i
|
36
|
+
options[:width] = width if width > 0
|
37
|
+
end
|
38
|
+
|
39
|
+
opts.on("--size=4", "change qrcode size") do |n|
|
40
|
+
size = n.to_i
|
41
|
+
options[:size] = size if size > 0
|
42
|
+
end
|
43
|
+
|
44
|
+
opts.on("--border=1", "change qrcode border width") do |n|
|
45
|
+
border = n.to_i
|
46
|
+
options[:border] = border if border >= 0
|
47
|
+
end
|
48
|
+
|
49
|
+
opts.on("--level=h", "change qrcode level, support: l,m,q,h") do |n|
|
50
|
+
options[:level] = n.to_sym if ["l", "m", "q", "h"].include?(n)
|
51
|
+
end
|
52
|
+
|
53
|
+
opts.on("--logo=true", "if you don't want logo, set false") do |n|
|
54
|
+
options[:logo] = false if n=="false"
|
55
|
+
end
|
56
|
+
|
57
|
+
opts.on("--logo_denominator=3", "if set 3, then the logo's width will equal to 1/3 of whole image width") do |n|
|
58
|
+
logo_denominator = n.to_i
|
59
|
+
options[:logo_denominator] = logo_denominator if logo_denominator > 0
|
60
|
+
end
|
61
|
+
|
62
|
+
opts.on("--tag=someString", "if set 'someString', will add '(someString)' to the end of filename") do |n|
|
63
|
+
tag = n.to_s.strip
|
64
|
+
options[:tag] = tag if !tag.empty?
|
65
|
+
end
|
66
|
+
|
67
|
+
end.parse!
|
68
|
+
|
69
|
+
command = ARGV[0]
|
70
|
+
case command
|
71
|
+
when "init"
|
72
|
+
QrcodeGenerator::Initer.new(Dir.pwd).init
|
73
|
+
when "run"
|
74
|
+
QrcodeGenerator::Runner.new(Dir.pwd, options).run
|
75
|
+
else
|
76
|
+
puts "unknown command: #{command}"
|
77
|
+
end
|
data/lib/qrcode-generator.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
2
|
-
require "qrcode-generator/version"
|
3
|
-
require "qrcode-generator/initer"
|
1
|
+
|
2
|
+
require "qrcode-generator/version"
|
3
|
+
require "qrcode-generator/initer"
|
4
4
|
require "qrcode-generator/runner"
|
@@ -1,44 +1,44 @@
|
|
1
|
-
|
2
|
-
require 'find'
|
3
|
-
require 'fileutils'
|
4
|
-
|
5
|
-
module QrcodeGenerator
|
6
|
-
|
7
|
-
class Initer
|
8
|
-
|
9
|
-
attr_reader :workspace
|
10
|
-
|
11
|
-
def initialize(workspace)
|
12
|
-
@workspace = workspace
|
13
|
-
end
|
14
|
-
|
15
|
-
def init
|
16
|
-
if !workspace_empty?
|
17
|
-
puts "ERROR: Count not init, because current folder is not empty"
|
18
|
-
return
|
19
|
-
end
|
20
|
-
|
21
|
-
init_workspace
|
22
|
-
end
|
23
|
-
|
24
|
-
def workspace_empty?
|
25
|
-
total_size = 0
|
26
|
-
|
27
|
-
Find.find(workspace) do |path|
|
28
|
-
next if path == workspace or File.basename(path)[0] == ?.
|
29
|
-
total_size += 1
|
30
|
-
end
|
31
|
-
|
32
|
-
total_size == 0
|
33
|
-
end
|
34
|
-
|
35
|
-
#copy files into workspace
|
36
|
-
def init_workspace
|
37
|
-
source = File.expand_path("../workspace", __dir__)
|
38
|
-
|
39
|
-
FileUtils.cp_r("#{source}/.", workspace)
|
40
|
-
end
|
41
|
-
|
42
|
-
end
|
43
|
-
|
1
|
+
|
2
|
+
require 'find'
|
3
|
+
require 'fileutils'
|
4
|
+
|
5
|
+
module QrcodeGenerator
|
6
|
+
|
7
|
+
class Initer
|
8
|
+
|
9
|
+
attr_reader :workspace
|
10
|
+
|
11
|
+
def initialize(workspace)
|
12
|
+
@workspace = workspace
|
13
|
+
end
|
14
|
+
|
15
|
+
def init
|
16
|
+
if !workspace_empty?
|
17
|
+
puts "ERROR: Count not init, because current folder is not empty"
|
18
|
+
return
|
19
|
+
end
|
20
|
+
|
21
|
+
init_workspace
|
22
|
+
end
|
23
|
+
|
24
|
+
def workspace_empty?
|
25
|
+
total_size = 0
|
26
|
+
|
27
|
+
Find.find(workspace) do |path|
|
28
|
+
next if path == workspace or File.basename(path)[0] == ?.
|
29
|
+
total_size += 1
|
30
|
+
end
|
31
|
+
|
32
|
+
total_size == 0
|
33
|
+
end
|
34
|
+
|
35
|
+
#copy files into workspace
|
36
|
+
def init_workspace
|
37
|
+
source = File.expand_path("../workspace", __dir__)
|
38
|
+
|
39
|
+
FileUtils.cp_r("#{source}/.", workspace)
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
44
|
end
|
@@ -1,131 +1,131 @@
|
|
1
|
-
|
2
|
-
require 'fileutils'
|
3
|
-
require 'rmagick'
|
4
|
-
require 'rqrcode'
|
5
|
-
|
6
|
-
module QrcodeGenerator
|
7
|
-
|
8
|
-
class Runner
|
9
|
-
|
10
|
-
attr_reader :workspace, :options, :destSpace
|
11
|
-
|
12
|
-
def initialize(workspace, options)
|
13
|
-
@workspace = workspace
|
14
|
-
@options = options
|
15
|
-
|
16
|
-
time = Time.now
|
17
|
-
|
18
|
-
dayStr = time.strftime('%m-%d')
|
19
|
-
clockStr = time.strftime('%H.%M.%S')
|
20
|
-
@destSpace = File.join(workspace, "qrcode #{dayStr} #{clockStr}")
|
21
|
-
end
|
22
|
-
|
23
|
-
def run
|
24
|
-
txtPath = File.join(workspace, "links.txt")
|
25
|
-
if !File.file?(txtPath)
|
26
|
-
puts "ERROR: Could not find file 'links.txt' in current directory"
|
27
|
-
return
|
28
|
-
end
|
29
|
-
|
30
|
-
FileUtils.mkdir(destSpace) if !File.directory?(destSpace)
|
31
|
-
|
32
|
-
filename_tag = parse_filename_tag
|
33
|
-
|
34
|
-
File.readlines(txtPath).each do |line|
|
35
|
-
line = line.strip.chomp
|
36
|
-
next if line.empty?
|
37
|
-
|
38
|
-
line = line.gsub("\t", " ") #tab to space
|
39
|
-
|
40
|
-
if reverse_point = line.reverse.index(/[ ,]/)
|
41
|
-
point = line.length - reverse_point
|
42
|
-
link = line[point..-1]
|
43
|
-
title = line[0, point].strip.chomp(",").strip
|
44
|
-
title = link if title.empty?
|
45
|
-
else
|
46
|
-
link = line
|
47
|
-
title = link
|
48
|
-
end
|
49
|
-
|
50
|
-
filename = sanitize_filename(title) + filename_tag + ".png"
|
51
|
-
|
52
|
-
to_qrcode_file(link, filename)
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
def parse_filename_tag
|
57
|
-
options[:tag] ? "(#{options[:tag]})" : ""
|
58
|
-
end
|
59
|
-
|
60
|
-
def sanitize_filename(basename)
|
61
|
-
|
62
|
-
basename.strip.gsub(/[\:\*\"\<\>\|]/, '').gsub(/[\\\/\.\?]/, '_')
|
63
|
-
end
|
64
|
-
|
65
|
-
def to_qrcode_file(url,filename)
|
66
|
-
|
67
|
-
img = draw_qrcode_image(url, options[:width])
|
68
|
-
|
69
|
-
img.format = "PNG"
|
70
|
-
|
71
|
-
img.write(File.join(destSpace, filename))
|
72
|
-
end
|
73
|
-
|
74
|
-
def draw_qrcode_image(url, width)
|
75
|
-
qr_width = 17 + 4 * options[:size] + 2 * options[:border]
|
76
|
-
width = width < qr_width ? qr_width : width
|
77
|
-
img = Magick::Image.new(qr_width,qr_width)
|
78
|
-
build_qrcode(url).draw(img)
|
79
|
-
img = img.resize(width,width,Magick::PointFilter)
|
80
|
-
|
81
|
-
if options[:logo]
|
82
|
-
logo_size = width/(options[:logo_denominator]*1.0)
|
83
|
-
build_logo(img, logo_size)
|
84
|
-
end
|
85
|
-
|
86
|
-
img
|
87
|
-
end
|
88
|
-
|
89
|
-
def build_qrcode(url)
|
90
|
-
qr = RQRCode::QRCode.new(url, size: options[:size], level: options[:level])
|
91
|
-
border = options[:border]
|
92
|
-
qrcode = Magick::Draw.new
|
93
|
-
qr.modules.each_index do |x|
|
94
|
-
qr.modules.each_index do |y|
|
95
|
-
qrcode.fill( qr.dark?(x,y) ? 'black' : 'white' )
|
96
|
-
qrcode.point(y+border,x+border)
|
97
|
-
end
|
98
|
-
end
|
99
|
-
qrcode
|
100
|
-
end
|
101
|
-
|
102
|
-
def build_logo(img,size)
|
103
|
-
logo_filepath = File.join(workspace, "logo.png")
|
104
|
-
|
105
|
-
if File.exist?(logo_filepath)
|
106
|
-
logo = Magick::Image.read(logo_filepath)[0]
|
107
|
-
logo = logo.resize(size,size)
|
108
|
-
|
109
|
-
img.composite!(logo, Magick::CenterGravity, Magick::OverCompositeOp)
|
110
|
-
end
|
111
|
-
|
112
|
-
img
|
113
|
-
end
|
114
|
-
|
115
|
-
def to_qrcode_blob(url,width)
|
116
|
-
|
117
|
-
img = draw_qrcode_image(url, width)
|
118
|
-
|
119
|
-
blob = img.to_blob {
|
120
|
-
self.format = 'PNG'
|
121
|
-
}
|
122
|
-
blob
|
123
|
-
end
|
124
|
-
|
125
|
-
def to_qrcode_base64(blob)
|
126
|
-
"data:image/png;base64,#{Base64.encode64(blob)}"
|
127
|
-
end
|
128
|
-
|
129
|
-
end
|
130
|
-
|
1
|
+
|
2
|
+
require 'fileutils'
|
3
|
+
require 'rmagick'
|
4
|
+
require 'rqrcode'
|
5
|
+
|
6
|
+
module QrcodeGenerator
|
7
|
+
|
8
|
+
class Runner
|
9
|
+
|
10
|
+
attr_reader :workspace, :options, :destSpace
|
11
|
+
|
12
|
+
def initialize(workspace, options)
|
13
|
+
@workspace = workspace
|
14
|
+
@options = options
|
15
|
+
|
16
|
+
time = Time.now
|
17
|
+
|
18
|
+
dayStr = time.strftime('%m-%d')
|
19
|
+
clockStr = time.strftime('%H.%M.%S')
|
20
|
+
@destSpace = File.join(workspace, "qrcode #{dayStr} #{clockStr}")
|
21
|
+
end
|
22
|
+
|
23
|
+
def run
|
24
|
+
txtPath = File.join(workspace, "links.txt")
|
25
|
+
if !File.file?(txtPath)
|
26
|
+
puts "ERROR: Could not find file 'links.txt' in current directory"
|
27
|
+
return
|
28
|
+
end
|
29
|
+
|
30
|
+
FileUtils.mkdir(destSpace) if !File.directory?(destSpace)
|
31
|
+
|
32
|
+
filename_tag = parse_filename_tag
|
33
|
+
|
34
|
+
File.readlines(txtPath).each do |line|
|
35
|
+
line = line.strip.chomp
|
36
|
+
next if line.empty?
|
37
|
+
|
38
|
+
line = line.gsub("\t", " ") #tab to space
|
39
|
+
|
40
|
+
if reverse_point = line.reverse.index(/[ ,]/)
|
41
|
+
point = line.length - reverse_point
|
42
|
+
link = line[point..-1]
|
43
|
+
title = line[0, point].strip.chomp(",").strip
|
44
|
+
title = link if title.empty?
|
45
|
+
else
|
46
|
+
link = line
|
47
|
+
title = link
|
48
|
+
end
|
49
|
+
|
50
|
+
filename = sanitize_filename(title) + filename_tag + ".png"
|
51
|
+
|
52
|
+
to_qrcode_file(link, filename)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def parse_filename_tag
|
57
|
+
options[:tag] ? "(#{options[:tag]})" : ""
|
58
|
+
end
|
59
|
+
|
60
|
+
def sanitize_filename(basename)
|
61
|
+
|
62
|
+
basename.strip.gsub(/[\:\*\"\<\>\|]/, '').gsub(/[\\\/\.\?]/, '_')
|
63
|
+
end
|
64
|
+
|
65
|
+
def to_qrcode_file(url,filename)
|
66
|
+
|
67
|
+
img = draw_qrcode_image(url, options[:width])
|
68
|
+
|
69
|
+
img.format = "PNG"
|
70
|
+
|
71
|
+
img.write(File.join(destSpace, filename))
|
72
|
+
end
|
73
|
+
|
74
|
+
def draw_qrcode_image(url, width)
|
75
|
+
qr_width = 17 + 4 * options[:size] + 2 * options[:border]
|
76
|
+
width = width < qr_width ? qr_width : width
|
77
|
+
img = Magick::Image.new(qr_width,qr_width)
|
78
|
+
build_qrcode(url).draw(img)
|
79
|
+
img = img.resize(width,width,Magick::PointFilter)
|
80
|
+
|
81
|
+
if options[:logo]
|
82
|
+
logo_size = width/(options[:logo_denominator]*1.0)
|
83
|
+
build_logo(img, logo_size)
|
84
|
+
end
|
85
|
+
|
86
|
+
img
|
87
|
+
end
|
88
|
+
|
89
|
+
def build_qrcode(url)
|
90
|
+
qr = RQRCode::QRCode.new(url, size: options[:size], level: options[:level])
|
91
|
+
border = options[:border]
|
92
|
+
qrcode = Magick::Draw.new
|
93
|
+
qr.modules.each_index do |x|
|
94
|
+
qr.modules.each_index do |y|
|
95
|
+
qrcode.fill( qr.dark?(x,y) ? 'black' : 'white' )
|
96
|
+
qrcode.point(y+border,x+border)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
qrcode
|
100
|
+
end
|
101
|
+
|
102
|
+
def build_logo(img,size)
|
103
|
+
logo_filepath = File.join(workspace, "logo.png")
|
104
|
+
|
105
|
+
if File.exist?(logo_filepath)
|
106
|
+
logo = Magick::Image.read(logo_filepath)[0]
|
107
|
+
logo = logo.resize(size,size)
|
108
|
+
|
109
|
+
img.composite!(logo, Magick::CenterGravity, Magick::OverCompositeOp)
|
110
|
+
end
|
111
|
+
|
112
|
+
img
|
113
|
+
end
|
114
|
+
|
115
|
+
def to_qrcode_blob(url,width)
|
116
|
+
|
117
|
+
img = draw_qrcode_image(url, width)
|
118
|
+
|
119
|
+
blob = img.to_blob {
|
120
|
+
self.format = 'PNG'
|
121
|
+
}
|
122
|
+
blob
|
123
|
+
end
|
124
|
+
|
125
|
+
def to_qrcode_base64(blob)
|
126
|
+
"data:image/png;base64,#{Base64.encode64(blob)}"
|
127
|
+
end
|
128
|
+
|
129
|
+
end
|
130
|
+
|
131
131
|
end
|
@@ -1,6 +1,6 @@
|
|
1
|
-
|
2
|
-
module QrcodeGenerator
|
3
|
-
|
4
|
-
Version = "0.0.
|
5
|
-
|
1
|
+
|
2
|
+
module QrcodeGenerator
|
3
|
+
|
4
|
+
Version = "0.0.5"
|
5
|
+
|
6
6
|
end
|
data/lib/workspace/links.txt
CHANGED
@@ -1,20 +1,20 @@
|
|
1
|
-
|
2
|
-
http://baidu.com
|
3
|
-
|
4
|
-
a1.这一行是自定义文件名,http://weibo.com
|
5
|
-
|
6
|
-
a2.上一行用英文逗号分隔,也可以用空格分隔 http://tmall.com
|
7
|
-
|
8
|
-
a3.一行一个, 空行会被忽略 http://zhihu.com
|
9
|
-
|
10
|
-
a4.多余的空格不影响识别 http://z.cn
|
11
|
-
|
12
|
-
最后一个值 会解析成 链接,前面 的值 会合并成 文件名 http://taobao.com
|
13
|
-
|
14
|
-
,, , , , 测试兼容性, , , , , , , , http://stackoverflow.com
|
15
|
-
|
16
|
-
, http://renren.com
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
1
|
+
|
2
|
+
http://baidu.com
|
3
|
+
|
4
|
+
a1.这一行是自定义文件名,http://weibo.com
|
5
|
+
|
6
|
+
a2.上一行用英文逗号分隔,也可以用空格分隔 http://tmall.com
|
7
|
+
|
8
|
+
a3.一行一个, 空行会被忽略 http://zhihu.com
|
9
|
+
|
10
|
+
a4.多余的空格不影响识别 http://z.cn
|
11
|
+
|
12
|
+
最后一个值 会解析成 链接,前面 的值 会合并成 文件名 http://taobao.com
|
13
|
+
|
14
|
+
,, , , , 测试兼容性, , , , , , , , http://stackoverflow.com
|
15
|
+
|
16
|
+
, http://renren.com
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
|