zxing-js/library

repository·master·Indexed 25 days ago

https://github.com/zxing-js/library

A TypeScript port of the ZXing multi-format 1D/2D barcode image processing library for web and mobile applications. It supports a wide range of formats including QR Code, Data Matrix, Aztec, PDF 417, and various 1D industrial and product codes. The library provides tools for decoding barcodes from images, video streams, and URLs, as well as generating QR Code SVGs.

Tokens
3.3K
Snippets
2
Records
28
Agent score
84%

What's inside @zxing/library

  1. Browser compatibility and limitations

    master

    iOS Limitations

    On iOS devices with iOS < 14.3, camera access via WebRTC works only in native Safari and not in other browsers (Chrome, etc.) or apps using UIWebView/WKWebView.

    Web API Requirements

    • The library uses the MediaDevices web API. For older browsers, use a polyfill like WebRTC adapter.
    • The library uses TypedArray (Int32Array, Uint8ClampedArray, etc.). For older browsers (e.g., Android 4 default browser), use core-js.

    BigInt Support

    The PDF 417 decoder uses the BigInt type. While browsers without BigInt support can still use the rest of the library, PDF 417 decoding will not work as there are no viable polyfills for BigInt in this implementation.

  2. Basic usage of MultiFormatReader for decoding

    master

    To decode a barcode, you need to provide a BinaryBitmap (constructed from a LuminanceSource and a Binarizer) to the MultiFormatReader.decode() method. You can also provide hints to restrict the search to specific BarcodeFormat types.

    // use with commonJS
    const { MultiFormatReader, BarcodeFormat } = require('@zxing/library');
    // or with ES6 modules
    import { MultiFormatReader, BarcodeFormat } from '@zxing/library';
    
    const hints = new Map();
    const formats = [BarcodeFormat.QR_CODE, BarcodeFormat.DATA_MATRIX/*, ...*/];
    
    hints.set(DecodeHintType.POSSIBLE_FORMATS, formats);
    
    const reader = new MultiFormatReader();
    
    const luminanceSource = new RGBLuminanceSource(imgByteArray, imgWidth, imgHeight);
    const binaryBitmap = new BinaryBitmap(new HybridBinarizer(luminanceSource));
    
    reader.decode(binaryBitmap, hints);
  3. Configure decoding delay and hints

    master

    You can adjust the performance and behavior of the reader using the following properties:

    • timeBetweenDecodingAttempts: Set the delay (in milliseconds) between consecutive decoding tries. Setting this helps manage CPU usage.
    • hints: A Map<DecodeHintType, any> used to provide additional configuration to the underlying reader (e.g., specifying formats).
  4. Supported barcode formats

    master

    ZXing supports a wide range of 1D and 2D barcode formats:

    1D Products:

    • UPC-A, UPC-E, EAN-8, EAN-13

    1D Industrial:

    • Code 39, Code 93, Code 128, Codabar, ITF

    2D Formats:

    • QR Code, Data Matrix, Aztec, PDF 417, MaxiCode (needs testing!), RSS-14, RSS-Expanded (not production ready!), Micro-QR (needs testing!)
  5. Generate a QR Code SVG element with BrowserQRCodeSvgWriter.write()

    master

    Use the write method to generate an SVGSVGElement containing a QR code. This method returns the SVG element directly, allowing you to manipulate it or append it to the DOM manually.

    Parameters:

    • contents: The string to encode in the QR code.
    • width: The target width for the SVG.
    • height: The target height for the SVG.
    • hints (optional): A Map<EncodeHintType, any> used to configure encoding. Supported keys include:
      • EncodeHintType.ERROR_CORRECTION: Sets the error correction level (e.g., via ErrorCorrectionLevel.fromString()).
      • EncodeHintType.MARGIN: Sets the quiet zone (margin) size as an integer.

    Throws:

    • IllegalArgumentException: If contents is empty or if width/height are negative.
  6. Continuously decode from a video device

    master

    Use decodeFromVideoDevice(deviceId, videoSource, callbackFn) to start a continuous scanning loop. The loop will continue until stopContinuousDecode() is called.

    • deviceId: The ID of the device.
    • videoSource: An element ID or HTMLVideoElement to display the stream.
    • callbackFn: A function of type DecodeContinuouslyCallback that receives the Result or an error.
  7. Use MinimalECIInput for minimal encoding of character strings

    master

    The MinimalECIInput class implements the ECIInput interface and is used to encode a string of characters into a sequence of bytes, ECIs (Extended Channel Identifiers), or FNC1 characters in a way that minimizes the total representation size. This is particularly useful for GS1 data or when multiple charsets are available.

    Constructor Parameters

    • stringToEncode: The character string to encode.
    • priorityCharset: A Charset object representing the preferred encoding. If null, the algorithm automatically selects charsets to achieve the minimal representation. If provided, the algorithm will use this charset for any character it can encode, provided it is among the supported charsets.
    • fnc1: The character code representing the FNC1 character (used in GS1). If this is not GS1 input, use -1.