This TypeScript utility provides functionality for detecting barcodes from an image file. It works primarily with browser-based applications and leverages the experimental BarcodeDetector API. Please note that this API is not universally supported across all browsers.
$: npm i serial-number-reader
See: https://jsfiddle.net/shaydoc/q1kjnyx9/
$ cd example
$ python3 -m http.server
# navigation to http://localhost:8000import { readImageFile, detectSerialNumbers } from "serial-number-reader";
// example usage with file input
function getFileFromInputEvent(event: Event): File | null {
return (<HTMLInputElement>event.target)?.files?.[0] || null;
}
async function handleFileInputChange(event: Event): Promise<void> {
const file = getFileFromInputEvent(event);
if (!file) {
console.error("No file chosen");
return;
}
try {
const image = await readImageFile(file);
const serialNumbers = await detectSerialNumbers(image);
console.log(serialNumbers);
// do something with image
} catch (error) {
console.error("Error reading file", error);
}
}
const fileInputElement = document.querySelector('input[type="file"]');
fileInputElement.addEventListener("change", handleFileInputChange);This utility includes the following features:
-
Barcode Interface: A TypeScript interface
Barcoderepresenting a barcode with a raw value. -
BarcodeDetector Class: A wrapper around the experimental
BarcodeDetectorAPI, providing adetectmethod that takes anImageBitmapSourceand returns a Promise of an array ofBarcode. -
Image File Reader: An async function
readImageFilethat takes an event (usually from an input of typefile), reads the file as a Blob, creates an Image object, and returns a Promise ofHTMLImageElement. -
Barcode Detection from Image: An async function
detectSerialNumberthat takes anImageBitmapSourceand returns a Promise of an array of serial numbers extracted from the detected barcodes in the image. -
File to Blob Converter: A function
readFileAsBlobthat reads aFileas anArrayBuffer, then creates aBlobwith the file type, and returns a Promise of the Blob. -
Barcode Detection Wrapper: A function
detectBarcodesthat creates a newBarcodeDetectorfor a specified set of barcode formats, detects barcodes in the providedImageBitmapSource, and returns a Promise of an array of detectedBarcodeobjects.
In the detectBarcodes function, the barcodeTypes parameter defaults to ['code_39', 'codabar', 'ean_13'], but you can specify other types if needed. It uses a defined type BarcodeType which allows for these specific values only: 'aztec', 'code_128', 'code_39', 'code_93', 'codabar', 'data_matrix', 'ean_13', 'ean_8', 'itf', 'pdf417', 'qr_code', 'upc_a', 'upc_e'.
const barcodes = await detectBarcodes(image, ["ean_8", "code_128"]);Please note that the Barcode Detector API is experimental and may not be supported on all browsers. Always check for the existence of BarcodeDetector in the global scope before usage.
No external dependencies are required. This utility uses native browser APIs and TypeScript. However, please make sure you have TypeScript installed and configured properly.
Please refer to the official documentation for the most current browser compatibility information. As of the last update, the API is supported on Chrome and Edge (desktop) only.
There is a changelog release process implemented with changesets/cli see release.yml The workflow template is based off of changesets/action
If you encounter any issues or want to contribute, feel free to open an issue or a pull request.
Be sure to make a PR, and add a changeset using pnpm changeset
This project is open source and available under the MIT License.

