visual-qrcode 0.1.2 → 0.1.3

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: f61f0ec6cbd27dd795252d141fb7dd3b6b9cfb98efe35e73d19176505d3e1c0e
4
- data.tar.gz: 5aeb4d2931b1b574d2824dbcfe8d0c03480e30f3395dcc39ec5b262fa61b27ac
3
+ metadata.gz: 0ed1452bc8167ca44fd547c65998487c46f259967c28edc10ea7730f3e968b62
4
+ data.tar.gz: e6f33b6faf3d491657f13db6218260ab68888d15e046286bac339267451f79f8
5
5
  SHA512:
6
- metadata.gz: 5f3588aa015afac87a8ef7375b25c033d8a3a39071941ed9e4ffe6dd35ba3e7039c8c05ec1fca29c5b7fc43348a39ab7d209273aff9c9a92f949f809a6d4d6fe
7
- data.tar.gz: 1af2b54970af438dae3c551cf7e0f5126723dff1873f0ea0ca53e0f298e1b2c6948ad93ec7cb832801ad783bf209a61a503987dd12bd01d6ec2820e3717c8c71
6
+ metadata.gz: 429de2eb4cfa170258b51f814d452df963a32bf54afa6dc2ce8c3ccaf65fc2c8d0da1e490aa9bb2705881cddaf3c511da975610c6f198ae53930793610d6a99a
7
+ data.tar.gz: d9b1f0e0e9c16592a44a99cb4272adb605189c6902d8e42cf5ef9c4aa0e11ec4c11b209e39fd54ab3b2a4287ca2cf415929e36af742975f3979e5b24cf30f41e
data/README.md CHANGED
@@ -1,10 +1,16 @@
1
- # Visual::Qrcode
1
+ # VisualQrcode
2
2
 
