scandit-sdk
Version:
Scandit Barcode Scanner SDK for the Web
138 lines (126 loc) • 4.64 kB
text/typescript
import "objectFitPolyfill";
import "webrtc-adapter/out/adapter_no_edge_no_global";
import { BrowserCompatibility } from "./lib/browserCompatibility";
import { BrowserHelper } from "./lib/browserHelper";
import { CustomError } from "./lib/customError";
import { UnsupportedBrowserError } from "./lib/unsupportedBrowserError";
import "./styles/styles.scss";
/**
* @hidden
*/
declare global {
interface Window {
MediaStreamTrack?: {
getSources?(callback: (devices: MediaDeviceInfo[]) => void): void;
};
Worker: Function;
WebAssembly: object;
OffscreenCanvas: Function;
WebGLRenderingContext: Function;
objectFitPolyfill(elements?: HTMLElement | HTMLElement[]): void;
}
interface Navigator {
mozVibrate?: Navigator["vibrate"];
msVibrate?: Navigator["vibrate"];
webkitVibrate?: Navigator["vibrate"];
enumerateDevices?(): Promise<MediaDeviceInfo[]>;
}
}
export * from "./lib/barcode";
export * from "./lib/barcodeEncodingRange";
export * from "./lib/barcodePicker";
export * from "./lib/browserCompatibility";
export * from "./lib/browserHelper";
export * from "./lib/camera";
export * from "./lib/cameraAccess";
export * from "./lib/cameraSettings";
export * from "./lib/customError";
export * from "./lib/imageSettings";
export * from "./lib/point";
export * from "./lib/quadrilateral";
export * from "./lib/parser";
export * from "./lib/parserField";
export * from "./lib/parserResult";
export * from "./lib/scanResult";
export * from "./lib/scanner";
export * from "./lib/scanSettings";
export * from "./lib/searchArea";
export * from "./lib/symbologySettings";
export * from "./lib/workers/engineWorker";
/**
* @hidden
*/
export let deviceId: string = BrowserHelper.getDeviceId();
/**
* @hidden
*/
export let userLicenseKey: string | undefined;
/**
* @hidden
*/
export let scanditEngineLocation: string;
/**
* Initialize and configure the Scandit Barcode Scanner SDK library. This function must be called as first thing
* before using any other function of the library.
*
* Depending on parameters and device features, any of the following errors could be the rejected result of the returned
* promise:
* - `NoLicenseKeyError`
* - `UnsupportedBrowserError`
*
* Camera access requests and external Scandit Engine library loads are done lazily only when needed by a
* [[BarcodePicker]] (or [[Scanner]]) object. To make the loading process faster when scanning is actually needed, it is
* recommended depending on the use case to create in advance a (hidden and paused) [[BarcodePicker]] or [[Scanner]]
* object, to later simply show and unpause it when needed. You can also eagerly ask only for camera access permissions
* by calling the [[CameraAccess.getCameras]] function.
*
* @param licenseKey The Scandit license key to be used by the library.
* @param engineLocation <div class="tsd-signature-symbol">Default = "/"</div>
* The location of the folder containing the external scandit-engine-sdk.min.js and
* scandit-engine-sdk.wasm files (external Scandit Engine library).
* By default they are retrieved from the root of the web application.
* Can be a full URL to folder or an absolute folder path.
* @returns A promise resolving when the library has been configured.
*/
export function configure(
licenseKey: string,
{
engineLocation = "/"
}: {
engineLocation?: string;
} = {}
): Promise<void> {
const browserCompatibility: BrowserCompatibility = BrowserHelper.checkBrowserCompatibility();
if (!browserCompatibility.fullSupport && !browserCompatibility.scannerSupport) {
return Promise.reject(new UnsupportedBrowserError(browserCompatibility));
}
if (licenseKey == null || licenseKey.trim().length < 64) {
return Promise.reject(new CustomError({ name: "NoLicenseKeyError", message: "No license key provided" }));
}
userLicenseKey = licenseKey;
engineLocation += engineLocation.slice(-1) === "/" ? "" : "/";
if (/^https?:\/\//.test(engineLocation)) {
scanditEngineLocation = `${engineLocation}`;
} else {
engineLocation = engineLocation
.split("/")
.filter(s => {
return s.length > 0;
})
.join("/");
if (engineLocation === "") {
engineLocation = "/";
} else {
engineLocation = `/${engineLocation}/`;
}
if (location.protocol === "file:" || location.origin === "null") {
scanditEngineLocation = `${location.href
.split("/")
.slice(0, -1)
.join("/")}${engineLocation}`;
} else {
scanditEngineLocation = `${location.origin}${engineLocation}`;
}
}
return Promise.resolve();
}