qr_code_maker 0.1.0 → 0.2.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 883092da5fc7735a8babef3fe6bb57b630147a3ed7c4ce7c21e3e095b829c9e8
4
- data.tar.gz: 26869583a0e07a791bd552a2675000e5b33cf00c4217e08e82cca4208f7bc196
3
+ metadata.gz: 8d3c889a8eb8f1e0d62eda0bb8aecd40ae4940af5ba4390b456d21117c7cfc5f
4
+ data.tar.gz: 3bf1ef9d4247b28a672257694a20efce1b6f5b3579d6e8abe1e9d531b320d223
5
5
  SHA512:
6
- metadata.gz: 44d9ea7bd6708381e7445a180956273309269b50d5b9ff54ce8c01736c8c48de030fb4ff6a3a154ad195f3c27d73ca8a488d0d6ccf58c861fe5e2a316b90d9da
7
- data.tar.gz: 2b9e34abdbae056cf89512be3ebbb9216832db62af0a9d2f9b063bc019e63e864607cf02ea287c10ec968c08052cec14ce97b6eca3be1757d91dd08b80a53e53
6
+ metadata.gz: f64a4c0240bb757ae33ba1ea0b64604d7d38878be0ec04df7aeee9ed456c157f3d247737aa1daa7a500e7c9909b3ff49edcb8b6e4835acf0aa495521d0bb9c59
7
+ data.tar.gz: 93e7e92a5758d0c5a8a6b59e67209bd50a1ae347122a50ce3a22b9436b38bcf32d9c2408b5d47e0e62a0b060dc7e912f5d136779ce8a32ee95dc8a261f16fe51
@@ -52,5 +52,54 @@ module QrCodeMaker
52
52
  margin.times { rows << (light * (@module_count + 2 * margin)) }
53
53
  rows.join("\n")
54
54
  end
55
+
56
+ def as_png(options = {})
57
+ require "zlib" unless defined?(Zlib)
58
+
59
+ module_size = options[:module_size] || 10
60
+ margin = options[:margin] || 4
61
+ dark_color = parse_rgb_color(options[:color], [0, 0, 0])
62
+ bg_color = parse_rgb_color(options[:bg_color], [255, 255, 255])
63
+
64
+ dim = (@module_count + 2 * margin) * module_size
65
+
66
+ raw_data = String.new(encoding: "BINARY")
67
+ dim.times do |y|
68
+ raw_data << "\x00".b
69
+ r = (y / module_size) - margin
70
+ dim.times do |x|
71
+ c = (x / module_size) - margin
72
+ is_dark = (r >= 0 && r < @module_count && c >= 0 && c < @module_count) ? @modules[r][c] : false
73
+ color = is_dark ? dark_color : bg_color
74
+ raw_data << color.pack("C3")
75
+ end
76
+ end
77
+
78
+ png = String.new("\x89PNG\r\n\x1a\n".b, encoding: "BINARY")
79
+ ihdr = [dim, dim, 8, 2, 0, 0, 0].pack("N2C5")
80
+ png << png_chunk("IHDR", ihdr)
81
+ png << png_chunk("IDAT", Zlib::Deflate.deflate(raw_data, Zlib::BEST_COMPRESSION))
82
+ png << png_chunk("IEND", "")
83
+ png
84
+ end
85
+
86
+ private
87
+
88
+ def png_chunk(type, data)
89
+ [data.bytesize].pack("N") + type.b + data.b + [Zlib.crc32(type.b + data.b)].pack("N")
90
+ end
91
+
92
+ def parse_rgb_color(val, default)
93
+ return default if val.nil?
94
+ str = val.to_s.tr("#", "")
95
+ return default unless str =~ /\A[0-9a-fA-F]{3,6}\z/
96
+ if str.length == 6
97
+ [str[0..1].hex, str[2..3].hex, str[4..5].hex]
98
+ elsif str.length == 3
99
+ [str[0].hex * 17, str[1].hex * 17, str[2].hex * 17]
100
+ else
101
+ default
102
+ end
103
+ end
55
104
  end
56
105
  end
data/lib/qr_code_maker.rb CHANGED
@@ -13,4 +13,9 @@ module QrCodeMaker
13
13
  def self.generate_ascii(data, options = {})
14
14
  QRCode.new(data, options).as_ascii(options)
15
15
  end
16
+
17
+ # High-level helper to generate PNG in a single call
18
+ def self.generate_png(data, options = {})
19
+ QRCode.new(data, options).as_png(options)
20
+ end
16
21
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: qr_code_maker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - swlkr
@@ -39,7 +39,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  requirements: []
42
- rubygems_version: 3.7.2
42
+ rubygems_version: 3.6.9
43
43
  specification_version: 4
44
44
  summary: A zero-dependency standard-library-only QR code maker.
45
45
  test_files: []