npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

serial-number-reader

v0.1.3

Published

A TypeScript utility for detecting serial numbers from barcodes using the Barcode Detection Web API

Downloads

7

Readme

Serial Number Reader Utility

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.

Install

$: npm i serial-number-reader

Demo

$ cd example
$ python3 -m http.server

# navigation to http://localhost:8000

Usage TLDR

import { 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);

Features

This utility includes the following features:

  1. Barcode Interface: A TypeScript interface Barcode representing a barcode with a raw value.

  2. BarcodeDetector Class: A wrapper around the experimental BarcodeDetector API, providing a detect method that takes an ImageBitmapSource and returns a Promise of an array of Barcode.

  3. Image File Reader: An async function readImageFile that takes an event (usually from an input of type file), reads the file as a Blob, creates an Image object, and returns a Promise of HTMLImageElement.

  4. Barcode Detection from Image: An async function detectSerialNumber that takes an ImageBitmapSource and returns a Promise of an array of serial numbers extracted from the detected barcodes in the image.

  5. File to Blob Converter: A function readFileAsBlob that reads a File as an ArrayBuffer, then creates a Blob with the file type, and returns a Promise of the Blob.

  6. Barcode Detection Wrapper: A function detectBarcodes that creates a new BarcodeDetector for a specified set of barcode formats, detects barcodes in the provided ImageBitmapSource, and returns a Promise of an array of detected Barcode objects.

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.

Dependencies

No external dependencies are required. This utility uses native browser APIs and TypeScript. However, please make sure you have TypeScript installed and configured properly.

Browser Compatibility

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.

Contributions

If you encounter any issues or want to contribute, feel free to open an issue or a pull request.

License

This project is open source and available under the MIT License.