UNPKG

5.34 kBMarkdownView Raw
1# jsQR
2
3[![Build Status](https://travis-ci.org/cozmo/jsQR.svg?branch=master)](https://travis-ci.org/cozmo/jsQR)
4
5A pure javascript QR code reading library.
6This library takes in raw images and will locate, extract and parse any QR code found within.
7
8[Demo](https://cozmo.github.io/jsQR)
9
10
11## Installation
12
13
14
15### NPM
16Available [on npm](https://www.npmjs.com/package/jsqr). Can be used in a Node.js program or with a module bundler such as Webpack or Browserify.
17
18```
19npm install jsqr --save
20```
21
22```javascript
23// ES6 import
24import jsQR from "jsqr";
25
26// CommonJS require
27const jsQR = require("jsqr");
28
29jsQR(...);
30```
31
32### Browser
33Alternatively for frontend use [`jsQR.js`](./dist/jsQR.js) can be included with a script tag
34
35```html
36<script src="jsQR.js"></script>
37<script>
38 jsQR(...);
39</script>
40```
41
42### A note on webcams
43jsQR is designed to be a completely standalone library for scanning QR codes. By design it does not include any platform specific code. This allows it to just as easily scan a frontend webcam stream, a user uploaded image, or be used as part of a backend Node.js process.
44
45If you want to use jsQR to scan a webcam stream you'll need to extract the [`ImageData`](https://developer.mozilla.org/en-US/docs/Web/API/ImageData) from the video stream. This can then be passed to jsQR. The [jsQR demo](https://cozmo.github.io/jsQR) contains a barebones implementation of webcam scanning that can be used as a starting point and customized for your needs. For more advanced questions you can refer to the [`getUserMedia` docs](https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia) or the fairly comprehensive [webRTC sample code](https://github.com/webrtc/samples), both of which are great resources for consuming a webcam stream.
46
47## Usage
48
49jsQR exports a method that takes in 3 arguments representing the image data you wish to decode. Additionally can take an options object to further configure scanning behavior.
50
51```javascript
52const code = jsQR(imageData, width, height, options?);
53
54if (code) {
55 console.log("Found QR code", code);
56}
57```
58
59### Arguments
60- `imageData` - An `Uint8ClampedArray` of RGBA pixel values in the form `[r0, g0, b0, a0, r1, g1, b1, a1, ...]`.
61As such the length of this array should be `4 * width * height`.
62This data is in the same form as the [`ImageData`](https://developer.mozilla.org/en-US/docs/Web/API/ImageData) interface, and it's also [commonly](https://www.npmjs.com/package/jpeg-js#decoding-jpegs) [returned](https://github.com/lukeapage/pngjs/blob/master/README.md#property-data) by node modules for reading images.
63- `width` - The width of the image you wish to decode.
64- `height` - The height of the image you wish to decode.
65- `options` (optional) - Additional options.
66 - `inversionAttempts` - (`attemptBoth` (default), `dontInvert`, `onlyInvert`, or `invertFirst`) - Should jsQR attempt to invert the image to find QR codes with white modules on black backgrounds instead of the black modules on white background. This option defaults to `attemptBoth` for backwards compatibility but causes a ~50% performance hit, and will probably be default to `dontInvert` in future versions.
67
68### Return value
69If a QR is able to be decoded the library will return an object with the following keys.
70
71- `binaryData` - `Uint8ClampedArray` - The raw bytes of the QR code.
72- `data` - The string version of the QR code data.
73- `chunks` - The QR chunks.
74- `version` - The QR version.
75- `location` - An object with keys describing key points of the QR code. Each key is a point of the form `{x: number, y: number}`.
76Has points for the following locations.
77 - Corners - `topRightCorner`/`topLeftCorner`/`bottomRightCorner`/`bottomLeftCorner`;
78 - Finder patterns - `topRightFinderPattern`/`topLeftFinderPattern`/`bottomLeftFinderPattern`
79 - May also have a point for the `bottomRightAlignmentPattern` assuming one exists and can be located.
80
81Because the library is written in [typescript](http://www.typescriptlang.org/) you can also view the [type definitions](./dist/index.d.ts) to understand the API.
82
83## Contributing
84
85jsQR is written using [typescript](http://www.typescriptlang.org/).
86You can view the development source in the [`src`](./src) directory.
87
88Tests can be run with
89
90```
91npm test
92```
93
94Besides unit tests the test suite contains several hundred images that can be found in the [/tests/end-to-end/](./tests/end-to-end/) folder.
95
96Not all the images can be read. In general changes should hope to increase the number of images that read. However due to the nature of computer vision some changes may cause images that pass to start to fail and visa versa. To update the expected outcomes run `npm run-script generate-test-data`. These outcomes can be evaluated in the context of a PR to determine if a change improves or harms the overall ability of the library to read QR codes. A summary of which are passing
97and failing can be found at [/tests/end-to-end/report.json](./tests/end-to-end/report.json)
98
99After testing any changes, you can compile the production version by running
100```
101npm run-script build
102```
103
104- Source hosted at [GitHub](https://github.com/cozmo/jsQR)
105- Report issues, questions, feature requests on [GitHub Issues](https://github.com/cozmo/jsQR/issues)
106
107Pull requests are welcome! Please create seperate branches for seperate features/patches.