Skip to content

Commit c7918af

Browse files
committed
docs: describe detect event
1 parent cf42788 commit c7918af

File tree

1 file changed

+47
-6
lines changed

1 file changed

+47
-6
lines changed

README.md

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
[![vue2](https://img.shields.io/badge/vue-2.x-brightgreen.svg)](https://vuejs.org/) ![gzip-size](http://img.badgesize.io/gruhn/vue-qrcode-reader/master/dist/vue-qrcode-reader.common.js?compression=gzip) [![npm](https://img.shields.io/npm/v/vue-qrcode-reader.svg) ![npm](https://img.shields.io/npm/dm/vue-qrcode-reader.svg)](https://www.npmjs.com/package/vue-qrcode-reader)
44

5-
A Vue.js component, accessing the device camera and allowing users to read QR codes, within the browser.
5+
A Vue.js 2 component, accessing the device camera and allowing users to read QR codes, within the browser.
6+
7+
* read camera stream and/or drag-and-dropped images
8+
* customizable visual tracking of QR codes
9+
* responsive and layout agnostic
610

711
### [Demos](https://gruhn.github.io/vue-qrcode-reader/)
812

@@ -19,7 +23,7 @@ A Vue.js component, accessing the device camera and allowing users to read QR co
1923
# Usage
2024

2125
### `decode` event
22-
Once a stream from the users camera is loaded, it is displayed and continuously scanned for QR codes. Results are indicated by the `decode` event.
26+
Once a stream from the users camera is loaded, it is displayed and continuously scanned for QR codes. Results are indicated by the `decode` event. This also accounts for decoded images drag-and-dropped in the area the component occupies.
2327

2428
```html
2529
<qrcode-reader @decode="onDecode"></qrcode-reader>
@@ -32,8 +36,45 @@ methods: {
3236
}
3337
```
3438

35-
### `init` event
39+
### `detect` event
40+
The `detect` event is quite similar to `decode` but it provides more details. `decode` only gives you the string encoded by QR codes. `detect` additionally
41+
42+
* tells you where results came from (i.e. a camera stream, a drag-and-dropped file or url)
43+
* gives you the unprocessed raw image data
44+
* the coordinates of the QR code in the image/camera frame
45+
46+
In case of errors `decode` also silently fails. For example when a non-image file is drag-and-dropped.
47+
48+
```html
49+
<qrcode-reader @detect="onDetect"></qrcode-reader>
50+
```
51+
```javascript
52+
methods: {
53+
async onDetect (promise) {
54+
try {
55+
const {
56+
source, // 'file', 'url' or 'stream'
57+
imageData, // raw image data of image/frame
58+
content, // decoded String
59+
location // QR code coordinates
60+
} = await promise
61+
62+
// ...
63+
} catch (error) {
64+
if (error.name === 'DropImageFetchError') {
65+
// drag-and-dropped URL (probably just an <img> element) from different
66+
// domain without CORS header caused same-origin-policy violation
67+
} else if (error.name === 'DropImageDecodeError') {
68+
// drag-and-dropped file is not of type image and can't be decoded
69+
} else {
70+
// idk, open an issue ¯\_(ツ)_/¯
71+
}
72+
}
73+
}
74+
}
75+
```
3676

77+
### `init` event
3778
It might take a while before the component is ready and the scanning process starts. The user has to be asked for camera access permission first and the camera stream has to be loaded.
3879

3980
If you want to show a loading indicator, you can listen for the `init` event. It's emitted as soon as the component is mounted and carries a promise which resolves when everything is ready. The promise is rejected if initialization fails. This can have [a couple of reasons](https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia#Exceptions).
@@ -62,9 +103,10 @@ methods: {
62103
} else if (error.name === 'NotReadableError') {
63104
// maybe camera is already in use
64105
} else if (error.name === 'OverconstrainedError') {
65-
// passed constraints don't match any camera. Did you requested the front camera although there is none?
106+
// passed constraints don't match any camera.
107+
// Did you requested the front camera although there is none?
66108
} else {
67-
// browser is probably lacking features (WebRTC, Canvas)
109+
// browser might be lacking features (WebRTC, ...)
68110
}
69111
} finally {
70112
// hide loading indicator
@@ -155,7 +197,6 @@ This component uses [getUserMedia](https://developer.mozilla.org/en-US/docs/Web/
155197
facingMode: { ideal: 'environment' }, // use rear camera if available
156198
width: { min: 360, ideal: 680, max: 1920 }, // constrain video width resolution
157199
height: { min: 240, ideal: 480, max: 1080 }, // constrain video height resolution
158-
frameRate: { min: 10, ideal: 25 }
159200
}
160201
}
161202
```

0 commit comments

Comments
 (0)