pngqr 0.1 → 0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +2 -0
- data/README +10 -4
- data/lib/pngqr.rb +42 -8
- data/pngqr.gemspec +1 -1
- metadata +2 -2
data/CHANGELOG
CHANGED
data/README
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
PNGQR (Ping-queer?)
|
3
3
|
-------------------
|
4
4
|
|
5
|
-
Ruby Gem to generate PNG files
|
5
|
+
Pure Ruby Gem to generate PNG files of QR codes.
|
6
6
|
|
7
7
|
|
8
8
|
|
@@ -12,13 +12,19 @@ Usage
|
|
12
12
|
Example:
|
13
13
|
|
14
14
|
require 'pngqr'
|
15
|
-
File.open('out.png','wb') {|io| io << Pngqr.encode('https://github.com/andys') }
|
16
15
|
|
17
|
-
|
16
|
+
Pngqr.encode 'https://github.com/andys', :file => 'output.png'
|
17
|
+
|
18
|
+
The parameters for encode() are the same as the rqrcode gem's new(), except:
|
19
|
+
1. we add :file if you want to output to a file instead of returning the PNG as
|
20
|
+
a blob, and
|
21
|
+
2. you can :scale with an integer parameter and it will scale up the output
|
22
|
+
image by that many times.
|
18
23
|
See: http://whomwah.github.com/rqrcode/
|
19
24
|
|
20
25
|
|
21
|
-
|
26
|
+
|
27
|
+
Dependencies
|
22
28
|
------------
|
23
29
|
|
24
30
|
Gems: rqrcode and chunky_png (both pure ruby gems)
|
data/lib/pngqr.rb
CHANGED
@@ -2,14 +2,48 @@ require 'rubygems'
|
|
2
2
|
require 'chunky_png'
|
3
3
|
require 'rqrcode'
|
4
4
|
|
5
|
-
|
6
5
|
class Pngqr
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
6
|
+
class << self
|
7
|
+
|
8
|
+
def encode(*opts)
|
9
|
+
@filename = nil
|
10
|
+
@scale = 1
|
11
|
+
if Hash===opts.last
|
12
|
+
@filename = opts.last.delete(:file)
|
13
|
+
@scale = opts.last.delete(:scale)
|
14
|
+
end
|
15
|
+
|
16
|
+
qr = RQRCode::QRCode.new(*opts)
|
17
|
+
len = qr.module_count
|
18
|
+
png = ChunkyPNG::Image.new(len*@scale, len*@scale, ChunkyPNG::Color::WHITE)
|
19
|
+
|
20
|
+
for_each_pixel(len) do |x,y|
|
21
|
+
if qr.modules[y][x]
|
22
|
+
for_each_scale(x, y, @scale) do |scaled_x, scaled_y|
|
23
|
+
png[scaled_x, scaled_y] = ChunkyPNG::Color::BLACK
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
if @filename
|
29
|
+
File.open(@filename, 'wb') {|io| io << png.to_blob(:fast_rgb) }
|
30
|
+
else
|
31
|
+
png.to_blob(:fast_rgb)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
protected
|
36
|
+
|
37
|
+
def for_each_scale(x, y, scale)
|
38
|
+
for_each_pixel(scale) do |x_off, y_off|
|
39
|
+
yield(x * scale + x_off, y * scale + y_off)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def for_each_pixel(len, &bl)
|
44
|
+
ary = (0...len).to_a
|
45
|
+
ary.product(ary).each(&bl)
|
46
|
+
end
|
47
|
+
|
13
48
|
end
|
14
49
|
end
|
15
|
-
|
data/pngqr.gemspec
CHANGED