-
Notifications
You must be signed in to change notification settings - Fork 0
/
scan-single.js
62 lines (56 loc) · 1.95 KB
/
scan-single.js
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
/**
* Import STRICH SDK via ES6 import clause. It is recommended to pin a specific version, especially for
* business-critical applications.
*/
import {StrichSDK, BarcodeReader} from "https://cdn.jsdelivr.net/npm/@pixelverse/strichjs-sdk@1.6.0";
/**
* Store the scanned code in the session storage and return to the home page.
*/
function handleCodeDetection(codeDetection) {
sessionStorage.setItem('scannedCode', codeDetection.data);
window.location.replace('/');
}
/**
* Initialize STRICH BarcodeReader and start scanning.
*/
async function initializeBarcodeReader() {
// see https://docs.strich.io/reference/interfaces/Configuration.html for all available options
let configuration = {
selector: '.barcode-scanner',
frameSource: {
resolution: 'full-hd'
},
engine: {
symbologies: ['code128', 'qr'],
duplicateInterval: 1500
}
};
// initialize BarcodeReader and start scanning barcodes
const barcodeReader = await new BarcodeReader(configuration).initialize();
barcodeReader.detected = (detections) => {
barcodeReader.destroy();
handleCodeDetection(detections[0]);
};
await barcodeReader.start();
}
/**
* Handle initialization error. All exceptions thrown by the SDK are of type SdkError:
* https://docs.strich.io/reference/classes/SdkError.html
*
* This is just for illustration, in a production app you would not be using window.alert().
*/
function handleInitializationError(err) {
let message = err.message;
if (err.detailMessage) { // some errors contain additional details in the detailMessage property
message += '\n' + err.detailMessage;
}
window.alert(`Initialization failed: ${message}`);
}
// Initialize STRICH SDK and BarcodeReader
const licenseKey = '<your license key>';
try {
await StrichSDK.initialize(licenseKey);
await initializeBarcodeReader();
} catch (err) {
handleInitializationError(err);
}