Created
November 7, 2023 09:49
-
-
Save suzukieng/854f215bafcf5affc169cd97e108f19a to your computer and use it in GitHub Desktop.
PDF417 sample code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// import STRICH SDK as an ES6 module directly from a CDN | |
import {StrichSDK, BarcodeReader} from 'https://cdn.jsdelivr.net/npm/@pixelverse/[email protected]/dist/strich.js'; | |
// AAMVA helper routines | |
import {parseAAMVALicenseData} from "./aamva.js"; | |
function processPDF417Barcode(codeDetection) { | |
// attempt to parse barcode data as AAMVA driver's license | |
const parsed = parseAAMVALicenseData(codeDetection.data); | |
if (!parsed) { | |
console.error('PDF417 data could not be parsed according to AAMVA spec'); | |
return; | |
} | |
// calculate age from system time | |
const age = new Date().getFullYear() - parsed.dateOfBirth.getFullYear(); | |
// depending on age, show success or reject popup | |
const dialog = document.getElementById(age < 21 ? 'failure' : 'success'); | |
dialog.getElementsByClassName('popup-name')[0].innerText = parsed.firstName + ' ' + parsed.lastName; | |
dialog.getElementsByClassName('popup-age')[0].innerText = age + ' yrs old'; | |
dialog.showModal(); | |
} | |
// Initialize BarcodeReader with appropriate settings for PDF417 | |
function initializeBarcodeReader() { | |
let configuration = { | |
selector: '.container', | |
engine: { | |
symbologies: ['pdf417'] | |
}, | |
locator: { | |
regionOfInterest: { | |
// PDF417 is typically a wide rectangle, size the ROI appropriately | |
left: 0.05, right: 0.05, top: 0.3, bottom: 0.3 | |
} | |
}, | |
frameSource: { | |
// high resolution recommended for PDF417 | |
resolution: 'full-hd' | |
}, | |
}; | |
return new BarcodeReader(configuration).initialize() | |
.then(barcodeReader => { | |
// store the BarcodeReader in a global, to be able to access it later (e.g. to destroy it) | |
window['barcodeReader'] = barcodeReader; | |
barcodeReader.detected = (detections) => { | |
processPDF417Barcode(detections[0]); | |
}; | |
return barcodeReader.start(); | |
}); | |
} | |
// initialize STRICH SDK with a valid license key | |
const licenseKey = '<your license key here>'; | |
StrichSDK.initialize(licenseKey) | |
.then(() => { | |
console.log('SDK initialized successfully'); | |
return initializeBarcodeReader(); | |
}) | |
.catch(err => { | |
// See: https://docs.strich.io/reference/classes/SdkError.html | |
window.alert(err.localizedMessage); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment