custom_rqrcode 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +12 -0
- data/MIT-LICENSE +20 -0
- data/README.md +52 -0
- data/Rakefile +29 -0
- data/lib/custom_rqrcode.rb +54 -0
- data/lib/rqrcode-rails3/renderers/svg.rb +46 -0
- data/lib/rqrcode-rails3/size_calculator.rb +40 -0
- metadata +64 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ef2f8329c3650f1476d38103fc63a82dce86e9aa
|
4
|
+
data.tar.gz: c999d5bbef2592952ee487635f54b47ef8144a3d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2582e340f28f2485162986d96a2b0138a24a2305c71a9832fa831583fb4d636d5064509757700ddd3f1ab1352e2d6d790e861c99c4062f565b8ecac8f633fb1e
|
7
|
+
data.tar.gz: ed5aa61ed58d0cbf203b24b1638fd8b7a89b9659c145fc87096c9ac66d7a08078e82c18aefbb169a5d4ff13dfde3e9e7d9eb25db454200082c14088f3b896108
|
data/Gemfile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
|
3
|
+
gem "rails", ">= 3.0.0"
|
4
|
+
gem "capybara", ">= 0.4.0"
|
5
|
+
gem "sqlite3"
|
6
|
+
|
7
|
+
gem "rqrcode"
|
8
|
+
gem "mini_magick"
|
9
|
+
|
10
|
+
# To use debugger (ruby-debug for Ruby 1.8.7+, ruby-debug19 for Ruby 1.9.2+)
|
11
|
+
# gem 'ruby-debug'
|
12
|
+
# gem 'ruby-debug19'
|
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,52 @@
|
|
1
|
+
# Render and customize QR codes from Rails 3 applications
|
2
|
+
|
3
|
+
This gem extends rqrcode-rails published by samvincent, supporting the output in either SVG or PNG, JPEG, and GIF formats and
|
4
|
+
|
5
|
+
providing new options to set background image combined with QR code image.
|
6
|
+
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add the following to your `Gemfile`.
|
11
|
+
|
12
|
+
gem 'custom_rqrcode'
|
13
|
+
|
14
|
+
If you want to use the PNG, JPEG or GIF format, you will have to have **ImageMagick** installed on your system.
|
15
|
+
You will also want to add the **mini_magick** gem to your application's `Gemfile`.
|
16
|
+
|
17
|
+
gem 'mini_magick'
|
18
|
+
|
19
|
+
## How to use
|
20
|
+
|
21
|
+
In your controller actions, you could return a QR code that links to the current page like this:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
respond_to do |format|
|
25
|
+
format.html
|
26
|
+
format.svg { render :qrcode => request.url, :level => :l, :unit => 10 }
|
27
|
+
format.png { render :qrcode => request.url }
|
28
|
+
format.gif { render :qrcode => request.url }
|
29
|
+
format.jpeg { render :qrcode => request.url }
|
30
|
+
end
|
31
|
+
```
|
32
|
+
|
33
|
+
#### Options:
|
34
|
+
|
35
|
+
* `:size` – This controls how big the QR Code will be. Smallest size will be chosen by default. Set to maintain consistent size.
|
36
|
+
* `:level` – The error correction level, can be:
|
37
|
+
* Level `:l` 7% of code can be restored
|
38
|
+
* Level `:m` 15% of code can be restored
|
39
|
+
* Level `:q` 25% of code can be restored
|
40
|
+
* Level `:h` 30% of code can be restored (default :h)
|
41
|
+
* `:offset` – Padding around the QR Code (e.g. 10)
|
42
|
+
* `:unit` – How many pixels per module (e.g. 11)
|
43
|
+
* `:fill` – Background color (e.g "ffffff" or :white)
|
44
|
+
* `:color` – Foreground color for the code (e.g. "000000" or :black)
|
45
|
+
* `:bg` – Background image to be combined with QR code image (e.g. "./img/000.png")
|
46
|
+
* `:gravity` – Location where QR code image will be put on the background image (e.g. "center" or "north")
|
47
|
+
|
48
|
+
## About
|
49
|
+
|
50
|
+
This project was modified from samvincent's project [rqrcode-rails3](https://github.com/samvincent/rqrcode-rails3)
|
51
|
+
|
52
|
+
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 'rake/rdoctask'
|
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 = 'rqrcode-rails3'
|
26
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
27
|
+
rdoc.rdoc_files.include('README.rdoc')
|
28
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
29
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'action_controller'
|
2
|
+
require 'rqrcode'
|
3
|
+
require 'rqrcode-rails3/size_calculator.rb'
|
4
|
+
require 'rqrcode-rails3/renderers/svg.rb'
|
5
|
+
|
6
|
+
module RQRCode
|
7
|
+
Mime::Type.register "image/svg+xml", :svg unless Mime::Type.lookup_by_extension(:svg)
|
8
|
+
Mime::Type.register "image/png", :png unless Mime::Type.lookup_by_extension(:png)
|
9
|
+
Mime::Type.register "image/jpeg", :jpeg unless Mime::Type.lookup_by_extension(:jpeg)
|
10
|
+
Mime::Type.register "image/gif", :gif unless Mime::Type.lookup_by_extension(:gif)
|
11
|
+
|
12
|
+
extend SizeCalculator
|
13
|
+
|
14
|
+
ActionController::Renderers.add :qrcode do |string, options|
|
15
|
+
format = self.request.format.symbol
|
16
|
+
size = options[:size] || RQRCode.minimum_qr_size_from_string(string)
|
17
|
+
level = options[:level] || :h
|
18
|
+
|
19
|
+
bg = options[:bg] || false
|
20
|
+
|
21
|
+
qrcode = RQRCode::QRCode.new(string, :size => size, :level => level)
|
22
|
+
svg = RQRCode::Renderers::SVG::render(qrcode, options)
|
23
|
+
|
24
|
+
data = \
|
25
|
+
if format && format == :svg
|
26
|
+
svg
|
27
|
+
else
|
28
|
+
image = MiniMagick::Image.read(svg) { |i| i.format "svg" }
|
29
|
+
image.format format
|
30
|
+
|
31
|
+
#image = RQRCode::mergeImages(image, bg) if bg
|
32
|
+
if bg
|
33
|
+
print "=============================="
|
34
|
+
bg_image = MiniMagick::Image.open bg
|
35
|
+
gravity = options[:gravity] || "north"
|
36
|
+
image = bg_image.composite(image) do |c|
|
37
|
+
c.gravity gravity
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
image.to_blob
|
42
|
+
end
|
43
|
+
|
44
|
+
self.response_body = render_to_string(:text => data, :template => nil)
|
45
|
+
end
|
46
|
+
|
47
|
+
def mergeImages(image, bg, options)
|
48
|
+
bg_image = MiniMagick::Image.open bg
|
49
|
+
gravity = options[:gravity] || "north"
|
50
|
+
bg_image.composite(image) do |c|
|
51
|
+
c.gravity gravity
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module RQRCode
|
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 RQRCode
|
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
|
+
RQRCode::QRCode.new(string, :size => i)
|
34
|
+
return i
|
35
|
+
rescue RQRCode::QRCodeRunTimeError
|
36
|
+
retry
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: custom_rqrcode
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Johnson Chang
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-06-30 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
|
+
description: Based on Rails, Render QR codes with the ability to customize your own
|
28
|
+
QR codes
|
29
|
+
email: johnsonchang2012@gmail.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- lib/custom_rqrcode.rb
|
35
|
+
- lib/rqrcode-rails3/renderers/svg.rb
|
36
|
+
- lib/rqrcode-rails3/size_calculator.rb
|
37
|
+
- MIT-LICENSE
|
38
|
+
- Rakefile
|
39
|
+
- Gemfile
|
40
|
+
- README.md
|
41
|
+
homepage: http://github.com/johnsonchang2012/custom_rqrcode
|
42
|
+
licenses: []
|
43
|
+
metadata: {}
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options: []
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - '>='
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - '>='
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
requirements: []
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 2.0.2
|
61
|
+
signing_key:
|
62
|
+
specification_version: 4
|
63
|
+
summary: Render QR codes with Rails 3
|
64
|
+
test_files: []
|