scandit-sdk
Version:
Scandit Barcode Scanner SDK for the Web
258 lines (244 loc) • 8.28 kB
text/typescript
import { BarcodeEncodingRange } from "./barcodeEncodingRange";
import { Quadrilateral } from "./quadrilateral";
/**
* A barcode result.
*/
export interface Barcode {
/**
* The symbology type.
*/
readonly symbology: Barcode.Symbology;
/**
* The data encoded in the barcode interpreted as a UTF-8 string.
* If the raw data is not a valid UTF-8 string, this field will be an empty string.
*/
readonly data: string;
/**
* The raw data encoded in the barcode, given as an array of bytes.
* To interpret this correctly you may have to use the information contained in encodingArray.
*/
readonly rawData: Uint8Array;
/**
* The location of the barcode.
*/
readonly location: Quadrilateral;
/**
* Whether the barcode is part of a composite code.
*/
readonly compositeFlag: Barcode.CompositeFlag;
/**
* Whether the barcode is a GS1 data carrier.
*/
readonly isGs1DataCarrier: boolean;
/**
* The data encoding of the data in the barcode, given as an array of encoding ranges.
*/
readonly encodingArray: BarcodeEncodingRange[];
}
/**
* @hidden
*/
export type BarcodeWASMResult = {
readonly symbology: Barcode.Symbology;
readonly rawData: number[];
readonly location: number[][];
readonly compositeFlag: Barcode.CompositeFlag;
readonly isGs1DataCarrier: boolean;
readonly encodingArray: BarcodeEncodingRange[];
readonly isRecognized: boolean;
};
export namespace Barcode {
// Deprecated but useful function for easy UTF-8 handling
/**
* @hidden
*/
declare function escape(s: string): string;
/**
* @hidden
*
* Create a [[Barcode]] object from a partial object returned by the external Scandit Engine library.
* The *rawData* and *data* fields are computed and stored.
*
* @param result The barcode result coming from the external Scandit Engine library.
* @returns The generated [[Barcode]] object.
*/
export function createFromWASMResult(result: BarcodeWASMResult): Barcode {
let decodedData: string;
try {
decodedData = decodeURIComponent(escape(String.fromCharCode.apply(null, result.rawData)));
} catch {
decodedData = "";
}
return {
symbology: result.symbology,
data: decodedData,
rawData: new Uint8Array(result.rawData),
location: {
topLeft: { x: result.location[0][0], y: result.location[0][1] },
topRight: { x: result.location[1][0], y: result.location[1][1] },
bottomRight: { x: result.location[2][0], y: result.location[2][1] },
bottomLeft: { x: result.location[3][0], y: result.location[3][1] }
},
compositeFlag: result.compositeFlag,
isGs1DataCarrier: result.isGs1DataCarrier,
encodingArray: result.encodingArray
};
}
/**
* Barcode symbology type.
*/
export enum Symbology {
AZTEC = "aztec",
CODABAR = "codabar",
CODE11 = "code11",
CODE128 = "code128",
CODE25 = "code25",
CODE32 = "code32",
CODE39 = "code39",
CODE93 = "code93",
DATA_MATRIX = "data-matrix",
DOTCODE = "dotcode",
EAN13 = "ean13",
EAN8 = "ean8",
FIVE_DIGIT_ADD_ON = "five-digit-add-on",
GS1_DATABAR = "databar",
GS1_DATABAR_EXPANDED = "databar-expanded",
GS1_DATABAR_LIMITED = "databar-limited",
INTERLEAVED_2_OF_5 = "itf",
KIX = "kix",
LAPA4SC = "lapa4sc",
MAXICODE = "maxicode",
MICRO_PDF417 = "micropdf417",
MICRO_QR = "microqr",
MSI_PLESSEY = "msi-plessey",
PDF417 = "pdf417",
QR = "qr",
RM4SCC = "rm4scc",
TWO_DIGIT_ADD_ON = "two-digit-add-on",
UPCA = "upca",
UPCE = "upce"
}
/**
* Flags to hint that two codes form a composite code.
*/
export enum CompositeFlag {
/**
* Code is not part of a composite code.
*/
NONE = 0x0,
/**
* Code could be part of a composite code. This flag is set by linear (1D) symbologies that have
* no composite flag support but can be part of a composite code like the EAN/UPC symbology family.
*/
UNKNOWN = 0x1,
/**
* Code is the linear component of a composite code. This flag can be set by GS1 DataBar or GS1-128 (Code 128).
*/
LINKED = 0x2,
/**
* Code is a GS1 Composite Code Type A (CC - A).This flag can be set by MicroPDF417 codes.
*/
GS1_A = 0x4,
/**
* Code is a GS1 Composite Code Type B (CC-B). This flag can be set by MicroPDF417 codes.
*/
GS1_B = 0x8,
/**
* Code is a GS1 Composite Code Type C (CC-C). This flag can be set by PDF417 codes.
*/
GS1_C = 0x10
}
// istanbul ignore next
export namespace Symbology {
// tslint:disable:no-unnecessary-qualifier
const humanizedSymbologyNames: Map<string, string> = new Map([
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[]
]);
const jsonSymbologyNames: Map<string, string> = new Map([
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[]
]);
// tslint:enable:no-unnecessary-qualifier
/**
* Get the humanized name of a symbology.
*
* @param symbology The symbology for which to retrieve the name.
* @returns The humanized name of the symbology.
*/
export function toHumanizedName(symbology: Symbology): string {
const humanizedSymbologyName: string | undefined = humanizedSymbologyNames.get(symbology.toLowerCase());
if (humanizedSymbologyName == null) {
return "Unknown";
}
return humanizedSymbologyName;
}
/**
* Get the JSON key name of a symbology, used for JSON-formatted ScanSettings and Scandit Engine library.
*
* @param symbology The symbology for which to retrieve the name.
* @returns The json key name of the symbology.
*/
export function toJSONName(symbology: Symbology): string {
const jsonSymbologyName: string | undefined = jsonSymbologyNames.get(symbology.toLowerCase());
if (jsonSymbologyName == null) {
return "unknown";
}
return jsonSymbologyName;
}
}
}