You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -19,7 +23,7 @@ A Vue.js component, accessing the device camera and allowing users to read QR co
19
23
# Usage
20
24
21
25
### `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.
23
27
24
28
```html
25
29
<qrcode-reader@decode="onDecode"></qrcode-reader>
@@ -32,8 +36,45 @@ methods: {
32
36
}
33
37
```
34
38
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
+
asynconDetect (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
+
} elseif (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
+
```
36
76
77
+
### `init` event
37
78
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.
38
79
39
80
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: {
62
103
} elseif (error.name==='NotReadableError') {
63
104
// maybe camera is already in use
64
105
} elseif (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?
66
108
} else {
67
-
// browser is probably lacking features (WebRTC, Canvas)
109
+
// browser might be lacking features (WebRTC, ...)
68
110
}
69
111
} finally {
70
112
// hide loading indicator
@@ -155,7 +197,6 @@ This component uses [getUserMedia](https://developer.mozilla.org/en-US/docs/Web/
155
197
facingMode: { ideal:'environment' }, // use rear camera if available
0 commit comments