qrscan 0.1.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 +7 -0
- data/.rubocop.yml +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +19 -0
- data/Rakefile +8 -0
- data/ext/qrscan/extconf.rb +12 -0
- data/ext/qrscan/qr_scanner.o +0 -0
- data/ext/qrscan/qrscan.c +94 -0
- data/ext/qrscan/stb_image.h +7988 -0
- data/lib/qrscan/version.rb +5 -0
- data/lib/qrscan.rb +4 -0
- metadata +54 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 9ecaba6d7978240dfaff4ce078906437b8d212ea39dcb3271d3fa08e44851b55
|
4
|
+
data.tar.gz: 60ce766b66cf9c5a1e414a0fd5297c3ba004d8463102b7aa42faece93976ddcc
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8c06e3ec464428ac7b6d0910e14d80ca0b6648731449b6d3360e66f5da167ecfc60b6e6f57e98d5c1868f3f7a0dcd3db0e21e5d513dcb638902ac93a5dbb60f3
|
7
|
+
data.tar.gz: a807d6e2e3cd31c0de78aa7caf2e46ec755f80c08b9664e97be39ee66e187adc3fdb8eedf638b0049d94358289b92c7bb8722eb386c4e54cca4bf5c061ec6c32
|
data/.rubocop.yml
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2025 pioz
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# Qrscan
|
2
|
+
|
3
|
+
A Ruby gem for decoding QR code from images.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
```
|
8
|
+
gem install qrscan
|
9
|
+
```
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
decoded_value = Qrscan.scan("/image/path/file.jpg")
|
15
|
+
```
|
16
|
+
|
17
|
+
## License
|
18
|
+
|
19
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
# Creating a gem that includes an extension that is built at install time
|
2
|
+
# https://guides.rubygems.org/gems-with-extensions/
|
3
|
+
|
4
|
+
require "mkmf"
|
5
|
+
|
6
|
+
dir_config("qrscan", "/opt/homebrew/include", "/opt/homebrew/lib")
|
7
|
+
|
8
|
+
# Verifica la presenza dell'header e della libreria (opzionale)
|
9
|
+
have_header("zbar.h") or abort "zbar.h not found"
|
10
|
+
have_library("zbar") or abort "libzbar not found"
|
11
|
+
|
12
|
+
create_makefile("qrscan")
|
Binary file
|
data/ext/qrscan/qrscan.c
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
#include "ruby.h"
|
2
|
+
|
3
|
+
#include <stdio.h>
|
4
|
+
#include <stdlib.h>
|
5
|
+
#include <string.h>
|
6
|
+
#include <zbar.h>
|
7
|
+
|
8
|
+
#define STB_IMAGE_IMPLEMENTATION
|
9
|
+
#include "stb_image.h"
|
10
|
+
|
11
|
+
// Cleanup handler for the image data
|
12
|
+
void free_image(zbar_image_t *img) {
|
13
|
+
const void *data = zbar_image_get_data(img);
|
14
|
+
if (data) free((void *)data);
|
15
|
+
}
|
16
|
+
|
17
|
+
unsigned char* load_image(const char* image_path, int *width, int *height) {
|
18
|
+
int channels;
|
19
|
+
// Load image using stb_image
|
20
|
+
unsigned char *img = stbi_load(image_path, width, height, &channels, 0);
|
21
|
+
if (!img) {
|
22
|
+
return NULL;
|
23
|
+
}
|
24
|
+
int pixels = (*width) * (*height);
|
25
|
+
// Convert image to grayscale
|
26
|
+
unsigned char *gray = malloc(pixels);
|
27
|
+
if (!gray) {
|
28
|
+
stbi_image_free(img);
|
29
|
+
return NULL;
|
30
|
+
}
|
31
|
+
|
32
|
+
if (channels == 1) {
|
33
|
+
memcpy(gray, img, pixels);
|
34
|
+
} else {
|
35
|
+
for (int i = 0; i < pixels; i++) {
|
36
|
+
int r = img[i * channels];
|
37
|
+
int g = img[i * channels + 1];
|
38
|
+
int b = img[i * channels + 2];
|
39
|
+
// Compute luminance using standard coefficients
|
40
|
+
gray[i] = (unsigned char)(0.299 * r + 0.587 * g + 0.114 * b);
|
41
|
+
}
|
42
|
+
}
|
43
|
+
|
44
|
+
stbi_image_free(img);
|
45
|
+
return gray;
|
46
|
+
}
|
47
|
+
|
48
|
+
const char* scan_image(unsigned char* image, int width, int height) {
|
49
|
+
const char *decoded_string = "";
|
50
|
+
// Initialize zbar image scanner
|
51
|
+
zbar_image_scanner_t *scanner = zbar_image_scanner_create();
|
52
|
+
// Enable all barcode types
|
53
|
+
zbar_image_scanner_set_config(scanner, 0, ZBAR_CFG_ENABLE, 1);
|
54
|
+
// Wrap the grayscale image data in a zbar image
|
55
|
+
zbar_image_t *zbar_img = zbar_image_create();
|
56
|
+
// Set image format to "Y800" (8-bit grayscale)
|
57
|
+
zbar_image_set_format(zbar_img, *(int*)"Y800");
|
58
|
+
zbar_image_set_size(zbar_img, width, height);
|
59
|
+
// Pass our cleanup handler instead of free
|
60
|
+
zbar_image_set_data(zbar_img, image, width * height, free_image);
|
61
|
+
// Scan the image for barcodes/QR codes
|
62
|
+
zbar_scan_image(scanner, zbar_img);
|
63
|
+
// Extract the results
|
64
|
+
const zbar_symbol_t *symbol = zbar_image_first_symbol(zbar_img);
|
65
|
+
if (symbol) {
|
66
|
+
decoded_string = strdup(zbar_symbol_get_data(symbol));
|
67
|
+
}
|
68
|
+
|
69
|
+
// Cleanup
|
70
|
+
zbar_image_destroy(zbar_img);
|
71
|
+
zbar_image_scanner_destroy(scanner);
|
72
|
+
|
73
|
+
return decoded_string;
|
74
|
+
}
|
75
|
+
|
76
|
+
VALUE qrscan_error;
|
77
|
+
|
78
|
+
VALUE scan(VALUE self, VALUE image_path) {
|
79
|
+
const char *image_path_ptr = StringValuePtr(image_path);
|
80
|
+
int width, height;
|
81
|
+
unsigned char *img = load_image(image_path_ptr, &width, &height);
|
82
|
+
if (!img) {
|
83
|
+
rb_raise(qrscan_error, "Error loading image");
|
84
|
+
}
|
85
|
+
const char* s = scan_image(img, width, height);
|
86
|
+
return rb_str_new2(s);
|
87
|
+
}
|
88
|
+
|
89
|
+
void Init_qrscan() {
|
90
|
+
VALUE qrscan = rb_define_module("Qrscan");
|
91
|
+
rb_define_singleton_method(qrscan, "scan", scan, 1);
|
92
|
+
|
93
|
+
qrscan_error = rb_define_class_under(qrscan, "QrscanError", rb_eStandardError);
|
94
|
+
}
|