-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
110 lines (100 loc) · 3.63 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
mod utils;
use types::ScanResult;
use wasm_bindgen::{prelude::*, Clamped};
use web_sys::ImageData;
// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
// allocator.
#[cfg(feature = "wee_alloc")]
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
mod types;
#[wasm_bindgen]
pub fn read_qrcodes_from_image_data(
img: ImageData,
process_image_to_find_more: bool,
) -> ScanResult {
let mut decoder = quircs::Quirc::default(); // allocates stuff, so better move into class to be reused
let image_buffer = image::RgbaImage::from_raw(img.width(), img.height(), img.data().to_vec())
.expect("read image");
let image = image::DynamicImage::from(image_buffer);
let img_gray = image.into_luma8();
let mut codes: Vec<_> = decoder
.identify(
img_gray.width() as usize,
img_gray.height() as usize,
&img_gray,
)
.collect();
if process_image_to_find_more {
// try with adaptive_threshold small block_radius
let mut img_gray_threshold = imageproc::contrast::adaptive_threshold(&img_gray, 4);
if codes.is_empty() || codes.first().is_some_and(|qr| qr.is_err()) {
codes = decoder
.identify(
img_gray.width() as usize,
img_gray.height() as usize,
&img_gray_threshold,
)
.collect();
}
// try with adaptive_threshold normal block_radius
img_gray_threshold = imageproc::contrast::adaptive_threshold(&img_gray, 10);
if codes.is_empty() || codes.first().is_some_and(|qr| qr.is_err()) {
codes = decoder
.identify(
img_gray.width() as usize,
img_gray.height() as usize,
&img_gray_threshold,
)
.collect();
}
// try with an inverted version of adaptive_threshold
if codes.is_empty() || codes.first().is_some_and(|qr| qr.is_err()) {
image::imageops::invert(&mut img_gray_threshold);
codes = decoder
.identify(
img_gray.width() as usize,
img_gray.height() as usize,
&img_gray_threshold,
)
.collect();
}
}
let mut result = Vec::new();
codes.iter().for_each(|code| {
if let Ok(code) = code {
result.push(types::QRCode {
corners: code.corners.map(|p| p.into()),
data: match code.decode() {
Ok(data) => data.into(),
Err(err) => types::QRCodeDecodingResult::Error(err.to_string()),
},
})
}
});
ScanResult(result)
}
/// Only for debugging and improving understanding
#[wasm_bindgen]
pub fn luma_image_data(
img: ImageData,
threshold: bool,
inverted: bool,
) -> std::result::Result<web_sys::ImageData, wasm_bindgen::JsValue> {
let image_buffer = image::RgbaImage::from_raw(img.width(), img.height(), img.data().to_vec())
.expect("read image");
let image = image::DynamicImage::from(image_buffer);
let mut img_gray = image.into_luma8();
if threshold {
img_gray = imageproc::contrast::adaptive_threshold(&img_gray, 10);
}
if inverted {
image::imageops::invert(&mut img_gray);
}
let t = img_gray.to_vec();
let mut img2: Vec<u8> = Vec::with_capacity(t.len() * 4);
for p in t {
img2.extend([p, p, p, 255]);
}
ImageData::new_with_u8_clamped_array_and_sh(Clamped(&img2[..]), img.width(), img.height())
}