Install @zxing/library via npm or yarn
masterTo use the ZXing library in your project, install the @zxing/library package using your preferred package manager.
npm i @zxing/library --save
# or
yarn add @zxing/libraryrepository·master·Indexed 25 days ago
https://github.com/zxing-js/libraryA 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.
To use the ZXing library in your project, install the @zxing/library package using your preferred package manager.
npm i @zxing/library --save
# or
yarn add @zxing/libraryOn 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.
MediaDevices web API. For older browsers, use a polyfill like WebRTC adapter.TypedArray (Int32Array, Uint8ClampedArray, etc.). For older browsers (e.g., Android 4 default browser), use core-js.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.
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);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).ZXing supports a wide range of 1D and 2D barcode formats:
1D Products:
1D Industrial:
2D Formats:
BrowserQRCodeSvgWriter class is deprecated. Developers should migrate to the @zxing/browser package for browser-based QR code generation and related functionality.listVideoInputDevices() to retrieve an array of MediaDeviceInfo objects representing available cameras/video inputs on the user's device.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.To use the BrowserCodeReader, instantiate it with a Reader instance. You can optionally specify the delay between scans in milliseconds and provide decoding hints.
Note: This class is deprecated. It is recommended to move to @zxing/browser.
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.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.
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.decodeFromVideoUrl(url) to decode a barcode from a video file provided via a URL. This method creates an internal video element to process the stream.