qrcoder 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +8 -0
- data/MIT-LICENSE +20 -0
- data/README.md +77 -0
- data/Rakefile +29 -0
- data/lib/rqrcoder.rb +112 -0
- data/lib/rqrcoder/renderers/svg.rb +46 -0
- data/lib/rqrcoder/size_calculator.rb +40 -0
- metadata +74 -0
data/Gemfile
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2011 YOURNAME
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
# Creates QR codes images
|
2
|
+
|
3
|
+
This library is fork of Sam Vincent rqrcode-rails3 https://github.com/samvincent/rqrcode-rails3
|
4
|
+
Dependes on mini_magic and [rqrcode](https://github.com/whomwah/rqrcode)
|
5
|
+
|
6
|
+
This gem supports generating QR images in SVG, BMP, TIF, JPG, GIF and PNG format.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add the following to your +Gemfile+.
|
11
|
+
|
12
|
+
gem 'rqrcoder'
|
13
|
+
|
14
|
+
or
|
15
|
+
|
16
|
+
gem install rqrcoder
|
17
|
+
|
18
|
+
## How to use
|
19
|
+
|
20
|
+
### Image method
|
21
|
+
|
22
|
+
If you use image method you must define output format.
|
23
|
+
|
24
|
+
Allowed formats are
|
25
|
+
|
26
|
+
* png
|
27
|
+
* bmp
|
28
|
+
* svg
|
29
|
+
* jpg
|
30
|
+
* gif
|
31
|
+
* tif
|
32
|
+
|
33
|
+
QRCode.image(text, output, options)
|
34
|
+
|
35
|
+
Examples
|
36
|
+
|
37
|
+
QRCode.image("some text", "/home/user/", :format => [:png, :svg], :filename => "simple_test"
|
38
|
+
QRCode.image("some text", "/home/user/", :format => :png, :filename => "simple_test" , :unit => 12
|
39
|
+
|
40
|
+
### SVG output
|
41
|
+
|
42
|
+
Return svg output
|
43
|
+
|
44
|
+
QRCode.svg(text, output, options)
|
45
|
+
|
46
|
+
Example
|
47
|
+
|
48
|
+
QRCode.svg("some text", "/home/user", :filename => "simple_test", :unit => 12)
|
49
|
+
|
50
|
+
### Other methods
|
51
|
+
|
52
|
+
You can use other methods
|
53
|
+
|
54
|
+
QRCode.bmp(text, output, options)
|
55
|
+
QRCode.png(text, output, options)
|
56
|
+
QRCode.jpg(text, output, options)
|
57
|
+
QRCode.tif(text, output, options)
|
58
|
+
QRCode.gif(text, output, options)
|
59
|
+
|
60
|
+
#### Options:
|
61
|
+
|
62
|
+
* `:size` – This controls how big the QR Code will be. The smallest size will be chosen by default. Set to maintain consistent size.
|
63
|
+
* `:level` – The error correction level, can be:
|
64
|
+
* Level `:l` 7% of code can be restored
|
65
|
+
* Level `:m` 15% of code can be restored
|
66
|
+
* Level `:q` 25% of code can be restored
|
67
|
+
* Level `:h` 30% of code can be restored (default :h)
|
68
|
+
* `:offset` – Padding around the QR Code (e.g. 10)
|
69
|
+
* `:unit` – How many pixels per module (e.g. 11)
|
70
|
+
* `:fill` – Background color (e.g "ffffff" or :white)
|
71
|
+
* `:color` – Foreground color for the code (e.g. "000000" or :black)
|
72
|
+
|
73
|
+
## About
|
74
|
+
|
75
|
+
This library is fork of Sam Vincent [rqrcode-rails3](https://github.com/samvincent/rqrcode-rails3)
|
76
|
+
|
77
|
+
QR codes are encoded by [rqrcode](https://github.com/whomwah/rqrcode)
|
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'rubygems'
|
3
|
+
begin
|
4
|
+
require 'bundler/setup'
|
5
|
+
rescue LoadError
|
6
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
7
|
+
end
|
8
|
+
|
9
|
+
require 'rake'
|
10
|
+
require 'rdoc/task'
|
11
|
+
|
12
|
+
require 'rake/testtask'
|
13
|
+
|
14
|
+
Rake::TestTask.new(:test) do |t|
|
15
|
+
t.libs << 'lib'
|
16
|
+
t.libs << 'test'
|
17
|
+
t.pattern = 'test/**/*_test.rb'
|
18
|
+
t.verbose = false
|
19
|
+
end
|
20
|
+
|
21
|
+
task :default => [:test]
|
22
|
+
|
23
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
24
|
+
rdoc.rdoc_dir = 'rdoc'
|
25
|
+
rdoc.title = 'rqrcoder'
|
26
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
27
|
+
rdoc.rdoc_files.include('README.rdoc')
|
28
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
29
|
+
end
|
data/lib/rqrcoder.rb
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
require 'rqrcode'
|
2
|
+
require 'rqrcoder/size_calculator.rb'
|
3
|
+
require 'rqrcoder/renderers/svg.rb'
|
4
|
+
require 'mini_magick'
|
5
|
+
|
6
|
+
module QRCoder
|
7
|
+
extend SizeCalculator
|
8
|
+
|
9
|
+
class QRCodeError < StandardError; end
|
10
|
+
|
11
|
+
class QRCode
|
12
|
+
IMAGE_FORMATS = [:jpg , :png, :gif, :tif, :bmp]
|
13
|
+
|
14
|
+
class << self
|
15
|
+
|
16
|
+
#
|
17
|
+
# Create qr image or images
|
18
|
+
#
|
19
|
+
# == Parameters
|
20
|
+
#
|
21
|
+
# * text <tt>String</tt> -- String for qrcode
|
22
|
+
# * output <tt>String</tt> -- String for path where to save files
|
23
|
+
# * options <tt>Hash</tt> -- Hash with options
|
24
|
+
#
|
25
|
+
# === Options
|
26
|
+
#
|
27
|
+
# * :format - Array or symbol. Allowed formats are :png, :bmp, :svg, :jpg, :gif and :tif
|
28
|
+
# * :filename - String for filename without extension. Extension will be added automaticly.
|
29
|
+
# * :size - This controls how big the QR Code will be. Smallest size will be chosen by default. Set to maintain consistent size.
|
30
|
+
# * :level - The error correction level, can be:
|
31
|
+
# * Level :l 7% of code can be restored
|
32
|
+
# * Level :m 15% of code can be restored
|
33
|
+
# * Level :q 25% of code can be restored
|
34
|
+
# * Level :h 30% of code can be restored (default :h)
|
35
|
+
# * :offset - Padding around the QR Code (e.g. 10)
|
36
|
+
# * :unit - How many pixels per module (e.g. 11)
|
37
|
+
# * :fill - Background color (e.g "ffffff" or :white)
|
38
|
+
# * :color - Foreground color for the code (e.g. "000000" or :black)
|
39
|
+
#
|
40
|
+
# === Example
|
41
|
+
#
|
42
|
+
# QRCode.image("some text", "/home/user/", :format => [:png, :svg], :filename => "simple_test"
|
43
|
+
# QRCode.image("some text", "/home/user/", :format => :png, :filename => "simple_test" , :unit => 12
|
44
|
+
#
|
45
|
+
def image text, output, options
|
46
|
+
formats = []
|
47
|
+
if options[:format].kind_of? Array
|
48
|
+
formats = options[:format]
|
49
|
+
else
|
50
|
+
formats << options[:format] || :png
|
51
|
+
end
|
52
|
+
filename = options[:filename] || text
|
53
|
+
svg = self.svg(text, options)
|
54
|
+
|
55
|
+
err_f = formats - IMAGE_FORMATS - [:svg]
|
56
|
+
raise QRCodeError.new("#{err_f.join(', ')} is not supported file formats") unless(err_f.empty?)
|
57
|
+
|
58
|
+
begin
|
59
|
+
formats.each do |format|
|
60
|
+
if IMAGE_FORMATS.include?(format)
|
61
|
+
image = MiniMagick::Image.read(svg) { |i| i.format "svg" }
|
62
|
+
image.format format.to_s
|
63
|
+
image.write "#{output}/#{filename}.#{format}"
|
64
|
+
else
|
65
|
+
data = svg
|
66
|
+
file = File.new("#{output}/#{filename}.#{format}", "w")
|
67
|
+
file.write(data)
|
68
|
+
file.close
|
69
|
+
end
|
70
|
+
end
|
71
|
+
rescue
|
72
|
+
raise QRCodeError.new "File saving error"
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
# Create qr code svg output response
|
77
|
+
# * text <tt>String</tt> -- String for qrcode
|
78
|
+
# * options <tt>Hash</tt> -- Hash with options
|
79
|
+
#
|
80
|
+
# === Options
|
81
|
+
#
|
82
|
+
# * :format - Array or symbol. Allowed formats are :png, :bmp, :svg, :jpg, :gif and :tif
|
83
|
+
# * :filename - String for filename without extension. Extension will be added automaticly.
|
84
|
+
# * :size - This controls how big the QR Code will be. Smallest size will be chosen by default. Set to maintain consistent size.
|
85
|
+
# * :level - The error correction level, can be:
|
86
|
+
# * Level :l 7% of code can be restored
|
87
|
+
# * Level :m 15% of code can be restored
|
88
|
+
# * Level :q 25% of code can be restored
|
89
|
+
# * Level :h 30% of code can be restored (default :h)
|
90
|
+
# * :offset - Padding around the QR Code (e.g. 10)
|
91
|
+
# * :unit - How many pixels per module (e.g. 11)
|
92
|
+
# * :fill - Background color (e.g "ffffff" or :white)
|
93
|
+
# * :color - Foreground color for the code (e.g. "000000" or :black)
|
94
|
+
#
|
95
|
+
def svg text, options ={}
|
96
|
+
size = options[:size] || QRCoder.minimum_qr_size_from_string(text)
|
97
|
+
level = options[:level] || :h
|
98
|
+
qrcode = RQRCode::QRCode.new(text, :size => size, :level => level)
|
99
|
+
QRCoder::Renderers::SVG::render(qrcode, options)
|
100
|
+
end
|
101
|
+
|
102
|
+
|
103
|
+
IMAGE_FORMATS.each do |format|
|
104
|
+
define_method format.to_s do |text, output, options|
|
105
|
+
options[:format] = [format]
|
106
|
+
self.image text, output, options
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module QRCoder
|
2
|
+
module Renderers
|
3
|
+
class SVG
|
4
|
+
class << self
|
5
|
+
# Render the SVG from the qrcode string provided from the RQRCode gem
|
6
|
+
# Options:
|
7
|
+
# offset - Padding around the QR Code (e.g. 10)
|
8
|
+
# unit - How many pixels per module (Default: 11)
|
9
|
+
# fill - Background color (e.g "ffffff" or :white)
|
10
|
+
# color - Foreground color for the code (e.g. "000000" or :black)
|
11
|
+
|
12
|
+
def render(qrcode, options={})
|
13
|
+
offset = options[:offset].to_i || 0
|
14
|
+
color = options[:color] || "000"
|
15
|
+
unit = options[:unit] || 11
|
16
|
+
|
17
|
+
# height and width dependent on offset and QR complexity
|
18
|
+
dimension = (qrcode.module_count*unit) + (2*offset)
|
19
|
+
|
20
|
+
xml_tag = %{<?xml version="1.0" standalone="yes"?>}
|
21
|
+
open_tag = %{<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ev="http://www.w3.org/2001/xml-events" width="#{dimension}" height="#{dimension}">}
|
22
|
+
close_tag = "</svg>"
|
23
|
+
|
24
|
+
result = []
|
25
|
+
qrcode.modules.each_index do |c|
|
26
|
+
tmp = []
|
27
|
+
qrcode.modules.each_index do |r|
|
28
|
+
y = c*unit + offset
|
29
|
+
x = r*unit + offset
|
30
|
+
|
31
|
+
next unless qrcode.is_dark(c, r)
|
32
|
+
tmp << %{<rect width="#{unit}" height="#{unit}" x="#{x}" y="#{y}" style="fill:##{color}"/>}
|
33
|
+
end
|
34
|
+
result << tmp.join
|
35
|
+
end
|
36
|
+
|
37
|
+
if options[:fill]
|
38
|
+
result.unshift %{<rect width="#{dimension}" height="#{dimension}" x="0" y="0" style="fill:##{options[:fill]}"/>}
|
39
|
+
end
|
40
|
+
|
41
|
+
svg = [xml_tag, open_tag, result, close_tag].flatten.join("\n")
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module QRCoder
|
2
|
+
module SizeCalculator
|
3
|
+
# size - seems to follow this logic
|
4
|
+
# # | input | modules
|
5
|
+
# | size | created
|
6
|
+
#-------|-------|--------
|
7
|
+
# 1 | 7 | 21
|
8
|
+
# 2 | 14 | 25 (+4)
|
9
|
+
# 3 | 24 | 29 -
|
10
|
+
# 4 | 34 | 33 -
|
11
|
+
# 5 | 44 | 37 -
|
12
|
+
# 6 | 58 | 41 -
|
13
|
+
# 7 | 64 | 45 -
|
14
|
+
# 8 | 84 | 49 -
|
15
|
+
# 9 | 98 | 53 -
|
16
|
+
# 10 | 119 | 57 -
|
17
|
+
# 11 | 137 | 61 -
|
18
|
+
# 12 | 155 | 65 -
|
19
|
+
# 13 | 177 | 69 -
|
20
|
+
# 14 | 194 | 73 -
|
21
|
+
|
22
|
+
QR_CHAR_SIZE_VS_SIZE = [7, 14, 24, 34, 44, 58, 64, 84, 98, 119, 137, 155, 177, 194]
|
23
|
+
|
24
|
+
def minimum_qr_size_from_string(string)
|
25
|
+
QR_CHAR_SIZE_VS_SIZE.each_with_index do |size, index|
|
26
|
+
return (index + 1) if string.size < size
|
27
|
+
end
|
28
|
+
|
29
|
+
# If it's particularly big, we'll try and create codes until it accepts
|
30
|
+
i = QR_CHAR_SIZE_VS_SIZE.size
|
31
|
+
begin
|
32
|
+
i += 1
|
33
|
+
QRCoder::QRCode.new(string, :size => i)
|
34
|
+
return i
|
35
|
+
rescue QRCoder::QRCodeRunTimeError
|
36
|
+
retry
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: qrcoder
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Bojan Milosavljevic
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-05-08 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rqrcode
|
16
|
+
requirement: &2153265160 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.4.2
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *2153265160
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: mini_magick
|
27
|
+
requirement: &2153264660 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '3.0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *2153264660
|
36
|
+
description: Creates QR code files in png, bmp, png, jpg, tif and svg file format.
|
37
|
+
This is fork of sam vincent rqrcode-rails3
|
38
|
+
email: milboj@gmail.com
|
39
|
+
executables: []
|
40
|
+
extensions: []
|
41
|
+
extra_rdoc_files: []
|
42
|
+
files:
|
43
|
+
- lib/rqrcoder/renderers/svg.rb
|
44
|
+
- lib/rqrcoder/size_calculator.rb
|
45
|
+
- lib/rqrcoder.rb
|
46
|
+
- MIT-LICENSE
|
47
|
+
- Rakefile
|
48
|
+
- Gemfile
|
49
|
+
- README.md
|
50
|
+
homepage: http://github.com/milboj/rqrcoder
|
51
|
+
licenses: []
|
52
|
+
post_install_message:
|
53
|
+
rdoc_options: []
|
54
|
+
require_paths:
|
55
|
+
- lib
|
56
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ! '>='
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
requirements: []
|
69
|
+
rubyforge_project:
|
70
|
+
rubygems_version: 1.8.15
|
71
|
+
signing_key:
|
72
|
+
specification_version: 3
|
73
|
+
summary: Creates QR code files
|
74
|
+
test_files: []
|