@@ -19,7 +19,6 @@ import Camera from '../misc/Camera.js'
19
19
import isBoolean from ' lodash/isBoolean'
20
20
21
21
const NO_LOCATION = [] // use specific array instance to guarantee equality ([] !== [] but NO_LOCATION === NO_LOCATION)
22
- const SCAN_INTERVAL = 40 // milliseconds
23
22
24
23
export default {
25
24
props: {
@@ -36,15 +35,7 @@ export default {
36
35
37
36
data () {
38
37
return {
39
- // most recent decoded QR code content.
40
- // Is only null after unpause or before init, otherwise string.
41
- decodeResult: null ,
42
-
43
- // array of most recent detected QR code corner coordinates
44
- locateResult: NO_LOCATION ,
45
-
46
38
camera: null ,
47
-
48
39
destroyed: false ,
49
40
}
50
41
},
@@ -67,10 +58,8 @@ export default {
67
58
} else {
68
59
withDefaults = {
69
60
facingMode: { ideal: ' environment' },
70
- // width: { min: 360, ideal: 1280, max: 1920 },
71
- // height: { min: 240, ideal: 720, max: 1080 },
72
- width: { ideal: 360 },
73
- height: { ideal: 240 },
61
+ width: { min: 360 , ideal: 680 , max: 1920 },
62
+ height: { min: 240 , ideal: 480 , max: 1080 },
74
63
... this .videoConstraints ,
75
64
}
76
65
}
@@ -84,32 +73,6 @@ export default {
84
73
},
85
74
86
75
watch: {
87
- /**
88
- * Propagate new decoding results to parent component. Since holding a
89
- * camera for a few seconds over a QR code, produces the same result
90
- * multiple times in a row, an event is only emitted when the result
91
- * value actually changes. To allow re-scanning after the component is
92
- * un-paused, decodeResult is set to null (see #8). Null values are never
93
- * emitted though.
94
- */
95
- decodeResult (newValue ) {
96
- if (newValue !== null ) {
97
- this .$emit (' decode' , newValue)
98
- }
99
- },
100
-
101
- /**
102
- * Propagates location of QR code when it changes. Just like with decoding
103
- * results, this event is only emitted when the detected location changes.
104
- *
105
- * While a QR code is actually in sight, position changes are detected
106
- * nearly each scanned frame anyway. While no QR code is detected though,
107
- * this makes sure that empty results are only emitted once.
108
- */
109
- locateResult (newValue ) {
110
- this .$emit (' locate' , newValue)
111
- },
112
-
113
76
/**
114
77
* Automatically freezes the video stream when conditions for the scanning
115
78
* process are not fullfilled anymore.
@@ -121,7 +84,7 @@ export default {
121
84
shouldScan (shouldScan ) {
122
85
if (shouldScan) {
123
86
this .$refs .video .play ()
124
- this .keepScanning ()
87
+ this .startScanning ()
125
88
} else {
126
89
this .$refs .video .pause ()
127
90
}
@@ -170,34 +133,30 @@ export default {
170
133
this .camera = await Camera (this .constraints , this .$refs .video )
171
134
},
172
135
173
- /**
174
- * Continuously extracts frames from camera stream and tries to decode
175
- * potentially pictured QR codes.
176
- */
177
- keepScanning () {
178
- if (this .shouldScan ) {
179
- const imageData = this .camera .captureFrame ()
180
-
181
- window .requestAnimationFrame (() => {
182
- const result = Scanner .scan (imageData)
183
-
184
- if (result !== null ) {
185
- this .decodeResult = result .data
186
-
187
- const locationArray = [
188
- result .location .topLeftCorner ,
189
- result .location .topRightCorner ,
190
- result .location .bottomRightCorner ,
191
- result .location .bottomLeftCorner ,
192
- ]
193
-
194
- this .locateResult = this .normalizeLocation (locationArray)
195
- } else {
196
- this .locateResult = NO_LOCATION
197
- }
198
-
199
- window .setTimeout (this .keepScanning , SCAN_INTERVAL )
200
- })
136
+ startScanning () {
137
+ Scanner .keepScanning (this .camera , {
138
+ decodeHandler: this .onDecode ,
139
+ locateHandler: this .onLocate ,
140
+ shouldContinue : () => this .shouldScan ,
141
+ })
142
+ },
143
+
144
+ onDecode (content ) {
145
+ this .$emit (' decode' , content)
146
+ },
147
+
148
+ onLocate (location ) {
149
+ if (location === null ) {
150
+ this .$emit (' locate' , NO_LOCATION )
151
+ } else {
152
+ const locationArray = this .normalizeLocation ([
153
+ location .topLeftCorner ,
154
+ location .topRightCorner ,
155
+ location .bottomRightCorner ,
156
+ location .bottomLeftCorner ,
157
+ ])
158
+
159
+ this .$emit (' locate' , locationArray)
201
160
}
202
161
},
203
162
@@ -207,10 +166,8 @@ export default {
207
166
* the coordinates are re-calculated to be relative to the video element.
208
167
*/
209
168
normalizeLocation (locationArray ) {
210
- const video = this .$refs .video
211
-
212
- const widthRatio = video .offsetWidth / video .videoWidth
213
- const heightRatio = video .offsetHeight / video .videoHeight
169
+ const widthRatio = this .camera .displayWidth / this .camera .resolutionWidth
170
+ const heightRatio = this .camera .displayHeight / this .camera .resolutionHeight
214
171
215
172
return locationArray .map (({ x, y }) => ({
216
173
x: x * widthRatio,
0 commit comments