3
- `Visual::Qrcode` gives you various tools to generate working QRCodes with images in their backgrounds, based on [this research](https://cgv.cs.nthu.edu.tw/Projects/Recreational_Graphics/Halftone_QRCodes/).
3
+ `VisualQrcode` gives you various tools to generate working QR codes with images in their backgrounds, based on [this research](https://cgv.cs.nthu.edu.tw/Projects/Recreational_Graphics/Halftone_QRCodes/).
4
4
 
5
- Example of the marianne Visual QRCode generated by the tests :
5
+ Example of some `VisualQrcode` generated by the tests :
6
6
 
7
7
  ![image](/spec/images/marianne_visual_qrcode.png)
8
+ ![image](/spec/images/zidane_visual_qrcode.png)
9
+ ![image](/spec/images/leaf_visual_qrcode.png)
10
+
11
+ Basically, each QR Code module is transformed into 9 pixels. The central pixel is the QR Code data, and the 8 other pixels around are used to display the background image.
12
+
13
+ ![image](/docs/basic_to_visual_sample.png)
8
14
 
9
15
  ## Installation
10
16
 
@@ -30,10 +36,12 @@ If bundler is not being used to manage dependencies, install the gem by executin
30
36
  ## Usage
31
37
 
32
38
  The basic usage requires a string for the QRCode content and an `image_path` (or `image_url`).
33
- **Default size** : 3x the basic QRCode size generated with a high level of error correction.
34
39
 
35
40
  ```ruby
36
- visual_qr_code = VisualQrcode::Qrcode.new("bonjour", "spec/images/marianne.png")
41
+ visual_qr_code = VisualQrcode::Qrcode.new(
42
+ "Taataaaa Yoyoyooooo ! Qu'est-ce que tu caches sous ton grand chapeauuuuuu !",
43
+ "spec/images/marianne.png"
44
+ )
37
45
 
38
46
  # Returns a MiniMagick::Image
39
47
  image = visual_qr_code.as_png
@@ -42,26 +50,66 @@ image = visual_qr_code.as_png
42
50
  image.write("./marianne_visual_qrcode.png")
43
51
  ```
44
52
 
45
- You can also add a size parameter, in pixels. This size can't be smaller than the **default size**.
53
+ **Default size** : Because we need 9 pixels in each QR Code module, the **minimum size** is **3x the minimum basic QRCode size** with a high level of error correction. It varies with the amount of content you want to encode in the QR Code.
54
+
55
+ You can also add a size parameter, in pixels. This size can't be smaller than the **minimum size**.
56
+
57
+ ```ruby
58
+ visual_qr_code = VisualQrcode::Qrcode.new(
59
+ "This is a leaf. Yeah. Big surprise, isn't it ?",
60
+ "spec/images/leaf.png",
61
+ size: 280
62
+ )
63
+
64
+ visual_qrcode.as_png.write("./leaf_visual_qrcode_280x280.png")
65
+ ```
66
+
67
+ If you choose a size too small, you'll get an error informing you of the minimum size necessary for your content.
68
+
69
+ If your content is small and produces a QR Code of small size (big patterns, few modules), you can increase the amount of modules with the `qr_size` parameter. It corresponds to the [size option of RQRCodeCore](https://github.com/whomwah/rqrcode_core/tree/master?tab=readme-ov-file#options)
46
70
 
47
71
  ```ruby
48
- visual_qr_code = VisualQrcode::Qrcode.new("bonjour by 280", "spec/images/marianne.png", size: 280)
72
+ visual_qr_code = VisualQrcode::Qrcode.new(
73
+ "eh",
74
+ "spec/images/zidane.png",
75
+ qr_size: 10
76
+ )
49
77
 
50
- visual_qrcode.as_png.write("./marianne_visual_qrcode_280x280.png")
78
+ visual_qrcode.as_png.write("./zidane_visual_qrcode_size_10.png")
51
79
  ```
52
80
 
53
81
  ## Design choices
54
82
 
55
83
  ### Padding
56
84
 
57
- In order to have a nice visual, a padding is added on the image to keep it inside of the QRCode guide patterns. Also it helps to reckognize that the image _is_ a scannable QRCode and not just some random image.
85
+ In order to have a nice visual, a padding is added on the image to keep it inside of the QRCode line patterns on top and on the left. Also it helps to reckognize that the image _is_ a scannable QRCode and not just some random image.
86
+
87
+ **By default, the padding is equal to 7 modules.**
88
+
89
+ If your image has enough transparency to dodge the QRCode lines, you can remove the padding with the `padding_modules: 0` option.
90
+
91
+ ```ruby
92
+ visual_qr_code = VisualQrcode::Qrcode.new(
93
+ "My leaf don't need no padding, it's a strong and independant leaf",
94
+ "spec/images/leaf.png",
95
+ size: 280,
96
+ padding_modules: 0
97
+ )
98
+ ```
99
+
100
+ You can also customize the padding if you want more or less modules than the default value.
58
101
 
59
102
  ### Resize method
60
103
 
61
- The Visual QRCode will be generated at a mutiple of the **default size**, and then reduced to the expected size to maintain a good background image quality.
104
+ The Visual QRCode will be generated at a mutiple of the **minimum size**, and then reduced to the expected size to maintain a good background image quality.
105
+
106
+ > For example, if the minimum size is 140px, and you want a 230px image, it will generate a 280px Visual QRCode and then reduce it to 230px.
107
+
108
+ ### QRCode minimum Size
62
109
 
63
- > For example, if the Default size is 140px, and you want a 230px image, it will generate a 280px Visual QRCode and then reduce it to 230px.
110
+ The minimum [size of RQRCodeCore](https://github.com/whomwah/rqrcode_core/tree/master?tab=readme-ov-file#options) used is 6 by default, to get enough space for the image to be visible inside the Visual QRCode.
64
111
 
112
+ But you can force it to a lower value if you want, with the `qr_size` option.
65
113
 
66
114
  ## Development
67
115
 
Binary file
@@ -16,7 +16,7 @@ module ModuleFiller
16
16
 
17
17
  def fill_vqr_module_with_basic_qrcode(x_index, y_index)
18
18
  multiplied_range_each(x_index, y_index) do |new_x, new_y, x_offset, y_offset|
19
- if central_pixel?(x_offset, y_offset) || @image_handler.pixels[new_x][new_y][3].zero?
19
+ if central_pixel?(x_offset, y_offset) || @pixels_handler.pixels[new_x][new_y][3].zero?
20
20
  value = @basic_qrcode.modules[x_index][y_index]
21
21
  @vqr_pixels[new_x][new_y] = pixel_of(value)
22
22
  end
@@ -27,7 +27,7 @@ module ModuleFiller
27
27
  multiplied_range_each(x_index, y_index) do |new_x, new_y, x_offset, y_offset|
28
28
  next if central_pixel?(x_offset, y_offset)
29
29
 
30
- pixel = @image_handler.pixels[new_x][new_y]
30
+ pixel = @pixels_handler.pixels[new_x][new_y]
31
31
  @vqr_pixels[new_x][new_y] = pixel
32
32
  end
33
33
  end
@@ -14,6 +14,8 @@ module VisualQrcode
14
14
  @pixels = pixels
15
15
  else
16
16
  @image = MiniMagick::Image.open(image_path)
17
+ raise "Image should be a square" if @image.width != @image.height
18
+
17
19
  set_pixels_from_image
18
20
  end
19
21
  end
@@ -11,16 +11,18 @@ module VisualQrcode
11
11
  include PixelTools
12
12
  include ModuleFiller
13
13
 
14
- PADDING_MODULES = 7
14
+ DEFAULT_PADDING_MODULES = 7
15
15
 
16
- attr_reader :content, :basic_qrcode, :image_handler, :vqr_pixels
16
+ attr_reader :content, :basic_qrcode, :pixels_handler, :vqr_pixels
17
17
 
18
- def initialize(content, image_path, size: nil)
18
+ def initialize(content, image_path, size: nil, padding_modules: nil, qr_size: nil)
19
19
  @content = content
20
20
  @size = size
21
+ @padding_modules = padding_modules || DEFAULT_PADDING_MODULES
22
+ @qr_size = qr_size || 6
21
23
 
22
- @basic_qrcode = RQRCodeCore::QRCode.new(content, level: :h)
23
- @image_handler = VisualQrcode::PixelsHandler.new(image_path: image_path)
24
+ initialize_basic_qr_code_and_qr_size(content)
25
+ @pixels_handler = VisualQrcode::PixelsHandler.new(image_path: image_path)
24
26
  @common_patterns = @basic_qrcode.instance_variable_get(:@common_patterns)
25
27
  end
26
28
 
@@ -29,16 +31,6 @@ module VisualQrcode
29
31
  VisualQrcode::Export.new(@vqr_pixels).as_png(size: @size, margin: margin)
30
32
  end
31
33
 
32
- def basic_qrcode_as_png(margin: default_margin)
33
- make
34
- basic_qrcode_pixels = @basic_qrcode.modules.map do |module_row|
35
- pixels_row = module_row.map { |value| [pixel_of(value)] * PIXELS_PER_MODULE }.flatten
36
- ([pixels_row] * PIXELS_PER_MODULE)
37
- end.flatten(1)
38
-
39
- VisualQrcode::Export.new(basic_qrcode_pixels).as_png(size: @size, margin: margin)
40
- end
41
-
42
34
  def make
43
35
  intit_vqr_pixels
44
36
  resize_image
@@ -51,8 +43,8 @@ module VisualQrcode
51
43
  end
52
44
 
53
45
  def resize_image
54
- padding_size = PADDING_MODULES * module_size
55
- @image_handler.resize_with_padding(@vqr_length, padding_size)
46
+ padding_size = @padding_modules * module_size
47
+ @pixels_handler.resize_with_padding(@vqr_length, padding_size)
56
48
  end
57
49
 
58
50
  def fill_vqr_pixels
@@ -65,6 +57,15 @@ module VisualQrcode
65
57
 
66
58
  private
67
59
 
60
+ def initialize_basic_qr_code_and_qr_size(content)
61
+ @basic_qrcode = RQRCodeCore::QRCode.new(content, level: :h, size: @qr_size)
62
+ rescue RQRCodeCore::QRCodeRunTimeError => e
63
+ raise e unless e.message =~ /^code length overflow./
64
+
65
+ @qr_size += 1
66
+ initialize_basic_qr_code_and_qr_size(content)
67
+ end
68
+
68
69
  def default_margin
69
70
  module_size * 2
70
71
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module VisualQrcode
4
- VERSION = "0.1.2"
4
+ VERSION = "0.1.3"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: visual-qrcode
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Caillou
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-07-12 00:00:00.000000000 Z
11
+ date: 2024-07-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mini_magick
@@ -53,6 +53,7 @@ files:
53
53
  - LICENSE.txt
54
54
  - README.md
55
55
  - Rakefile
56
+ - docs/basic_to_visual_sample.png
56
57
  - lib/visual_qrcode.rb
57
58
  - lib/visual_qrcode/export.rb
58
59
  - lib/visual_qrcode/module_filler.rb