forked from rxing-core/rxing-wasm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecode_hints.rs
294 lines (265 loc) · 10.8 KB
/
decode_hints.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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
use std::collections::{HashMap, HashSet};
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub enum DecodeHintTypes {
/**
* Unspecified, application-specific hint. Maps to an unspecified {@link Object}.
*/
Other,
/**
* Image is a pure monochrome image of a barcode. Doesn't matter what it maps to;
* use {@link Boolean#TRUE}.
*/
PureBarcode,
/**
* Image is known to be of one of a few possible formats.
* Maps to a {@link List} of {@link BarcodeFormat}s.
*/
PossibleFormats,
/**
* Spend more time to try to find a barcode; optimize for accuracy, not speed.
* Doesn't matter what it maps to; use {@link Boolean#TRUE}.
*/
TryHarder,
/**
* Specifies what character encoding to use when decoding, where applicable (type String)
*/
CharacterSet,
/**
* Allowed lengths of encoded data -- reject anything else. Maps to an {@code int[]}.
*/
AllowedLengths,
/**
* Assume Code 39 codes employ a check digit. Doesn't matter what it maps to;
* use {@link Boolean#TRUE}.
*/
AssumeCode39CheckDigit,
/**
* Assume the barcode is being processed as a GS1 barcode, and modify behavior as needed.
* For example this affects FNC1 handling for Code 128 (aka GS1-128). Doesn't matter what it maps to;
* use {@link Boolean#TRUE}.
*/
AssumeGs1,
/**
* If true, return the start and end digits in a Codabar barcode instead of stripping them. They
* are alpha, whereas the rest are numeric. By default, they are stripped, but this causes them
* to not be. Doesn't matter what it maps to; use {@link Boolean#TRUE}.
*/
ReturnCodabarStartEnd,
/**
* The caller needs to be notified via callback when a possible {@link RXingResultPoint}
* is found. Maps to a {@link RXingResultPointCallback}.
*/
NeedResultPointCallback,
/**
* Allowed extension lengths for EAN or UPC barcodes. Other formats will ignore this.
* Maps to an {@code int[]} of the allowed extension lengths, for example [2], [5], or [2, 5].
* If it is optional to have an extension, do not set this hint. If this is set,
* and a UPC or EAN barcode is found but an extension is not, then no result will be returned
* at all.
*/
AllowedEanExtensions,
/**
* If true, also tries to decode as inverted image. All configured decoders are simply called a
* second time with an inverted image. Doesn't matter what it maps to; use {@link Boolean#TRUE}.
*/
AlsoInverted,
/**
* Translate the ASCII values parsed by the Telepen reader into the Telepen Numeric form; use {@link Boolean#TRUE}.
*/
TelepenAsNumeric,
}
impl From<DecodeHintTypes> for rxing::DecodeHintType {
fn from(value: DecodeHintTypes) -> Self {
match value {
DecodeHintTypes::Other => rxing::DecodeHintType::OTHER,
DecodeHintTypes::PureBarcode => rxing::DecodeHintType::PURE_BARCODE,
DecodeHintTypes::PossibleFormats => rxing::DecodeHintType::POSSIBLE_FORMATS,
DecodeHintTypes::TryHarder => rxing::DecodeHintType::TRY_HARDER,
DecodeHintTypes::CharacterSet => rxing::DecodeHintType::CHARACTER_SET,
DecodeHintTypes::AllowedLengths => rxing::DecodeHintType::ALLOWED_LENGTHS,
DecodeHintTypes::AssumeCode39CheckDigit => {
rxing::DecodeHintType::ASSUME_CODE_39_CHECK_DIGIT
}
DecodeHintTypes::AssumeGs1 => rxing::DecodeHintType::ASSUME_GS1,
DecodeHintTypes::ReturnCodabarStartEnd => {
rxing::DecodeHintType::RETURN_CODABAR_START_END
}
DecodeHintTypes::NeedResultPointCallback => {
rxing::DecodeHintType::NEED_RESULT_POINT_CALLBACK
}
DecodeHintTypes::AllowedEanExtensions => rxing::DecodeHintType::ALLOWED_EAN_EXTENSIONS,
DecodeHintTypes::AlsoInverted => rxing::DecodeHintType::ALSO_INVERTED,
DecodeHintTypes::TelepenAsNumeric => rxing::DecodeHintType::TELEPEN_AS_NUMERIC,
}
}
}
#[wasm_bindgen]
#[derive(Default, Clone)]
pub struct DecodeHintDictionary(HashMap<rxing::DecodeHintType, rxing::DecodeHintValue>);
#[wasm_bindgen]
impl DecodeHintDictionary {
#[wasm_bindgen(constructor)]
pub fn new() -> DecodeHintDictionary {
DecodeHintDictionary(HashMap::new())
}
#[wasm_bindgen]
pub fn get_hint(&self, hint: DecodeHintTypes) -> String {
if let Some(val) = self.0.get(&hint.into()) {
match val {
rxing::DecodeHintValue::Other(val) | rxing::DecodeHintValue::CharacterSet(val) => {
val.to_owned()
}
rxing::DecodeHintValue::PureBarcode(val)
| rxing::DecodeHintValue::AssumeCode39CheckDigit(val)
| rxing::DecodeHintValue::AssumeGs1(val)
| rxing::DecodeHintValue::ReturnCodabarStartEnd(val)
| rxing::DecodeHintValue::TryHarder(val)
| rxing::DecodeHintValue::TelepenAsNumeric(val)
| rxing::DecodeHintValue::AlsoInverted(val) => val.to_string(),
rxing::DecodeHintValue::PossibleFormats(val) => val
.iter()
.fold("".to_string(), |acc, v| acc + v.to_string().as_str()),
rxing::DecodeHintValue::AllowedLengths(val)
| rxing::DecodeHintValue::AllowedEanExtensions(val) => val
.iter()
.fold("".to_string(), |acc, v| acc + v.to_string().as_str()),
rxing::DecodeHintValue::NeedResultPointCallback(_) => String::from("UNSUPORTED"),
}
} else {
String::from("")
}
}
#[wasm_bindgen]
pub fn set_hint(&mut self, hint: DecodeHintTypes, value: String) -> bool {
if value.is_empty() {
return false;
}
match hint {
DecodeHintTypes::Other => {
self.0
.insert(hint.into(), rxing::DecodeHintValue::Other(value));
}
DecodeHintTypes::PureBarcode => {
let Ok(pure_barcode) = value.parse() else {
return false;
};
self.0.insert(
hint.into(),
rxing::DecodeHintValue::PureBarcode(pure_barcode),
);
}
DecodeHintTypes::PossibleFormats => {
let formats = HashSet::from_iter(value.split(',').map(rxing::BarcodeFormat::from));
if formats.is_empty() {
return false;
}
self.0.insert(
hint.into(),
rxing::DecodeHintValue::PossibleFormats(formats),
);
}
DecodeHintTypes::TryHarder => {
let Ok(try_harder) = value.parse() else {
return false;
};
self.0
.insert(hint.into(), rxing::DecodeHintValue::TryHarder(try_harder));
}
DecodeHintTypes::CharacterSet => {
self.0
.insert(hint.into(), rxing::DecodeHintValue::CharacterSet(value));
}
DecodeHintTypes::AllowedLengths => {
let lengths = value.split(',').flat_map(|v| v.parse()).collect();
self.0
.insert(hint.into(), rxing::DecodeHintValue::AllowedLengths(lengths));
}
DecodeHintTypes::AssumeCode39CheckDigit => {
let Ok(assume_code_39_check_digit) = value.parse() else {
return false;
};
self.0.insert(
hint.into(),
rxing::DecodeHintValue::AssumeCode39CheckDigit(assume_code_39_check_digit),
);
}
DecodeHintTypes::AssumeGs1 => {
let Ok(assume_gs1) = value.parse() else {
return false;
};
self.0
.insert(hint.into(), rxing::DecodeHintValue::AssumeGs1(assume_gs1));
}
DecodeHintTypes::ReturnCodabarStartEnd => {
let Ok(return_codebar_start_end) = value.parse() else {
return false;
};
self.0.insert(
hint.into(),
rxing::DecodeHintValue::ReturnCodabarStartEnd(return_codebar_start_end),
);
}
DecodeHintTypes::NeedResultPointCallback => {
return false;
}
DecodeHintTypes::AllowedEanExtensions => {
let extensions = value.split(',').flat_map(|v| v.parse()).collect();
self.0.insert(
hint.into(),
rxing::DecodeHintValue::AllowedEanExtensions(extensions),
);
}
DecodeHintTypes::AlsoInverted => {
let Ok(also_inverted) = value.parse() else {
return false;
};
self.0.insert(
hint.into(),
rxing::DecodeHintValue::AlsoInverted(also_inverted),
);
}
DecodeHintTypes::TelepenAsNumeric => {
let Ok(telepen_as_numeric) = value.parse() else {
return false;
};
self.0
.insert(hint.into(), rxing::DecodeHintValue::TelepenAsNumeric(telepen_as_numeric));
}
}
true
}
#[wasm_bindgen]
pub fn remove_hint(&mut self, hint: DecodeHintTypes) -> bool {
let h: rxing::DecodeHintType = hint.into();
if self.0.contains_key(&h) {
self.0.remove(&h).is_some()
} else {
false
}
}
// #[wasm_bindgen]
// pub fn setResultPointCallback(&mut self, callback: &js_sys::Function){
// self.0.insert(rxing::DecodeHintType::NEED_RESULT_POINT_CALLBACK, rxing::DecodeHintValue::NeedResultPointCallback(
// Rc::new(|point: &dyn rxing::ResultPoint| {
// let this = JsValue::null();
// let js_point = wasm_bindgen::JsValue::from( js_sys::Array::from_iter([wasm_bindgen::JsValue::from(point.getX()), wasm_bindgen::JsValue::from(point.getY())].iter()) );
// callback.call1(&this, &js_point);
// })
// ));
// }
// #[wasm_bindgen]
// pub fn clearResultPointCallback(&mut self) -> bool {
// self.0.remove(&rxing::DecodeHintType::NEED_RESULT_POINT_CALLBACK).is_some()
// }
}
impl DecodeHintDictionary {
pub fn get_dictionary(&self) -> &HashMap<rxing::DecodeHintType, rxing::DecodeHintValue> {
&self.0
}
pub fn get_dictionary_mut(
&mut self,
) -> &mut HashMap<rxing::DecodeHintType, rxing::DecodeHintValue> {
&mut self.0
}
}