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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
use image::DynamicImage;
use image::GrayImage;
use failure::Error;
use crate::decode::{Decode, QRDecoder};
use crate::detect::{Detect, LineScan, Location};
use crate::extract::{Extract, QRExtractor};
use crate::prepare::{BlockedMean, Prepare};
use crate::util::qr::{QRData, QRError, QRLocation};
pub struct Decoder<IMG, PREPD> {
prepare: Box<dyn Prepare<IMG, PREPD>>,
detect: Box<dyn Detect<PREPD>>,
qr: ExtractDecode<PREPD, QRLocation, QRData, QRError>,
}
impl<IMG, PREPD> Decoder<IMG, PREPD> {
pub fn decode(&self, source: IMG) -> Vec<Result<String, Error>> {
let prepared = self.prepare.prepare(source);
let locations = self.detect.detect(&prepared);
if locations.is_empty() {
return vec![];
}
let mut all_decoded = vec![];
for location in locations {
match location {
Location::QR(qrloc) => {
let extracted = self.qr.extract.extract(&prepared, qrloc);
let decoded = self.qr.decode.decode(extracted);
all_decoded.push(decoded.or_else(|err| Err(Error::from(err))));
}
}
}
all_decoded
}
}
pub fn default_decoder() -> Decoder<DynamicImage, GrayImage> {
default_builder().build()
}
pub struct DecoderBuilder<IMG, PREPD> {
prepare: Option<Box<dyn Prepare<IMG, PREPD>>>,
detect: Option<Box<dyn Detect<PREPD>>>,
qr: Option<ExtractDecode<PREPD, QRLocation, QRData, QRError>>,
}
impl<IMG, PREPD> DecoderBuilder<IMG, PREPD> {
pub fn new() -> DecoderBuilder<IMG, PREPD> {
DecoderBuilder {
prepare: None,
detect: None,
qr: None,
}
}
pub fn prepare(
&mut self,
prepare: Box<dyn Prepare<IMG, PREPD>>,
) -> &mut DecoderBuilder<IMG, PREPD> {
self.prepare = Some(prepare);
self
}
pub fn detect(&mut self, detect: Box<dyn Detect<PREPD>>) -> &mut DecoderBuilder<IMG, PREPD> {
self.detect = Some(detect);
self
}
pub fn qr(
&mut self,
extract: Box<dyn Extract<PREPD, QRLocation, QRData, QRError>>,
decode: Box<dyn Decode<QRData, QRError>>,
) -> &mut DecoderBuilder<IMG, PREPD> {
self.qr = Some(ExtractDecode { extract, decode });
self
}
pub fn build(self) -> Decoder<IMG, PREPD> {
if self.prepare.is_none() {
panic!("Cannot build Decoder without Prepare component");
}
if self.detect.is_none() {
panic!("Cannot build Decoder without Detect component");
}
Decoder {
prepare: self.prepare.unwrap(),
detect: self.detect.unwrap(),
qr: self.qr.unwrap(),
}
}
}
pub fn default_builder() -> DecoderBuilder<DynamicImage, GrayImage> {
let mut db = DecoderBuilder::new();
db.prepare(Box::new(BlockedMean::new(5, 7)));
db.detect(Box::new(LineScan::new()));
db.qr(Box::new(QRExtractor::new()), Box::new(QRDecoder::new()));
db
}
struct ExtractDecode<PREPD, LOC, DATA, ERROR> {
extract: Box<dyn Extract<PREPD, LOC, DATA, ERROR>>,
decode: Box<dyn Decode<DATA, ERROR>>,
}