1 | [<img align="right" src="https://raw.github.com/wiki/zxing/zxing/zxing-logo.png"/>][1]
|
2 |
|
3 | # ZXing
|
4 |
|
5 | ### Runs on your favorite ECMAScript ecosystem
|
6 |
|
7 | > If it doesn't, we gonna make it.
|
8 |
|
9 | ## What is ZXing?
|
10 |
|
11 | > [ZXing][1] ("zebra crossing") is an open-source, multi-format 1D/2D barcode image processing library implemented in Java, with ports to other languages.
|
12 |
|
13 | ## Supported Formats
|
14 |
|
15 | > See [Projects](https://github.com/zxing-js/library/projects) and [Milestones](https://github.com/zxing-js/library/milestones) for what is currently done and what's planned next. 👀
|
16 |
|
17 | | 1D product | 1D industrial | 2D |
|
18 | | ---------- | ------------------- | -------------- |
|
19 | | ~~UPC-A~~ | Code 39 | QR Code |
|
20 | | ~~UPC-E~~ | ~~Code 93~~ | Data Matrix |
|
21 | | EAN-8 | Code 128 | ~~Aztec~~ \* |
|
22 | | EAN-13 | ~~Codabar~~ | ~~PDF 417~~ \* |
|
23 | | | ITF | ~~MaxiCode~~ |
|
24 | | | RSS-14 |
|
25 | | | ~~RSS-Expanded~~ \* |
|
26 |
|
27 | **\*** In progress, may have open PR.
|
28 |
|
29 | ## Status
|
30 |
|
31 | [](https://travis-ci.org/zxing-js/library)
|
32 | 
|
33 | [](https://greenkeeper.io/)
|
34 |
|
35 | [][0]
|
36 | [][0]
|
37 | [](https://github.com/zxing-js/library/graphs/contributors)
|
38 | [](https://github.com/zxing-js/library/compare/master...develop)
|
39 |
|
40 | [](https://www.codacy.com/app/zxing/library?utm_source=github.com&utm_medium=referral&utm_content=zxing-js/library&utm_campaign=Badge_Grade)
|
41 | [](https://codeclimate.com/github/zxing-js/library/maintainability)
|
42 | [](https://codeclimate.com/github/zxing-js/library/test_coverage)
|
43 | [](https://bettercodehub.com/)
|
44 |
|
45 | ## Demo
|
46 |
|
47 | See [Live Preview](https://zxing-js.github.io/library/) in browser.
|
48 |
|
49 | **Note:** All the examples are using ES6, be sure is supported in your browser or modify as needed, Chrome recommended.
|
50 |
|
51 | ## Installation
|
52 |
|
53 | `npm i @zxing/library --save`
|
54 |
|
55 | or
|
56 |
|
57 | `yarn add @zxing/library`
|
58 |
|
59 | ## Usage
|
60 |
|
61 | ### Use on browser with ES6 modules:
|
62 |
|
63 | ```html
|
64 | <script type="module">
|
65 | import { BrowserQRCodeReader } from '@zxing/library';
|
66 |
|
67 | const codeReader = new BrowserQRCodeReader();
|
68 | const img = document.getElementById('img');
|
69 |
|
70 | try {
|
71 | const result = await codeReader.decodeFromImage(img);
|
72 | } catch (err) {
|
73 | console.error(err);
|
74 | }
|
75 |
|
76 | console.log(result);
|
77 | </script>
|
78 | ```
|
79 |
|
80 | #### Or asynchronously:
|
81 |
|
82 | ```html
|
83 | <script type="module">
|
84 | import('@zxing/library').then({ BrowserQRCodeReader } => {
|
85 |
|
86 | const codeReader = new BrowserQRCodeReader();
|
87 | const img = document.getElementById('img');
|
88 |
|
89 | try {
|
90 | const result = await codeReader.decodeFromImage(img);
|
91 | } catch (err) {
|
92 | console.error(err);
|
93 | }
|
94 |
|
95 | console.log(result);
|
96 |
|
97 | });
|
98 | </script>
|
99 | ```
|
100 |
|
101 | ### Use on browser with AMD:
|
102 |
|
103 | ```html
|
104 | <script type="text/javascript" src="https://unpkg.com/requirejs"></script>
|
105 | <script type="text/javascript">
|
106 | require(['@zxing/library'], ZXing => {
|
107 | const codeReader = new ZXing.BrowserQRCodeReader();
|
108 | const img = document.getElementById('img');
|
109 |
|
110 | try {
|
111 | const result = await codeReader.decodeFromImage(img);
|
112 | } catch (err) {
|
113 | console.error(err);
|
114 | }
|
115 |
|
116 | console.log(result);
|
117 | });
|
118 | </script>
|
119 | ```
|
120 |
|
121 | ### Use on browser with UMD:
|
122 |
|
123 | ```html
|
124 | <script type="text/javascript" src="https://unpkg.com/@zxing/library@latest"></script>
|
125 | <script type="text/javascript">
|
126 | window.addEventListener('load', () => {
|
127 | const codeReader = new ZXing.BrowserQRCodeReader();
|
128 | const img = document.getElementById('img');
|
129 |
|
130 | try {
|
131 | const result = await codeReader.decodeFromImage(img);
|
132 | } catch (err) {
|
133 | console.error(err);
|
134 | }
|
135 |
|
136 | console.log(result);
|
137 | });
|
138 | </script>
|
139 | ```
|
140 |
|
141 | ### Use outside the browser with CommonJS:
|
142 |
|
143 | ```javascript
|
144 | const { MultiFormatReader, BarcodeFormat } = require('@zxing/library/esm5'); // use this path since v0.5.1
|
145 |
|
146 | const hints = new Map();
|
147 | const formats = [BarcodeFormat.QR_CODE, BarcodeFormat.DATA_MATRIX/*, ...*/];
|
148 |
|
149 | hints.set(DecodeHintType.POSSIBLE_FORMATS, formats);
|
150 |
|
151 | const reader = new MultiFormatReader();
|
152 |
|
153 | reader.setHints(hints);
|
154 |
|
155 | const luminanceSource = new RGBLuminanceSource(imgWidth, imgHeight, imgByteArray);
|
156 | const binaryBitmap = new BinaryBitmap(new HybridBinarizer(luminanceSource));
|
157 |
|
158 | reader.decode(binaryBitmap);
|
159 | ```
|
160 |
|
161 | ## Browser Support
|
162 |
|
163 | The browser layer is using the [MediaDevices](https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices) web API which is not supported by older browsers.
|
164 |
|
165 | _You can use external polyfills like [WebRTC adapter](https://github.com/webrtc/adapter) to increase browser compatibility._
|
166 |
|
167 | Also, note that the library is using the [`TypedArray`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray) (`Int32Array`, `Uint8ClampedArray`, etc.) which are not available in older browsers (e.g. Android 4 default browser).
|
168 |
|
169 | _You can use [core-js](https://github.com/zloirock/core-js) to add support to these browsers._
|
170 |
|
171 | ### Scanning from Video Camera
|
172 |
|
173 | To display the input from the video camera you will need to add a video element in the HTML page:
|
174 |
|
175 | ```html
|
176 | <video
|
177 | id="video"
|
178 | width="300"
|
179 | height="200"
|
180 | style="border: 1px solid gray"
|
181 | ></video>
|
182 | ```
|
183 |
|
184 | To start decoding, first obtain a list of video input devices with:
|
185 |
|
186 | ```javascript
|
187 | const codeReader = new ZXing.BrowserQRCodeReader();
|
188 |
|
189 | codeReader
|
190 | .listVideoInputDevices()
|
191 | .then(videoInputDevices => {
|
192 | videoInputDevices.forEach(device =>
|
193 | console.log(`${device.label}, ${device.deviceId}`)
|
194 | );
|
195 | })
|
196 | .catch(err => console.error(err));
|
197 | ```
|
198 |
|
199 | 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:
|
200 |
|
201 | ```javascript
|
202 | const firstDeviceId = videoInputDevices[0].deviceId;
|
203 |
|
204 | codeReader
|
205 | .decodeFromInputVideoDevice(firstDeviceId, 'video')
|
206 | .then(result => console.log(result.text))
|
207 | .catch(err => console.error(err));
|
208 | ```
|
209 |
|
210 | If there are more input devices then you will need to chose one for `codeReader.decodeFromInputVideoDevice` device id parameter.
|
211 |
|
212 | 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:
|
213 |
|
214 | ```javascript
|
215 | codeReader
|
216 | .decodeFromInputVideoDevice(undefined, 'video')
|
217 | .then(result => console.log(result.text))
|
218 | .catch(err => console.error(err));
|
219 | ```
|
220 |
|
221 | ### Scanning from Video File
|
222 |
|
223 | Similar as above you can use a video element in the HTML page:
|
224 |
|
225 | ```html
|
226 | <video
|
227 | id="video"
|
228 | width="300"
|
229 | height="200"
|
230 | style="border: 1px solid gray"
|
231 | ></video>
|
232 | ```
|
233 |
|
234 | And to decode the video from an url:
|
235 |
|
236 | ```javascript
|
237 | const codeReader = new ZXing.BrowserQRCodeReader();
|
238 | const videoSrc = 'your url to a video';
|
239 |
|
240 | codeReader
|
241 | .decodeFromVideo('video', videoSrc)
|
242 | .then(result => console.log(result.text))
|
243 | .catch(err => console.error(err));
|
244 | ```
|
245 |
|
246 | You can also decode the video url without showing it in the page, in this case no `video` element is needed in HTML.
|
247 |
|
248 | ```javascript
|
249 | codeReader
|
250 | .decodeFromVideoUrl(videoUrl)
|
251 | .then(result => console.log(result.text))
|
252 | .catch(err => console.error(err));
|
253 |
|
254 | // or alternatively
|
255 |
|
256 | codeReader
|
257 | .decodeFromVideo(null, videoUrl)
|
258 | .then(result => console.log(result.text))
|
259 | .catch(err => console.error(err));
|
260 | ```
|
261 |
|
262 | ### Scanning from Image
|
263 |
|
264 | Similar as above you can use a img element in the HTML page (with src attribute set):
|
265 |
|
266 | ```html
|
267 | <img
|
268 | id="img"
|
269 | src="qrcode-image.png"
|
270 | width="200"
|
271 | height="300"
|
272 | style="border: 1px solid gray"
|
273 | />
|
274 | ```
|
275 |
|
276 | And to decode the image:
|
277 |
|
278 | ```javascript
|
279 | const codeReader = new ZXing.BrowserQRCodeReader();
|
280 | const img = document.getElementById('img');
|
281 |
|
282 | codeReader
|
283 | .decodeFromImage(img)
|
284 | .then(result => console.log(result.text))
|
285 | .catch(err => console.error(err));
|
286 | ```
|
287 |
|
288 | You can also decode the image url without showing it in the page, in this case no `img` element is needed in HTML:
|
289 |
|
290 | ```javascript
|
291 | const imgSrc = 'url to image';
|
292 |
|
293 | codeReader
|
294 | .decodeFromImage(undefined, imgSrc)
|
295 | .then(result => console.log(result.text))
|
296 | .catch(err => console.error(err));
|
297 | ```
|
298 |
|
299 | Or decode the image url directly from an url, with an `img` element in page (notice no `src` attribute is set for `img` element):
|
300 |
|
301 | ```html
|
302 | <img
|
303 | id="img-to-decode"
|
304 | width="200"
|
305 | height="300"
|
306 | style="border: 1px solid gray"
|
307 | />
|
308 | ```
|
309 |
|
310 | ```javascript
|
311 | const imgSrc = 'url to image';
|
312 | const imgDomId = 'img-to-decode';
|
313 |
|
314 | codeReader
|
315 | .decodeFromImage(imgDomId, imgSrc)
|
316 | .then(result => console.log(result.text))
|
317 | .catch(err => console.error(err));
|
318 | ```
|
319 |
|
320 | ## Barcode generation
|
321 |
|
322 | 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:
|
323 |
|
324 | ```html
|
325 | <div id="result"></div>
|
326 | ```
|
327 |
|
328 | And then:
|
329 |
|
330 | ```javascript
|
331 | const codeWriter = new ZXing.BrowserQRCodeSvgWriter();
|
332 | // you can get a SVG element.
|
333 | const svgElement = codeWriter.write(input, 300, 300);
|
334 | // or render it directly to DOM.
|
335 | codeWriter.writeToDom('#result', input, 300, 300);
|
336 | ```
|
337 |
|
338 | ## Contributing
|
339 |
|
340 | See [Contributing Guide](https://github.com/zxing-js/library/blob/master/CONTRIBUTING.md) for information regarding porting approach and reasoning behind some of the approaches taken.
|
341 |
|
342 | ## Contributors
|
343 |
|
344 | Special thanks to all the contributors who have contributed for this project. We heartly thankful to you all.
|
345 |
|
346 | [](https://sourcerer.io/fame/odahcam/zxing-js/library/links/0)[](https://sourcerer.io/fame/odahcam/zxing-js/library/links/1)[](https://sourcerer.io/fame/odahcam/zxing-js/library/links/2)[](https://sourcerer.io/fame/odahcam/zxing-js/library/links/3)[](https://sourcerer.io/fame/odahcam/zxing-js/library/links/4)[](https://sourcerer.io/fame/odahcam/zxing-js/library/links/5)[](https://sourcerer.io/fame/odahcam/zxing-js/library/links/6)[](https://sourcerer.io/fame/odahcam/zxing-js/library/links/7)
|
347 |
|
348 | And a special thanks to @aleris who created the project itself and made the initial QR code port.
|
349 |
|
350 | ---
|
351 |
|
352 | [](http://lunagao.github.io/BlessYourCodeTag/)
|
353 |
|
354 | [0]: https://www.npmjs.com/package/@zxing/library
|
355 | [1]: https://github.com/zxing/zxing
|