qrcode-generator 0.0.1

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,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f2d7b821a8fc2a7dfff87159086c35b312cc8c60
4
+ data.tar.gz: 2a9d3391c772e84bee9a9c33fb6162a17852c051
5
+ SHA512:
6
+ metadata.gz: b523a88d598fc91598c83d295f5058589f6e80f25f0da7a214533c11b7d2af494684bf6a609c5e7e939caf6c245fe3508c03635f600725a6157c54736ac9f8a7
7
+ data.tar.gz: d796385d305c3aed601f32de78fce335875eb6bf1e25b854283a1cca9231723de8f2920d880fd782dfaaba93b19d328ce2ba9f0cafda4c91539b05370474796c
@@ -0,0 +1,67 @@
1
+ #!/usr/bin/env ruby
2
+ $:.push File.expand_path('..', __dir__)
3
+
4
+ require 'qrcode-generator'
5
+
6
+ require 'optparse'
7
+
8
+ ARGV << '--help' if ARGV.empty?
9
+
10
+ options = {}
11
+ OptionParser.new do |opts|
12
+ opts.banner = %Q{
13
+ usage: qrcode-generator <command> [options]
14
+
15
+ the commands are:
16
+ init \tinit workspace in current directory
17
+ run \tstart generator qrcodes
18
+
19
+ the options are:
20
+ }
21
+
22
+ options[:width] = 400
23
+ options[:size] = 4
24
+ options[:border] = 1
25
+ options[:level] = :h
26
+ options[:logo] = true
27
+ options[:logo_denominator] = 3
28
+
29
+ opts.on("--width=400", "chnage qrcode image width") do |n|
30
+ width = n.to_i
31
+ options[:width] = width if width > 0
32
+ end
33
+
34
+ opts.on("--size=4", "change qrcode size") do |n|
35
+ size = n.to_i
36
+ options[:size] = size if size > 0
37
+ end
38
+
39
+ opts.on("--border=1", "change qrcode border width") do |n|
40
+ border = n.to_i
41
+ options[:border] = border if border >= 0
42
+ end
43
+
44
+ opts.on("--level=h", "change qrcode level, support: l,m,q,h") do |n|
45
+ options[:level] = n.to_sym if ["l", "m", "q", "h"].include?(n)
46
+ end
47
+
48
+ opts.on("--logo=true", "if you don't want logo, set false") do |n|
49
+ options[:logo] = false if n=="false"
50
+ end
51
+
52
+ opts.on("--logo_denominator=3", "if set 3, then the logo's width will equal to 1/3 of whole image width") do |n|
53
+ logo_denominator = n.to_i
54
+ options[:logo_denominator] = logo_denominator if logo_denominator > 0
55
+ end
56
+
57
+ end.parse!
58
+
59
+ command = ARGV[0]
60
+ case command
61
+ when "init"
62
+ QrcodeGenerator::Initer.new(Dir.pwd).init
63
+ when "run"
64
+ QrcodeGenerator::Runner.new(Dir.pwd, options).run
65
+ else
66
+ puts "unknown command: #{command}"
67
+ end
@@ -0,0 +1,4 @@
1
+
2
+ require "qrcode-generator/version"
3
+ require "qrcode-generator/initer"
4
+ require "qrcode-generator/runner"
@@ -0,0 +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
+
44
+ end
@@ -0,0 +1,123 @@
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
+ File.readlines(txtPath).each do |line|
33
+ line = line.strip.chomp
34
+ next if line.empty?
35
+
36
+ if reverse_point = line.reverse.index(/[ ,]/)
37
+ point = line.length - reverse_point
38
+ link = line[point..-1]
39
+ title = line[0, point].strip.chomp(",").strip
40
+ title = link if title.empty?
41
+ else
42
+ link = line
43
+ title = link
44
+ end
45
+
46
+ filename = sanitize_filename(title) + ".png"
47
+
48
+ to_qrcode_file(link, filename)
49
+ end
50
+ end
51
+
52
+ def sanitize_filename(basename)
53
+
54
+ basename.strip.gsub(/[\:\*\"\<\>\|]/, '').gsub(/[\\\/\.\?]/, '_')
55
+ end
56
+
57
+ def to_qrcode_file(url,filename)
58
+
59
+ img = draw_qrcode_image(url, options[:width])
60
+
61
+ img.format = "PNG"
62
+
63
+ img.write(File.join(destSpace, filename))
64
+ end
65
+
66
+ def draw_qrcode_image(url, width)
67
+ qr_width = 17 + 4 * options[:size] + 2 * options[:border]
68
+ width = width < qr_width ? qr_width : width
69
+ img = Magick::Image.new(qr_width,qr_width)
70
+ build_qrcode(url).draw(img)
71
+ img = img.resize(width,width,Magick::PointFilter)
72
+
73
+ if options[:logo]
74
+ logo_size = width/(options[:logo_denominator]*1.0)
75
+ build_logo(img, logo_size)
76
+ end
77
+
78
+ img
79
+ end
80
+
81
+ def build_qrcode(url)
82
+ qr = RQRCode::QRCode.new(url, size: options[:size], level: options[:level])
83
+ border = options[:border]
84
+ qrcode = Magick::Draw.new
85
+ qr.modules.each_index do |x|
86
+ qr.modules.each_index do |y|
87
+ qrcode.fill( qr.dark?(x,y) ? 'black' : 'white' )
88
+ qrcode.point(y+border,x+border)
89
+ end
90
+ end
91
+ qrcode
92
+ end
93
+
94
+ def build_logo(img,size)
95
+ logo_filepath = File.join(workspace, "logo.png")
96
+
97
+ if File.exist?(logo_filepath)
98
+ logo = Magick::Image.read(logo_filepath)[0]
99
+ logo = logo.resize(size,size)
100
+
101
+ img.composite!(logo, Magick::CenterGravity, Magick::OverCompositeOp)
102
+ end
103
+
104
+ img
105
+ end
106
+
107
+ def to_qrcode_blob(url,width)
108
+
109
+ img = draw_qrcode_image(url, width)
110
+
111
+ blob = img.to_blob {
112
+ self.format = 'PNG'
113
+ }
114
+ blob
115
+ end
116
+
117
+ def to_qrcode_base64(blob)
118
+ "data:image/png;base64,#{Base64.encode64(blob)}"
119
+ end
120
+
121
+ end
122
+
123
+ end
@@ -0,0 +1,6 @@
1
+
2
+ module QrcodeGenerator
3
+
4
+ Version = "0.0.1"
5
+
6
+ end
@@ -0,0 +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
+
Binary file
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: qrcode-generator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jeffrey
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-07-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rqrcode
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.4.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.4.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: rmagick
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 2.15.4
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 2.15.4
41
+ description: 二维码批量生成工具,支持内嵌Logo
42
+ email:
43
+ - jeffrey6052@163.com
44
+ executables:
45
+ - qrcode-generator
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - bin/qrcode-generator
50
+ - lib/qrcode-generator.rb
51
+ - lib/qrcode-generator/initer.rb
52
+ - lib/qrcode-generator/runner.rb
53
+ - lib/qrcode-generator/version.rb
54
+ - lib/workspace/links.txt
55
+ - lib/workspace/logo.png
56
+ homepage:
57
+ licenses:
58
+ - MIT
59
+ metadata: {}
60
+ post_install_message:
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubyforge_project:
76
+ rubygems_version: 2.5.1
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: 二维码批量生成工具,支持内嵌Logo
80
+ test_files: []