mpneuried / zxing-js

Multi-format 1D/2D barcode image processing library, usable in JavaScript ecosystem.

Home Page:https://zxing-js.github.io/library/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ZXing

Runs on your favorite ECMAScript ecosystem

If it doesn't, we gonna make it.

What is ZXing?

ZXing ("zebra crossing") is an open-source, multi-format 1D/2D barcode image processing library implemented in Java, with ports to other languages.

Supported Formats

See Projects and Milestones for what is currently done and what's planned next. đź‘€

1D product 1D industrial 2D
UPC-A Code 39 (no docs) QR Code
UPC-E Code 93 Data Matrix (no docs)
EAN-8 Code 128 (no docs) Aztec (beta)
EAN-13 (no docs) Codabar PDF 417 (in-progress)
ITF (no docs) MaxiCode
RSS-14
RSS-Expanded

Status

Build Status Dependencies

NPM version npm Contributors Commits to deploy

Codacy Badge Maintainability Test Coverage BCH compliance

Demo

See Live Preview in browser.

Note: All the examples are using ES6, be sure is supported in your browser or modify as needed, Chrome recommended.

Usage

Installation

npm i @zxing/library --save

or

yarn add @zxing/library

Environments

Examples below are for QR barcode, all other supported barcodes work similarly.

Browser

To use from JS you need to include what you need from build/umd folder (for example zxing.min.js).

Browser Support

The browser layer is using the MediaDevices web API which is not supported by older browsers.

You can use external polyfills like WebRTC adapter to increase browser compatibility.

Also, note that the library is using the TypedArray (Int32Array, Uint8ClampedArray, etc.) which are not available in older browsers (e.g. Android 4 default browser).

You can use core-js to add support to these browsers.

TypeScript

You can include directly the classes you need, for example:

import { BrowserQRCodeReader, VideoInputDevice } from '@zxing/library';

Node

To use in node you will need to provide an implementation of LuminanceSource for an image. A starting point is SharpImageLuminanceSource from tests that is using sharp image processing Node library.

No examples are availabe for now, however you can have a look at the extensive tests cases.

Scanning from Video Camera

To display the input from the video camera you will need to add a video element in the HTML page:

<video id="video" width="300" height="200" style="border: 1px solid gray"></video>

To start decoding, first obtain a list of video input devices with:

const codeReader = new ZXing.BrowserQRCodeReader();

codeReader.getVideoInputDevices()
    .then(videoInputDevices => {
        videoInputDevices.forEach(
            device => console.log(`${device.label}, ${device.deviceId}`)
        );
    })
    .catch(err => console.error(err));

If there is just one input device you can use the first deviceId and the video element id (in the example below is also 'video') to decode:

const firstDeviceId = videoInputDevices[0].deviceId;

codeReader.decodeFromInputVideoDevice(firstDeviceId, 'video')
    .then(result => console.log(result.text))
    .catch(err => console.error(err));

If there are more input devices then you will need to chose one for codeReader.decodeFromInputVideoDevice device id parameter.

You can also provide undefined for the device id parameter in which case the library will automatically choose the camera, preferring the main (environment facing) camera if more are available:

codeReader.decodeFromInputVideoDevice(undefined, 'video')
    .then(result => console.log(result.text))
    .catch(err => console.error(err));

Scanning from Video File

Similar as above you can use a video element in the HTML page:

<video id="video" width="300" height="200" style="border: 1px solid gray"></video>

And to decode the video from an url:

const codeReader = new ZXing.BrowserQRCodeReader();
const videoSrc = 'your url to a video';

codeReader.decodeFromVideoSource(videoSrc, 'video')
    .then(result => console.log(result.text))
    .catch(err => console.error(err));

You can also decode the video url without showing it in the page, in this case no video element is needed in HTML.

codeReader.decodeFromVideoSource(videoSrc)
    .then(result => console.log(result.text))
    .catch(err => console.error(err));

Scanning from Image

Similar as above you can use a img element in the HTML page (with src attribute set):

<img id="img" src="qrcode-image.png" width="200" height="300" style="border: 1px solid gray">

And to decode the image:

const codeReader = new ZXing.BrowserQRCodeReader();
const img = document.getElementById('img');

codeReader.decodeFromImage(img)
    .then(result => console.log(result.text))
    .catch(err => console.error(err));

You can also decode the image url without showing it in the page, in this case no img element is needed in HTML:

const imgSrc = 'url to image';

codeReader.decodeFromImage(undefined, imgSrc)
    .then(result => console.log(result.text))
    .catch(err => console.error(err));

Or decode the image url directly from an url, with an img element in page (notice no src attribute is set for img element):

<img id="img-to-decode" width="200" height="300" style="border: 1px solid gray">
const imgSrc = 'url to image';
const imgDomId = 'img-to-decode';

codeReader.decodeFromImage(imgDomId, imgSrc)
    .then(result => console.log(result.text))
    .catch(err => console.error(err));

Barcode generation

To generate a QR Code SVG image include 'zxing.qrcodewriter.min.js' from build/vanillajs. You will need to include an element where the SVG element will be appended:

<div id="result"></div>

And then:

const codeWriter = new ZXing.BrowserQRCodeSvgWriter('result');
const svgElement = codeWriter.write(input, 300, 300);

Porting Information

See Contributing Guide for information regarding porting approach and reasoning behind some of the approaches taken.


Bless

About

Multi-format 1D/2D barcode image processing library, usable in JavaScript ecosystem.

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

License:MIT License


Languages

Language:TypeScript 99.1%Language:JavaScript 0.9%