├── .gitignore ├── LICENSE ├── README.md ├── barcode-reader.js ├── barcode-worker.js ├── demo └── index.html ├── index.html ├── package.json ├── polymer.json ├── test └── index.html └── yarn.lock /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules/ 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Justin Ribeiro 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![npm version](https://badge.fury.io/js/%40justinribeiro%2Fbarcode-reader.svg)](https://badge.fury.io/js/%40justinribeiro%2Fbarcode-reader) 2 | 3 | # \ 4 | 5 | > A web component that reads barcodes via the Shape Detection API via a Web Worker. 6 | 7 | ![screenshot of barcode-reader](https://user-images.githubusercontent.com/643503/57164618-8765b100-6da9-11e9-9a22-6e6f7fda9c97.png) 8 | 9 | ## Features 10 | 11 | * Uses Shape Detection API available in Chrome M74 (see https://www.chromestatus.com/feature/4757990523535360). 12 | * Module scripts on DedicatedWorker. You can try the feature with '--enable-experimental-web-platform-features' flag (see https://crbug.com/680046) 13 | * Uses [Comlink](https://github.com/GoogleChromeLabs/comlink) for the proxy of the worker 14 | * Built as a web component via [LitElement](https://lit-element.polymer-project.org/) 15 | 16 | ## Experimental 17 | 18 | Please note, this is **not** production ready. It's not polyfilled, it relies on an experimental platform feature for the Web Worker, and generally I've just been toying with it in various Chrome builds for quite some time. 19 | 20 | Use at your own peril! 🐉🔥 21 | 22 | ## A more complete example 23 | 24 | If you're looking for a more complete PWA example of using barcodes on the web, I highly recommend [Paul Kinlans' QR Snapper](https://github.com/PaulKinlan/qrcode) as well as his blog. 25 | 26 | ## Install 27 | 28 | This web component is built with Polymer 3 and ES modules in mind and is 29 | available on NPM: 30 | 31 | Install barcode-reader: 32 | 33 | ```sh 34 | npm i @justinribeiro/barcode-reader 35 | # or 36 | yarn add @justinribeiro/barcode-reader 37 | ``` 38 | 39 | After install, import into your project: 40 | 41 | ```js 42 | import 'barcode-reader'; 43 | ``` 44 | 45 | Finally, use as required: 46 | 47 | ```html 48 | 49 | 69 | ``` 70 | -------------------------------------------------------------------------------- /barcode-reader.js: -------------------------------------------------------------------------------- 1 | import {LitElement, html, css} from 'lit-element'; 2 | import * as Comlink from 'comlink'; 3 | 4 | /** 5 | * `barcode-reader` 6 | * 7 | * 8 | * @polymer 9 | * @extends HTMLElement 10 | * @demo demo/index.html 11 | */ 12 | class BarcodeReader extends LitElement { 13 | static get properties() { 14 | return { 15 | 16 | }; 17 | } 18 | 19 | static get styles() { 20 | return css` 21 | :host { 22 | display: block; 23 | position: relative; 24 | } 25 | 26 | canvas { 27 | display:none; 28 | } 29 | 30 | video { 31 | width: 100%; 32 | height: 100%; 33 | } 34 | 35 | #overlay { 36 | position: absolute; 37 | top: 0; 38 | left: 0; 39 | bottom: 0; 40 | right: 0; 41 | background-color: transparent; 42 | border-style: solid; 43 | border-color: rgba(0, 0, 0, 0.5); 44 | pointer-events: none; 45 | z-index: 20; 46 | border-width: 2em; 47 | } 48 | 49 | #scanline { 50 | position: absolute; 51 | top: 0; 52 | left: 0; 53 | width: 100%; 54 | height: 100%; 55 | visibility: visible; 56 | background: linear-gradient(to bottom, transparent 51%, red 51%, transparent 52%) 57 | } 58 | `; 59 | } 60 | 61 | render() { 62 | return html` 63 |
64 |
65 |
66 | 67 |