pngqr 0.1 → 0.2

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.
Files changed (5) hide show
  1. data/CHANGELOG +2 -0
  2. data/README +10 -4
  3. data/lib/pngqr.rb +42 -8
  4. data/pngqr.gemspec +1 -1
  5. metadata +2 -2
data/CHANGELOG CHANGED
@@ -1 +1,3 @@
1
+ v0.2. Add output file and scaling support
2
+
1
3
  v0.1. Initial commit
data/README CHANGED
@@ -2,7 +2,7 @@
2
2
  PNGQR (Ping-queer?)
3
3
  -------------------
4
4
 
5
- Ruby Gem to generate PNG files with QR codes in them.
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
- The parameters for encode() are the same as the rqrcode gem's new().
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
- Requirements
26
+
27
+ Dependencies
22
28
  ------------
23
29
 
24
30
  Gems: rqrcode and chunky_png (both pure ruby gems)
@@ -2,14 +2,48 @@ require 'rubygems'
2
2
  require 'chunky_png'
3
3
  require 'rqrcode'
4
4
 
5
-
6
5
  class Pngqr
7
- def self.encode(*opts)
8
- qr = RQRCode::QRCode.new(*opts)
9
- len = qr.module_count
10
- png = ChunkyPNG::Image.new(len, len, ChunkyPNG::Color::WHITE)
11
- (0...len).each {|row| (0...len).each {|col| png[col, row] = ChunkyPNG::Color::BLACK if qr.modules[row][col] } }
12
- png.to_blob(:fast_rgb)
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
-
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{pngqr}
5
- s.version = "0.1"
5
+ s.version = "0.2"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Andrew Snow"]
metadata CHANGED
@@ -4,8 +4,8 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
8
- version: "0.1"
7
+ - 2
8
+ version: "0.2"
9
9
  platform: ruby
10
10
  authors:
11
11
  - Andrew Snow