Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adds unit size and level options

  • Loading branch information...
commit 8d7cdc8be41b12495ebbfb1ff3912f48f64d4f54 1 parent d07d586
Sam Vincent authored
8 README.rdoc → README.md
View
@@ -21,14 +21,20 @@ In your controller actions, you could return a QR code that links to the current
respond_to do |format|
format.html
- format.svg { render :qrcode => request.url }
+ format.svg { render :qrcode => request.url, :level => :l, :unit => 10 }
format.png { render :qrcode => request.url }
end
Options:
* +:size+ – This controls how big the QR Code will be. Smallest size will be chosen by default. Set to maintain consistent size.
+* +:level* – The error correction level, can be:
+ * Level :l 7% of code can be restored
+ * Level :m 15% of code can be restored
+ * Level :q 25% of code can be restored
+ * Level :h 30% of code can be restored (default :h)
* +:offset+ – Padding around the QR Code (e.g. 10)
+* +:unit+ – How many pixels per module (e.g. 11)
* +:fill+ – Background color (e.g "ffffff" or :white)
* +:color+ – Foreground color for the code (e.g. "000000" or :black)
5 lib/rqrcode-rails3.rb
View
@@ -11,9 +11,10 @@ module RQRCode
ActionController::Renderers.add :qrcode do |string, options|
format = self.request.format.symbol
- size = options[:size] || RQRCode.minimum_qr_size_from_string(string)
+ size = options[:size] || RQRCode.minimum_qr_size_from_string(string)
+ level = options[:level] || :h
- qrcode = RQRCode::QRCode.new(string, :size => size)
+ qrcode = RQRCode::QRCode.new(string, :size => size, :level => level)
svg = RQRCode::Renderers::SVG::render(qrcode, options)
data = \
10 lib/rqrcode-rails3/renderers/svg.rb
View
@@ -5,15 +5,17 @@ class << self
# Render the SVG from the qrcode string provided from the RQRCode gem
# Options:
# offset - Padding around the QR Code (e.g. 10)
+ # unit - How many pixels per module (Default: 11)
# fill - Background color (e.g "ffffff" or :white)
# color - Foreground color for the code (e.g. "000000" or :black)
def render(qrcode, options={})
offset = options[:offset].to_i || 0
color = options[:color] || "000"
+ unit = options[:unit] || 11
# height and width dependent on offset and QR complexity
- dimension = (qrcode.module_count*11) + (2*offset)
+ dimension = (qrcode.module_count*unit) + (2*offset)
xml_tag = %{<?xml version="1.0" standalone="yes"?>}
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}">}
@@ -23,11 +25,11 @@ def render(qrcode, options={})
qrcode.modules.each_index do |c|
tmp = []
qrcode.modules.each_index do |r|
- y = c*11 + offset
- x = r*11 + offset
+ y = c*unit + offset
+ x = r*unit + offset
next unless qrcode.is_dark(c, r)
- tmp << %{<rect width="11" height="11" x="#{x}" y="#{y}" style="fill:##{color}"/>}
+ tmp << %{<rect width="#{unit}" height="#{unit}" x="#{x}" y="#{y}" style="fill:##{color}"/>}
end
result << tmp.join
end
2  test/integration/navigation_test.rb
View
@@ -16,7 +16,7 @@ class NavigationTest < ActiveSupport::IntegrationCase
visit home_path
click_link 'PNG'
assert_equal 'image/png; charset=utf-8', headers['Content-Type']
- assert_equal File.read('test/support/data/qrcode.png')[0,4], page.body[0,4]
+ assert_equal File.read('test/support/data/qrcode.png')[0,4], page.body.force_encoding("UTF-8")[0,4]
end
protected
Please sign in to comment.
Something went wrong with that request. Please try again.