visual-qrcode 0.1.3 → 1.O.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0ed1452bc8167ca44fd547c65998487c46f259967c28edc10ea7730f3e968b62
4
- data.tar.gz: e6f33b6faf3d491657f13db6218260ab68888d15e046286bac339267451f79f8
3
+ metadata.gz: e1d5268c5f404484b75afcee3fa4dc74fe7d6f709a9cfc2405608ac37ce6ca5e
4
+ data.tar.gz: 5a17c764d5369a18de46ac7fa8b22e7b873eca29912a20dbcc76b27352883d71
5
5
  SHA512:
6
- metadata.gz: 429de2eb4cfa170258b51f814d452df963a32bf54afa6dc2ce8c3ccaf65fc2c8d0da1e490aa9bb2705881cddaf3c511da975610c6f198ae53930793610d6a99a
7
- data.tar.gz: d9b1f0e0e9c16592a44a99cb4272adb605189c6902d8e42cf5ef9c4aa0e11ec4c11b209e39fd54ab3b2a4287ca2cf415929e36af742975f3979e5b24cf30f41e
6
+ metadata.gz: 839b3876084aa3528b23ca0bd31aded4783ac835c1b4bd76adae190a8c34f987a6aa0746a97f9f233d8a9cdeed141c27880f128d583dd8f98d7f1807175cfa18
7
+ data.tar.gz: 0ccc5234545ed305ca8a23587da3d7a49200d253c526039dbabccd585bf89e5413fa348966bc09cd112077c2f73f4a21461809ab4481aea71ec480056f9c5a47
data/README.md CHANGED
@@ -4,9 +4,9 @@
4
4
 
5
5
  Example of some `VisualQrcode` generated by the tests :
6
6
 
7
+ ![image](/spec/images/ruby_visual_qrcode.png)
7
8
  ![image](/spec/images/marianne_visual_qrcode.png)
8
9
  ![image](/spec/images/zidane_visual_qrcode.png)
9
- ![image](/spec/images/leaf_visual_qrcode.png)
10
10
 
11
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
12
 
@@ -39,8 +39,9 @@ The basic usage requires a string for the QRCode content and an `image_path` (or
39
39
 
40
40
  ```ruby
41
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"
42
+ "This is the sentence that will read in the QR Code",
43
+ "spec/images/marianne.png",
44
+ size: 260
44
45
  )
45
46
 
46
47
  # Returns a MiniMagick::Image
@@ -50,39 +51,24 @@ image = visual_qr_code.as_png
50
51
  image.write("./marianne_visual_qrcode.png")
51
52
  ```
52
53
 
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
+ > **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](https://github.com/whomwah/rqrcode_core/tree/master?tab=readme-ov-file#options)). It varies with the amount of content you want to encode in the QR Code.
55
+ >
56
+ > If you choose a size too small, you'll get an error informing you of the minimum size necessary for your content.
54
57
 
55
- You can also add a size parameter, in pixels. This size can't be smaller than the **minimum size**.
56
58
 
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.
59
+ ## Design choices
68
60
 
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)
61
+ ### Resize method
70
62
 
71
- ```ruby
72
- visual_qr_code = VisualQrcode::Qrcode.new(
73
- "eh",
74
- "spec/images/zidane.png",
75
- qr_size: 10
76
- )
63
+ 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.
77
64
 
78
- visual_qrcode.as_png.write("./zidane_visual_qrcode_size_10.png")
79
- ```
65
+ > 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.
80
66
 
81
- ## Design choices
67
+ Because of that, it is recommended to provide an image bigger than the expected size, to not be made blurry by the resize. You can't go wrong with an image twice as big as the size of the wanted result.
82
68
 
83
69
  ### Padding
84
70
 
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.
71
+ 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 by keeping some of the well-known patterns of the QR Code in the padding.
86
72
 
87
73
  **By default, the padding is equal to 7 modules.**
88
74
 
@@ -90,26 +76,31 @@ If your image has enough transparency to dodge the QRCode lines, you can remove
90
76
 
91
77
  ```ruby
92
78
  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,
79
+ "https://en.wikipedia.org/wiki/Zinedine_Zidane",
80
+ "spec/images/zidane.png",
81
+ size: 260,
96
82
  padding_modules: 0
97
83
  )
98
84
  ```
99
85
 
100
86
  You can also customize the padding if you want more or less modules than the default value.
101
87
 
102
- ### Resize method
103
-
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
88
  ### QRCode minimum Size
109
89
 
110
90
  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.
111
91
 
