Giter Site home page Giter Site logo

aleris / zxing-typescript Goto Github PK

View Code? Open in Web Editor NEW
79.0 8.0 30.0 14.84 MB

Open-source, multi-format 1D/2D barcode image processing library ported from Java in TypeScript usable from node or browser

License: Apache License 2.0

TypeScript 99.75% JavaScript 0.25%
barcode-scanner zxing zxing-typescript barcode-reader barcode-generator qr-code qrcode-generator

zxing-typescript's Introduction

ZXing TypeScript

Build Status

ZXing ("zebra crossing") TypeScript is an open-source, multi-format 1D/2D barcode image processing library ported to TypeScript from Java.

See https://github.com/zxing/zxing for original Java project.

Work In Progress

This project is work in progress, see Status and Roadmap for what is currently done and what's planned next.

Demo

See some demo examples in browser.

Usage

The library can be used from browser with TypeScript (include anything from src/browser however you must do the packaging yourself) or with plain javascript (see below). It can also be used from node (see also below). The library is using separate builds for node and browser to allow different ES targeting.

Browser Usage

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

npm install zxing-typescript --save

To use from javascript you need to build the browser distribution package:

npm run build.browser.dist

And then include what you need from build-browser folder (for example zxing.qrcodereader.min.js for qr barcode reader).

Or just grab the minified files that are available in examples.

See some demo examples for browser code examples with javascript.

All the examples are using es6, be sure is supported in your browser or modify as needed (eg. var instead of const etc.).

The builded library itself is targeting es5 (see .babelrc). If you want to target es6 change to "presets": ["es2016"] and add babel-preset-es2016 to dependencies (but be aware that webpack uglify does not yet support es6 as of this writing).

The browser library is using the MediaDevices web api which is marked as experimental as of this writing. You can use external polyfills like webrtc-adapter to increase browser compatiblity.

