UNPKG

11.1 kBPlain TextView Raw
1/**
2 * A symbology-specific configuration object.
3 *
4 * See https://docs.scandit.com/stable/c_api/symbologies.html for more details.
5 */
6export class SymbologySettings {
7 private enabled: boolean;
8 private colorInvertedEnabled: boolean;
9 private activeSymbolCounts: number[];
10 private extensions: Set<SymbologySettings.Extension>;
11 private checksums: Set<SymbologySettings.Checksum>;
12 private customExtensions: boolean;
13 private customChecksums: boolean;
14
15 /**
16 * Create a SymbologySettings instance.
17 *
18 * @param enabled <div class="tsd-signature-symbol">Default =&nbsp;false</div>
19 * Whether the symbology is enabled for recognition.
20 * @param colorInvertedEnabled <div class="tsd-signature-symbol">Default =&nbsp;false</div>
21 * Whether color inverted recognition is enabled.
22 * @param activeSymbolCounts
23 * <div class="tsd-signature-symbol">Default =&nbsp;[] &nbsp;(default symbology range)</div>
24 * The list of active symbol counts.
25 * @param extensions
26 * <div class="tsd-signature-symbol">Default =&nbsp;undefined &nbsp;(default symbology extensions)</div>
27 * The list/set of enabled extensions.
28 * @param checksums
29 * <div class="tsd-signature-symbol">Default =&nbsp;undefined &nbsp;(default symbology checksums)</div>
30 * The list/set of enabled checksums.
31 */
32 constructor({
33 enabled = false,
34 colorInvertedEnabled = false,
35 activeSymbolCounts = [],
36 extensions,
37 checksums
38 }: {
39 enabled?: boolean;
40 colorInvertedEnabled?: boolean;
41 activeSymbolCounts?: number[];
42 extensions?: SymbologySettings.Extension[] | Set<SymbologySettings.Extension>;
43 checksums?: SymbologySettings.Checksum[] | Set<SymbologySettings.Checksum>;
44 } = {}) {
45 this.enabled = enabled;
46 this.colorInvertedEnabled = colorInvertedEnabled;
47 this.activeSymbolCounts = activeSymbolCounts;
48 this.customExtensions = extensions != null;
49 this.customChecksums = checksums != null;
50 if (extensions == null) {
51 extensions = [];
52 }
53 if (checksums == null) {
54 checksums = [];
55 }
56 this.extensions = new Set(
57 Array.from(extensions).filter(extension => {
58 return this.isValidExtension(extension);
59 })
60 );
61 this.checksums = new Set(
62 Array.from(checksums).filter(checksum => {
63 return this.isValidChecksum(checksum);
64 })
65 );
66 }
67
68 /**
69 * @returns Whether the symbology enabled for recognition.
70 */
71 public isEnabled(): boolean {
72 return this.enabled;
73 }
74
75 /**
76 * Enable or disable recognition of the symbology.
77 *
78 * @param enabled Whether the symbology is enabled for recognition.
79 * @returns The updated [[SymbologySettings]] object.
80 */
81 public setEnabled(enabled: boolean): SymbologySettings {
82 this.enabled = enabled;
83
84 return this;
85 }
86
87 /**
88 * @returns Whether color inverted recognition is enabled.
89 */
90 public isColorInvertedEnabled(): boolean {
91 return this.colorInvertedEnabled;
92 }
93
94 /**
95 * Enable or disable recognition of inverted-color symbology (in addition to normal colors).
96 *
97 * @param enabled Whether color inverted recognition is enabled.
98 * @returns The updated [[SymbologySettings]] object.
99 */
100 public setColorInvertedEnabled(enabled: boolean): SymbologySettings {
101 this.colorInvertedEnabled = enabled;
102
103 return this;
104 }
105
106 /**
107 * Get the currently set custom list of active symbol counts.
108 * If never set, an empty array is returned
109 * but the Scandit Engine library will use the default list for the symbology.
110 *
111 * @returns The list of active symbol counts.
112 */
113 public getActiveSymbolCounts(): number[] {
114 return this.activeSymbolCounts;
115 }
116
117 /**
118 * Set the list of active symbol counts.
119 *
120 * @param activeSymbolCounts The list of active symbol counts.
121 * @returns The updated [[SymbologySettings]] object.
122 */
123 public setActiveSymbolCounts(activeSymbolCounts: number[]): SymbologySettings {
124 this.activeSymbolCounts = activeSymbolCounts;
125
126 return this;
127 }
128
129 /**
130 * Set the (inclusive) range of active symbol counts.
131 *
132 * @param minCount The minimum accepted number of symbols.
133 * @param maxCount The maximum accepted number of symbols.
134 * @returns The updated [[SymbologySettings]] object.
135 */
136 public setActiveSymbolCountsRange(minCount: number, maxCount: number): SymbologySettings {
137 this.activeSymbolCounts = Array.from({ length: maxCount - minCount + 1 }, (_, k) => {
138 return k + minCount;
139 });
140
141 return this;
142 }
143
144 /**
145 * Get the currently set custom set of extensions.
146 * If never set, an empty set is returned
147 * but the Scandit Engine library will use the default extension set for the symbology.
148 *
149 * @returns The set of enabled extensions.
150 */
151 public getEnabledExtensions(): Set<SymbologySettings.Extension> {
152 return this.extensions;
153 }
154
155 /**
156 * Enable an extension or list/set of extensions
157 *
158 * @param extension The single extension or list/set of extensions to enable.
159 * @returns The updated [[SymbologySettings]] object.
160 */
161 public enableExtensions(
162 extension: SymbologySettings.Extension | SymbologySettings.Extension[] | Set<SymbologySettings.Extension>
163 ): SymbologySettings {
164 this.customExtensions = true;
165 if (typeof extension === "object") {
166 this.extensions = new Set([
167 ...this.extensions,
168 ...Array.from(extension).filter(e => {
169 return this.isValidExtension(e);
170 })
171 ]);
172 } else {
173 if (this.isValidExtension(extension)) {
174 this.extensions.add(extension);
175 }
176 }
177
178 return this;
179 }
180
181 /**
182 * Disable an extension or list/set of extensions.
183 *
184 * @param extension The single extension or list/set of extensions to disable.
185 * @returns The updated [[SymbologySettings]] object.
186 */
187 public disableExtensions(
188 extension: SymbologySettings.Extension | SymbologySettings.Extension[] | Set<SymbologySettings.Extension>
189 ): SymbologySettings {
190 if (typeof extension === "object") {
191 this.extensions = new Set(
192 [...this.extensions].filter(x => {
193 return extension instanceof Array ? !extension.includes(x) : !extension.has(x);
194 })
195 );
196 } else {
197 this.extensions.delete(extension);
198 }
199
200 return this;
201 }
202
203 /**
204 * Get the currently set custom set of checksums.
205 * If never set, an empty set is returned
206 * but the Scandit Engine library will use the default checksum set for the symbology.
207 *
208 * @returns The set of enabled checksums.
209 */
210 public getEnabledChecksums(): Set<SymbologySettings.Checksum> {
211 return this.checksums;
212 }
213
214 /**
215 * Enable a checksum or list/set of checksums.
216 *
217 * @param checksum The single checksum or list/set of checksums to enable.
218 * @returns The updated [[SymbologySettings]] object.
219 */
220 public enableChecksums(
221 checksum: SymbologySettings.Checksum | SymbologySettings.Checksum[] | Set<SymbologySettings.Checksum>
222 ): SymbologySettings {
223 this.customChecksums = true;
224 if (typeof checksum === "object") {
225 this.checksums = new Set([
226 ...this.checksums,
227 ...Array.from(checksum).filter(c => {
228 return this.isValidChecksum(c);
229 })
230 ]);
231 } else {
232 if (this.isValidChecksum(checksum)) {
233 this.checksums.add(checksum);
234 }
235 }
236
237 return this;
238 }
239
240 /**
241 * Disable a checksum or list/set of checksums.
242 *
243 * @param checksum The single checksum or list/set of checksums to disable.
244 * @returns The updated [[SymbologySettings]] object.
245 */
246 public disableChecksums(
247 checksum: SymbologySettings.Checksum | SymbologySettings.Checksum[] | Set<SymbologySettings.Checksum>
248 ): SymbologySettings {
249 if (typeof checksum === "object") {
250 this.checksums = new Set(
251 [...this.checksums].filter(x => {
252 return checksum instanceof Array ? !checksum.includes(x) : !checksum.has(x);
253 })
254 );
255 } else {
256 this.checksums.delete(checksum);
257 }
258
259 return this;
260 }
261
262 protected toJSON(): object {
263 return {
264 enabled: this.enabled,
265 colorInvertedEnabled: this.colorInvertedEnabled,
266 activeSymbolCounts: this.activeSymbolCounts.length === 0 ? undefined : this.activeSymbolCounts,
267 extensions: this.customExtensions ? Array.from(this.extensions) : undefined,
268 checksums: this.customChecksums ? Array.from(this.checksums) : undefined
269 };
270 }
271
272 private isValidExtension(extension: string | SymbologySettings.Extension): boolean {
273 return (
274 extension in SymbologySettings.Extension ||
275 (<string[]>Object.values(SymbologySettings.Extension)).includes(extension.toLowerCase())
276 );
277 }
278
279 private isValidChecksum(checksum: string | SymbologySettings.Checksum): boolean {
280 return (
281 checksum in SymbologySettings.Checksum ||
282 (<string[]>Object.values(SymbologySettings.Checksum)).includes(checksum.toLowerCase())
283 );
284 }
285}
286
287// istanbul ignore next
288export namespace SymbologySettings {
289 /**
290 * Symbology extensions for particular functionalities, only applicable to specific barcodes.
291 * See: https://docs.scandit.com/stable/c_api/symbologies.html.
292 */
293 export enum Extension {
294 /**
295 * Improve scan performance when reading direct part marked (DPM) Data Matrix codes.
296 * Enabling this extension comes at the cost of increased frame processing times.
297 */
298 DIRECT_PART_MARKING_MODE = "direct_part_marking_mode",
299 /**
300 * Interpret the Code 39 / Code 93 code data using two symbols per output character to encode all ASCII characters.
301 */
302 FULL_ASCII = "full_ascii",
303 /**
304 * Enable scanning codes that have quiet zones (white area before and after the code) significantly smaller
305 * than what's allowed by the symbology specification.
306 */
307 RELAXED_SHARP_QUIET_ZONE_CHECK = "relaxed_sharp_quiet_zone_check",
308 /**
309 * Remove the leading zero digit from the result.
310 */
311 REMOVE_LEADING_ZERO = "remove_leading_zero",
312 /**
313 * Remove the leading zero digit from the result if the UPC-A representation extension "RETURN_AS_UPCA" is enabled.
314 */
315 REMOVE_LEADING_UPCA_ZERO = "remove_leading_upca_zero",
316 /**
317 * Transform the UPC-E result into its UPC-A representation.
318 */
319 RETURN_AS_UPCA = "return_as_upca",
320 /**
321 * Remove the leading FNC1 character that indicates a GS1 code.
322 */
323 STRIP_LEADING_FNC1 = "strip_leading_fnc1"
324 }
325
326 /**
327 * Checksum algorithms, only applicable to specific barcodes.
328 * See: https://docs.scandit.com/stable/c_api/symbologies.html.
329 */
330 export enum Checksum {
331 /**
332 * Modulo 10 checksum.
333 */
334 MOD_10 = "mod10",
335 /**
336 * Modulo 11 checksum.
337 */
338 MOD_11 = "mod11",
339 /**
340 * Modulo 16 checksum.
341 */
342 MOD_16 = "mod16",
343 /**
344 * Modulo 43 checksum.
345 */
346 MOD_43 = "mod43",
347 /**
348 * Modulo 47 checksum.
349 */
350 MOD_47 = "mod47",
351 /**
352 * Modulo 103 checksum.
353 */
354 MOD_103 = "mod103",
355 /**
356 * Two modulo 10 checksums.
357 */
358 MOD_1010 = "mod1010",
359 /**
360 * Modulo 11 and modulo 10 checksum.
361 */
362 MOD_1110 = "mod1110"
363 }
364}