112
- But you can force it to a lower value if you want, with the `qr_size` option.
92
+ You can increase the amount of modules with the `minimum_qr_size` parameter. It corresponds to the [size option of RQRCodeCore](https://github.com/whomwah/rqrcode_core/tree/master?tab=readme-ov-file#options)
93
+
94
+ ```ruby
95
+ visual_qr_code = VisualQrcode::Qrcode.new(
96
+ "small content",
97
+ "spec/images/zidane.png",
98
+ size: 260,
99
+ minimum_qr_size: 15
100
+ )
101
+ ```
102
+
103
+ You can also force it to a lower value if you want. But it will always be incremented until finding a suitable value for your content size.
113
104
 
114
105
  ## Development
115
106
 
@@ -14,8 +14,6 @@ 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
-
19
17
  set_pixels_from_image
20
18
  end
21
19
  end
@@ -37,6 +35,7 @@ module VisualQrcode
37
35
  def resize(new_size)
38
36
  image.resize "#{new_size}x#{new_size}"
39
37
  set_pixels_from_image
38
+ make_square
40
39
  end
41
40
 
42
41
  def add_margin(margin, color: :transparent)
@@ -51,8 +50,42 @@ module VisualQrcode
51
50
  @pixels = col_margin + margined_rows + col_margin
52
51
  end
53
52
 
53
+ def make_square
54
+ max_size = [@pixels.length, @pixels.first.length].max
55
+
56
+ if @pixels.length < max_size
57
+ add_transparent_columns_to_size(max_size)
58
+ elsif @pixels.first.length < max_size
59
+ add_transparent_rows_to_size(max_size)
60
+ end
61
+ end
62
+
54
63
  private
55
64
 
65
+ def add_transparent_columns_to_size(max_size)
66
+ size_difference = max_size - @pixels.length
67
+ margin = size_difference / 2
68
+ rest_of_margin = size_difference % 2
69
+
70
+ left_col_margin = [Array.new(max_size, transparent_pixel)] * margin
71
+ right_col_margin = [Array.new(max_size, transparent_pixel)] * (margin + rest_of_margin)
72
+
73
+ @pixels = left_col_margin + @pixels + right_col_margin
74
+ end
75
+
76
+ def add_transparent_rows_to_size(max_size)
77
+ size_difference = max_size - @pixels.first.length
78
+ margin = size_difference / 2
79
+ rest_of_margin = size_difference % 2
80
+
81
+ left_row_margin = [transparent_pixel] * margin
82
+ right_row_margin = [transparent_pixel] * (margin + rest_of_margin)
83
+
84
+ @pixels = @pixels.map do |row|
85
+ left_row_margin + row + right_row_margin
86
+ end
87
+ end
88
+
56
89
  def set_pixels_from_image
57
90
  @pixels = @image.get_pixels("RGBA")
58
91
  end
@@ -15,13 +15,13 @@ module VisualQrcode
15
15
 
16
16
  attr_reader :content, :basic_qrcode, :pixels_handler, :vqr_pixels
17
17
 
18
- def initialize(content, image_path, size: nil, padding_modules: nil, qr_size: nil)
18
+ def initialize(content, image_path, size: nil, padding_modules: nil, minimum_qr_size: nil)
19
19
  @content = content
20
20
  @size = size
21
21
  @padding_modules = padding_modules || DEFAULT_PADDING_MODULES
22
- @qr_size = qr_size || 6
22
+ @minimum_qr_size = minimum_qr_size || 6
23
23
 
24
- initialize_basic_qr_code_and_qr_size(content)
24
+ initialize_basic_qr_code_and_minimum_qr_size(content)
25
25
  @pixels_handler = VisualQrcode::PixelsHandler.new(image_path: image_path)
26
26
  @common_patterns = @basic_qrcode.instance_variable_get(:@common_patterns)
27
27
  end
@@ -57,13 +57,13 @@ module VisualQrcode
57
57
 
58
58
  private
59
59
 
60
- def initialize_basic_qr_code_and_qr_size(content)
61
- @basic_qrcode = RQRCodeCore::QRCode.new(content, level: :h, size: @qr_size)
60
+ def initialize_basic_qr_code_and_minimum_qr_size(content)
61
+ @basic_qrcode = RQRCodeCore::QRCode.new(content, level: :h, size: @minimum_qr_size)
62
62
  rescue RQRCodeCore::QRCodeRunTimeError => e
63
63
  raise e unless e.message =~ /^code length overflow./
64
64
 
65
- @qr_size += 1
66
- initialize_basic_qr_code_and_qr_size(content)
65
+ @minimum_qr_size += 1
66
+ initialize_basic_qr_code_and_minimum_qr_size(content)
67
67
  end
68
68
 
69
69
  def default_margin
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module VisualQrcode
4
- VERSION = "0.1.3"
4
+ VERSION = "1.O.0"
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.3
4
+ version: 1.O.0
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-15 00:00:00.000000000 Z
11
+ date: 2024-07-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mini_magick