Also, note that the library is using the TypedArray (Int32Array, Uint8ClampedArray, etc.) which are not available in older browsers (for example Android 4 default browser, etc.). You can use a polyfill library like core-js to support these browsers.

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((element) => {
            console.log(`${element.label}, ${element.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, prefering the main (environment facing) camera if more are available:

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

A full working example for QR Code from Video Camera is provided in the examples.

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)
    })

A full working example for QR Code from Video File is provided in the examples.

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"></img>

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" width="200" height="300" style="border: 1px solid gray"></img>
const imgSrc = 'url to image'
codeReader.decodeFromImage('img', imgSrc)//here img is the image id from html, in our case 'img'
    .then((result) => {
        console.log(result.text)
    }).catch((err) => {
        console.error(err)
    })

A full working example for QR Code from Image is provided in the examples.

Barcode generation

To generate a QR Code SVG image include 'zxing.qrcodewriter.min.js' from build-browser. 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')
svgElement = codeWriter.write(input, 300, 300)

A full working example for QR Code write to SVG is provided in the examples.

Using from TypeScript

Install the package:

npm install zxing-typescript --save

And then include directly .ts files you need, for example:

import { BrowserQRCodeReader, VideoInputDevice } from 'zxing-typescript/src/browser/BrowserQRCodeReader'

The usage is identical with the above.

Node Usage

npm install zxing-typescript --save

If you want to use plain js (build to es5, see tsconfig.js):

npm run build.node

And the files will be available in build-node folder.

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.

Text Encoding and Decoding

To decode a barcode, the library needs at some point to decode from bits to text. Also, to generate a barcode it needs to encode text to bits. Unfortunately, the state of encoding and decoding text in javascript/browser is somehow messy at the moment.

To have full support for all encodings in CharacterSetECI except Cp437 use text-encoding library. The library is used implicitly for node (and tests), but is an optional dependency for browser because is rather large (> 600k). You will need to include it yourself if you want/need to use it.

By default, in browser, TextDecoder/TextEncoder web api are used if available (take care as these are labeled as experimental as of this writing). Also, be aware that TextEncoder encodes only to UTF-8 as per spec. If these are not available the library falls back to a minimal implementation that only encodes and decodes to/from UTF-8 (see StringEncoding).

Porting Information

See TypeScript Port Info for information regarding porting approach and reasoning behind some of the approaches taken.

Status and Roadmap

See Master Plan Project.

zxing-typescript's People

Contributors

aleris avatar werthdavid avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

zxing-typescript's Issues

TypeScript issues

I use zxing-typescript in a Typescript project with some strict compiler options

{
	"compilerOptions": {
		"noImplicitAny": true,
		"strictNullChecks": true,
		"noUnusedLocals": true,
		"noUnusedParameters": true,
		"allowUnreachableCode": false,
...
}

and I can't compile ;)

Are you open to PR to enforce stricter typescript rules ?

datamatrix support?

Hi there,

I have been trying to scan a datamatrix format from an image, but i get
e {type: "NotFoundException", message: undefined}
Any idea why? isn't this supported?

Not working with IE 11

The problem seems to be that IE 11 doesn't support Uint8ClampedArray, which is needed in GlobalHistogramBinarizer.ts (and others).

Cannot switch to back camera on Chrome/Android

Might be a Chrome issue, I am using Chrome 78.xxx.
Your demo page works with the front camera but switching to back camera has no effect.

I was able to reproduce this on Galaxy Tab S3 and S10e.

Exact DeviceID not working

The method BrowserCodeReader.decodeFromInputVideoDevice() accepts a device-id as input-parameter. Unfortunately this seems to be ignored (for some Browsers only? Safari iOS 11 is the only device I have for testing that has multiple cameras).

Only works with example Image

Hi there,
I finally successfully installed with angular 4 and typescript. but its only decode the QR code you have provided in example.

Its not working with any other QR code.

I have tried following images.

download
2.
code128
3.
22810301_813199782175429_385919422_o
4.
dd

It only works with.
qrcode-image

Here is my code.

onImageSelect(e) {
    const codeReader = new BrowserQRCodeReader();
    const myReader = new FileReader();

    myReader.onload = (event) => {
      codeReader.decodeFromImage(undefined, event.target['result'])
        .then((result) => {
          console.log(result['text']);
        }).catch((err) => {
        console.log(err);
      });
    };

    myReader.readAsDataURL(e.target.files[0]);
  }

Here is the error log
Exception {type: "NotFoundException", message: undefined}

is there anything wrong I am doing?

pdf417 type barcode reading

Hi,

I am working on scanning boarding pass barcode which is in format of pdf417 POC developed using Javascript/typescript web application. I need to run my application in browser and scan boarding pass to read passenger details.

Please let me of this library support pdf417 barcode reading, i have tried example of this library but seems its not working for pdf417 type bar code.

Please advise.

Passing image path to .decodeFromImage throwing error

With reference to the example of reading qr code from image

I tried to pass the sample qr code image path to
const codeReader = new ZXing.BrowserQRCodeReader() codeReader.decodeFromImage('./assets/images/sample.png').then((result) => { console.log(result) // document.getElementById('result').textContent = result.text }).catch((err) => { console.error(err) document.getElementById('result').textContent = err })

But it is throwing below error
message:"element with id './assets/images/sample.png' not found"
type:"ArgumentException"

Rectangle detections on live camera

is it possible to insert a rectangle during the barcode detection phase in live mode? if there is already can you tell me how to implement it? Thanks

not working...

I've downloaded two files

  1. zxing.qrcodereader.min.js
  2. zxing.qrcodewriter.min.js
    from following link
    zxing-typescript/docs/examples/
    and also downloaded "qr-camera" folder in which index.html file exists for scanning qr codes.
    Here I placed all in(local host and as well as at my web server) same sequence as placed it this git repo, After that I opened its index.html from safari and as well as google chrome in Mac Machine, it opens perfectly but when I click at start button it does nothing,
    I also tried in android, google chrome there too it does nothing. I cleared cached and all media licenses and then tried again same issue.
    I am attaching all three browser's snapshots
    Please tell me where I am doing wrong? or if there is some installation issue please guide me in a better way.
    Thanks
    android google chrome webserver
    mac local host google chrome
    mac safari local host
    mac safari webserver

Add compiled TypeScript sources to packaged publishes

Why? Other people can explain better:

You have TS files in your node_modules. This really goes against how libraries should be packaged:
libraries should never ship their source .ts files.

The reason for this rule is that the TypeScript version your app isn't necessarily the same as the TS version your library uses. Different versions of TS can produce different output, and give different errors for the same input. They don't even support the same language features. So packaging libraries with TS sources will always break someone's project.

Source: angular/angular-cli#8284 (comment)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.