undecaf / vue-barcode-scanner

A barcode/QR code scanner for Vue 2
MIT License
8 stars 3 forks source link
barcode-scanner component qrcode-scanner vue vuejs2

A barcode/QR code scanner for Vue 2

Minified size Open issues Vulnerabilities Total downloads License

This Vue component, BarcodeScanner, offers the following features:

Try these features on this online example (source code with build scripts for Rollup, esbuild and Webpack/Vue CLI).

Contents

Installation

As ES module

$ npm install @undecaf/vue-barcode-scanner
    or
$ yarn add @undecaf/vue-barcode-scanner

Then import BarcodeScanner from '@undecaf/vue-barcode-scanner' where required and place as <barcode-scanner> in your template. This CodePen demonstrates the scanner in a Vue SFC.

As plain <script>

<script src="https://cdn.jsdelivr.net/npm/@undecaf/vue-barcode-scanner/dist/index.js"></script>

This exposes the component options object as barcodeScanner.default. This CodePen shows the scanner in a Vue <script>.

Polyfilling BarcodeDetector

BarcodeScanner relies on the Barcode Detection API to do its work. For browsers that do not yet implement this API, a polyfill will be required.

The following snippets use @undecaf/barcode-detector-polyfill (written by the same author as this component) as an example.

Polyfill if necessary in an ES module (also shown in this CodePen):

import { BarcodeDetectorPolyfill } from '@undecaf/barcode-detector-polyfill'

try {
    window['BarcodeDetector'].getSupportedFormats()
} catch {
    window['BarcodeDetector'] = BarcodeDetectorPolyfill
}
    ⁝

In a plain <script> (shown in this CodePen):

<script src="https://cdn.jsdelivr.net/npm/@undecaf/zbar-wasm/dist/index.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@undecaf/barcode-detector-polyfill/dist/index.js"></script>
<script>
    try {
        window['BarcodeDetector'].getSupportedFormats()
    } catch {
        window['BarcodeDetector'] = barcodeDetectorPolyfill.BarcodeDetectorPolyfill
    }
        ⁝
</script>

Usage

Source element

BarcodeScanner needs an image or video source that is to be scanned for barcodes. This can be an <img>, <canvas> or <video> element, or a container or a Vue component having one of these elements as descendant (for other source types, see the source attribute). For example:

<barcode-scanner ...>
  <video ...></video>
</barcode-scanner>

The source element/container must be the only child of <barcode-scanner>. If located inside a container then <img>/<canvas>/<video> must cover that container exactly in order for masks and barcode highlights to appear in correct positions. The source attribute may specify a CSS selector for a particular source element inside the container.

The source element and its src and srcObject attributes are reactive, i.e. changed content is scanned automatically. Video sources are scanned repeatedly while being played. To scan animated <canvas> content, capture it as MediaStream and pass that to the source attribute.

Attributes

All attributes are reactive. Try them in the example project!

Getting results: events

If desired then the names of the events described above can be imported as constants:

import { SCANNED_EVENT, STARTED_EVENT, STOPPED_EVENT, ERROR_EVENT } from 'undecaf/vue-barcode-scanner'

In a plain script, these constants are named barcodeScanner.SCANNED_EVENT etc.

License

Software: MIT

Documentation: CC-BY-SA 4.0