UNPKG

8.28 kBPlain TextView Raw
1import { BarcodeEncodingRange } from "./barcodeEncodingRange";
2import { Quadrilateral } from "./quadrilateral";
3
4/**
5 * A barcode result.
6 */
7export interface Barcode {
8 /**
9 * The symbology type.
10 */
11 readonly symbology: Barcode.Symbology;
12 /**
13 * The data encoded in the barcode interpreted as a UTF-8 string.
14 * If the raw data is not a valid UTF-8 string, this field will be an empty string.
15 */
16 readonly data: string;
17 /**
18 * The raw data encoded in the barcode, given as an array of bytes.
19 * To interpret this correctly you may have to use the information contained in encodingArray.
20 */
21 readonly rawData: Uint8Array;
22 /**
23 * The location of the barcode.
24 */
25 readonly location: Quadrilateral;
26 /**
27 * Whether the barcode is part of a composite code.
28 */
29 readonly compositeFlag: Barcode.CompositeFlag;
30 /**
31 * Whether the barcode is a GS1 data carrier.
32 */
33 readonly isGs1DataCarrier: boolean;
34 /**
35 * The data encoding of the data in the barcode, given as an array of encoding ranges.
36 */
37 readonly encodingArray: BarcodeEncodingRange[];
38}
39
40/**
41 * @hidden
42 */
43export type BarcodeWASMResult = {
44 readonly symbology: Barcode.Symbology;
45 readonly rawData: number[];
46 readonly location: number[][];
47 readonly compositeFlag: Barcode.CompositeFlag;
48 readonly isGs1DataCarrier: boolean;
49 readonly encodingArray: BarcodeEncodingRange[];
50 readonly isRecognized: boolean;
51};
52
53export namespace Barcode {
54 // Deprecated but useful function for easy UTF-8 handling
55 /**
56 * @hidden
57 */
58 declare function escape(s: string): string;
59
60 /**
61 * @hidden
62 *
63 * Create a [[Barcode]] object from a partial object returned by the external Scandit Engine library.
64 * The *rawData* and *data* fields are computed and stored.
65 *
66 * @param result The barcode result coming from the external Scandit Engine library.
67 * @returns The generated [[Barcode]] object.
68 */
69 export function createFromWASMResult(result: BarcodeWASMResult): Barcode {
70 let decodedData: string;
71 try {
72 decodedData = decodeURIComponent(escape(String.fromCharCode.apply(null, result.rawData)));
73 } catch {
74 decodedData = "";
75 }
76
77 return {
78 symbology: result.symbology,
79 data: decodedData,
80 rawData: new Uint8Array(result.rawData),
81 location: {
82 topLeft: { x: result.location[0][0], y: result.location[0][1] },
83 topRight: { x: result.location[1][0], y: result.location[1][1] },
84 bottomRight: { x: result.location[2][0], y: result.location[2][1] },
85 bottomLeft: { x: result.location[3][0], y: result.location[3][1] }
86 },
87 compositeFlag: result.compositeFlag,
88 isGs1DataCarrier: result.isGs1DataCarrier,
89 encodingArray: result.encodingArray
90 };
91 }
92
93 /**
94 * Barcode symbology type.
95 */
96 export enum Symbology {
97 AZTEC = "aztec",
98 CODABAR = "codabar",
99 CODE11 = "code11",
100 CODE128 = "code128",
101 CODE25 = "code25",
102 CODE32 = "code32",
103 CODE39 = "code39",
104 CODE93 = "code93",
105 DATA_MATRIX = "data-matrix",
106 DOTCODE = "dotcode",
107 EAN13 = "ean13",
108 EAN8 = "ean8",
109 FIVE_DIGIT_ADD_ON = "five-digit-add-on",
110 GS1_DATABAR = "databar",
111 GS1_DATABAR_EXPANDED = "databar-expanded",
112 GS1_DATABAR_LIMITED = "databar-limited",
113 INTERLEAVED_2_OF_5 = "itf",
114 KIX = "kix",
115 LAPA4SC = "lapa4sc",
116 MAXICODE = "maxicode",
117 MICRO_PDF417 = "micropdf417",
118 MICRO_QR = "microqr",
119 MSI_PLESSEY = "msi-plessey",
120 PDF417 = "pdf417",
121 QR = "qr",
122 RM4SCC = "rm4scc",
123 TWO_DIGIT_ADD_ON = "two-digit-add-on",
124 UPCA = "upca",
125 UPCE = "upce"
126 }
127
128 /**
129 * Flags to hint that two codes form a composite code.
130 */
131 export enum CompositeFlag {
132 /**
133 * Code is not part of a composite code.
134 */
135 NONE = 0x0,
136 /**
137 * Code could be part of a composite code. This flag is set by linear (1D) symbologies that have
138 * no composite flag support but can be part of a composite code like the EAN/UPC symbology family.
139 */
140 UNKNOWN = 0x1,
141 /**
142 * Code is the linear component of a composite code. This flag can be set by GS1 DataBar or GS1-128 (Code 128).
143 */
144 LINKED = 0x2,
145 /**
146 * Code is a GS1 Composite Code Type A (CC - A).This flag can be set by MicroPDF417 codes.
147 */
148 GS1_A = 0x4,
149 /**
150 * Code is a GS1 Composite Code Type B (CC-B). This flag can be set by MicroPDF417 codes.
151 */
152 GS1_B = 0x8,
153 /**
154 * Code is a GS1 Composite Code Type C (CC-C). This flag can be set by PDF417 codes.
155 */
156 GS1_C = 0x10
157 }
158
159 // istanbul ignore next
160 export namespace Symbology {
161 // tslint:disable:no-unnecessary-qualifier
162 const humanizedSymbologyNames: Map<string, string> = new Map([
163 [Symbology.AZTEC, "Aztec"],
164 [Symbology.CODABAR, "Codabar"],
165 [Symbology.CODE11, "Code 11"],
166 [Symbology.CODE128, "Code 128"],
167 [Symbology.CODE25, "Code 25"],
168 [Symbology.CODE32, "Code 32"],
169 [Symbology.CODE39, "Code 39"],
170 [Symbology.CODE93, "Code 93"],
171 [Symbology.DATA_MATRIX, "Data Matrix"],
172 [Symbology.DOTCODE, "DotCode"],
173 [Symbology.EAN13, "EAN-13"],
174 [Symbology.EAN8, "EAN-8"],
175 [Symbology.FIVE_DIGIT_ADD_ON, "Five-Digit Add-On"],
176 [Symbology.GS1_DATABAR_EXPANDED, "GS1 DataBar Expanded"],
177 [Symbology.GS1_DATABAR_LIMITED, "GS1 DataBar Limited"],
178 [Symbology.GS1_DATABAR, "GS1 DataBar 14"],
179 [Symbology.INTERLEAVED_2_OF_5, "Interleaved Two of Five"],
180 [Symbology.KIX, "KIX"],
181 [Symbology.LAPA4SC, "LAPA4SC"],
182 [Symbology.MAXICODE, "MaxiCode"],
183 [Symbology.MICRO_PDF417, "MicroPDF417"],
184 [Symbology.MICRO_QR, "Micro QR"],
185 [Symbology.MSI_PLESSEY, "MSI-Plessey"],
186 [Symbology.PDF417, "PDF417"],
187 [Symbology.QR, "QR"],
188 [Symbology.RM4SCC, "RM4SCC"],
189 [Symbology.TWO_DIGIT_ADD_ON, "Two-Digit Add-On"],
190 [Symbology.UPCA, "UPC-A"],
191 [Symbology.UPCE, "UPC-E"]
192 ]);
193
194 const jsonSymbologyNames: Map<string, string> = new Map([
195 [Symbology.AZTEC, "aztec"],
196 [Symbology.CODABAR, "codabar"],
197 [Symbology.CODE11, "code11"],
198 [Symbology.CODE128, "code128"],
199 [Symbology.CODE25, "code25"],
200 [Symbology.CODE32, "code32"],
201 [Symbology.CODE39, "code39"],
202 [Symbology.CODE93, "code93"],
203 [Symbology.DATA_MATRIX, "data-matrix"],
204 [Symbology.DOTCODE, "dotcode"],
205 [Symbology.EAN13, "ean13"],
206 [Symbology.EAN8, "ean8"],
207 [Symbology.FIVE_DIGIT_ADD_ON, "five-digit-add-on"],
208 [Symbology.GS1_DATABAR_EXPANDED, "databar-expanded"],
209 [Symbology.GS1_DATABAR_LIMITED, "databar-limited"],
210 [Symbology.GS1_DATABAR, "databar"],
211 [Symbology.INTERLEAVED_2_OF_5, "itf"],
212 [Symbology.KIX, "kix"],
213 [Symbology.LAPA4SC, "lapa4sc"],
214 [Symbology.MAXICODE, "maxicode"],
215 [Symbology.MICRO_PDF417, "micropdf417"],
216 [Symbology.MICRO_QR, "microqr"],
217 [Symbology.MSI_PLESSEY, "msi-plessey"],
218 [Symbology.PDF417, "pdf417"],
219 [Symbology.QR, "qr"],
220 [Symbology.RM4SCC, "rm4scc"],
221 [Symbology.TWO_DIGIT_ADD_ON, "two-digit-add-on"],
222 [Symbology.UPCA, "upca"],
223 [Symbology.UPCE, "upce"]
224 ]);
225 // tslint:enable:no-unnecessary-qualifier
226
227 /**
228 * Get the humanized name of a symbology.
229 *
230 * @param symbology The symbology for which to retrieve the name.
231 * @returns The humanized name of the symbology.
232 */
233 export function toHumanizedName(symbology: Symbology): string {
234 const humanizedSymbologyName: string | undefined = humanizedSymbologyNames.get(symbology.toLowerCase());
235 if (humanizedSymbologyName == null) {
236 return "Unknown";
237 }
238
239 return humanizedSymbologyName;
240 }
241
242 /**
243 * Get the JSON key name of a symbology, used for JSON-formatted ScanSettings and Scandit Engine library.
244 *
245 * @param symbology The symbology for which to retrieve the name.
246 * @returns The json key name of the symbology.
247 */
248 export function toJSONName(symbology: Symbology): string {
249 const jsonSymbologyName: string | undefined = jsonSymbologyNames.get(symbology.toLowerCase());
250 if (jsonSymbologyName == null) {
251 return "unknown";
252 }
253
254 return jsonSymbologyName;
255 }
256 }
257}