-
-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy pathbrowser-code-reader.ts
460 lines (379 loc) · 12 KB
/
browser-code-reader.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/// <reference path="./image-capture.d.ts" />
import {
BinaryBitmap,
ChecksumException,
Exception,
FormatException,
HTMLCanvasElementLuminanceSource,
HybridBinarizer,
NotFoundException,
Reader,
Result,
} from '@zxing/library';
import {
BehaviorSubject,
Observable,
Subscriber,
Subscription
} from 'rxjs';
import { catchError } from 'rxjs/operators';
/**
* Based on zxing-typescript BrowserCodeReader
*/
export class BrowserCodeReader {
/**
* The HTML video element, used to display the camera stream.
*/
private videoElement: HTMLVideoElement;
/**
* Should contain the current registered listener for video play-ended,
* used to unregister that listener when needed.
*/
private videoPlayEndedEventListener: EventListener;
/**
* Should contain the current registered listener for video playing,
* used to unregister that listener when needed.
*/
private videoPlayingEventListener: EventListener;
/**
* Should contain the current registered listener for video loaded-metadata,
* used to unregister that listener when needed.
*/
private videoLoadedMetadataEventListener: EventListener;
/**
* The HTML image element, used as a fallback for the video element when decoding.
*/
private imageElement: HTMLImageElement;
/**
* Should contain the current registered listener for image loading,
* used to unregister that listener when needed.
*/
private imageLoadedEventListener: EventListener;
/**
* The HTML canvas element, used to draw the video or image's frame for decoding.
*/
private canvasElement: HTMLCanvasElement;
/**
* The HTML canvas element context.
*/
private canvasElementContext: CanvasRenderingContext2D;
/**
* Used to control the decoding stream when it's open.
*/
private decodingStream: Subscription;
/**
* The stream output from camera.
*/
private stream: MediaStream;
/**
* The track from camera.
*/
private track: MediaStreamTrack;
/**
* Shows if torch is available on the camera.
*/
private torchCompatible = new BehaviorSubject<boolean>(false);
/**
* The device id of the current media device.
*/
private deviceId: string;
/**
* Constructor for dependency injection.
*
* @param reader The barcode reader to be used to decode the stream.
* @param timeBetweenScans The scan throttling in milliseconds.
*/
public constructor(protected readonly reader: Reader, private timeBetweenScans: number = 500) { }
/**
* Starts the decoding from the current or a new video element.
*
* @param callbackFn The callback to be executed after every scan attempt
* @param deviceId The device's to be used Id
* @param videoElement A new video element
*
* @todo Return Promise<Result>
*/
public async decodeFromInputVideoDevice(
callbackFn?: (result: Result) => any,
deviceId?: string,
videoElement?: HTMLVideoElement
): Promise<void> {
this.reset();
this.prepareVideoElement(videoElement);
// Keeps the deviceId between scanner resets.
if (typeof deviceId !== 'undefined') {
this.deviceId = deviceId;
}
const video = typeof deviceId === 'undefined'
? { facingMode: { exact: 'environment' } }
: { deviceId: { exact: deviceId } };
const constraints: MediaStreamConstraints = {
audio: false,
video
};
if (typeof navigator === 'undefined') {
return;
}
try {
const stream = await navigator
.mediaDevices
.getUserMedia(constraints);
this.startDecodeFromStream(stream, callbackFn);
} catch (err) {
/* handle the error, or not */
console.error(err);
}
}
/**
* Sets the new stream and request a new decoding-with-delay.
*
* @param stream The stream to be shown in the video element.
* @param callbackFn A callback for the decode method.
*
* @todo Return Promise<Result>
*/
private startDecodeFromStream(stream: MediaStream, callbackFn?: (result: Result) => any): void {
this.stream = stream;
this.checkTorchCompatibility(this.stream);
this.bindVideoSrc(this.videoElement, this.stream);
this.bindEvents(this.videoElement, callbackFn);
}
/**
* Defines what the videoElement src will be.
*
* @param videoElement
* @param stream
*/
public bindVideoSrc(videoElement: HTMLVideoElement, stream: MediaStream): void {
// Older browsers may not have `srcObject`
try {
// @NOTE Throws Exception if interrupted by a new loaded request
videoElement.srcObject = stream;
} catch (err) {
// @NOTE Avoid using this in new browsers, as it is going away.
videoElement.src = window.URL.createObjectURL(stream);
}
}
/**
* Unbinds a HTML video src property.
*
* @param videoElement
*/
public unbindVideoSrc(videoElement: HTMLVideoElement): void {
try {
videoElement.srcObject = null;
} catch (err) {
videoElement.src = '';
}
}
/**
* Binds listeners and callbacks to the videoElement.
*
* @param videoElement
* @param callbackFn
*/
private bindEvents(videoElement: HTMLVideoElement, callbackFn?: (result: Result) => any): void {
if (typeof callbackFn !== 'undefined') {
this.videoPlayingEventListener = () => this.decodingStream = this.decodeWithDelay(this.timeBetweenScans)
.pipe(catchError((e, x) => this.handleDecodeStreamError(e, x)))
.subscribe((x: Result) => callbackFn(x));
}
videoElement.addEventListener('playing', this.videoPlayingEventListener);
this.videoLoadedMetadataEventListener = () => videoElement.play();
videoElement.addEventListener('loadedmetadata', this.videoLoadedMetadataEventListener);
}
/**
* Checks if the stream supports torch control.
*
* @param stream The media stream used to check.
*/
private async checkTorchCompatibility(stream: MediaStream): Promise<void> {
try {
this.track = stream.getVideoTracks()[0];
const imageCapture = new ImageCapture(this.track);
const capabilities = await imageCapture.getPhotoCapabilities();
const compatible = !!capabilities.torch || ('fillLightMode' in capabilities && capabilities.fillLightMode.length !== 0);
this.torchCompatible.next(compatible);
} catch (err) {
this.torchCompatible.next(false);
}
}
/**
* Enables and disables the device torch.
*/
public setTorch(on: boolean): void {
if (!this.torchCompatible.value) {
return;
}
if (on) {
this.track.applyConstraints({
advanced: [<any>{ torch: true }]
});
} else {
this.restart();
}
}
/**
* Observable that says if there's a torch available for the current device.
*/
public get torchAvailable(): Observable<boolean> {
return this.torchCompatible.asObservable();
}
/**
* Sets a HTMLVideoElement for scanning or creates a new one.
*
* @param videoElement The HTMLVideoElement to be set.
*/
private prepareVideoElement(videoElement?: HTMLVideoElement): void {
if (!videoElement && typeof document !== 'undefined') {
videoElement = document.createElement('video');
videoElement.width = 200;
videoElement.height = 200;
}
this.videoElement = videoElement;
}
/**
* Opens a decoding stream.
*/
private decodeWithDelay(delay: number = 500): Observable<Result> {
// The decoding stream.
return Observable.create((observer: Subscriber<Result>) => {
// Creates on Subscribe.
const intervalId = setInterval(() => {
try {
observer.next(this.decode());
} catch (err) {
observer.error(err);
}
}, delay);
// Destroys on Unsubscribe.
return () => clearInterval(intervalId);
});
}
/**
* Gets the BinaryBitmap for ya! (and decodes it)
*/
private decode(): Result {
// get binary bitmap for decode function
const binaryBitmap = this.createBinaryBitmap(this.videoElement || this.imageElement);
return this.decodeBitmap(binaryBitmap);
}
/**
* Call the encapsulated readers decode
*/
protected decodeBitmap(binaryBitmap: BinaryBitmap): Result {
return this.reader.decode(binaryBitmap);
}
/**
* Administra um erro gerado durante o decode stream.
*/
private handleDecodeStreamError(err: Exception, caught: Observable<Result>): Observable<Result> {
if (
// scan Failure - found nothing, no error
err instanceof NotFoundException ||
// scan Error - found the QR but got error on decoding
err instanceof ChecksumException ||
err instanceof FormatException
) {
return caught;
}
throw err;
}
/**
* Creates a binaryBitmap based in some image source.
*
* @param mediaElement HTML element containing drawable image source.
*/
private createBinaryBitmap(mediaElement: HTMLVideoElement | HTMLImageElement): BinaryBitmap {
if (undefined === this.canvasElementContext) {
this.prepareCaptureCanvas();
}
this.canvasElementContext.drawImage(mediaElement, 0, 0);
const luminanceSource = new HTMLCanvasElementLuminanceSource(this.canvasElement);
const hybridBinarizer = new HybridBinarizer(luminanceSource);
return new BinaryBitmap(hybridBinarizer);
}
/**
* 🖌 Prepares the canvas for capture and scan frames.
*/
private prepareCaptureCanvas(): void {
if (typeof document === 'undefined') {
this.canvasElement = undefined;
this.canvasElementContext = undefined;
return;
}
const canvasElement = document.createElement('canvas');
let width: number;
let height: number;
if (typeof this.videoElement !== 'undefined') {
width = this.videoElement.videoWidth;
height = this.videoElement.videoHeight;
}
if (typeof this.imageElement !== 'undefined') {
width = this.imageElement.naturalWidth || this.imageElement.width;
height = this.imageElement.naturalHeight || this.imageElement.height;
}
canvasElement.style.width = width + 'px';
canvasElement.style.height = height + 'px';
canvasElement.width = width;
canvasElement.height = height;
this.canvasElement = canvasElement;
this.canvasElementContext = canvasElement.getContext('2d');
}
/**
* Stops the continuous scan and cleans the stream.
*/
private stop(): void {
if (this.decodingStream) {
this.decodingStream.unsubscribe();
}
if (this.stream) {
this.stream.getVideoTracks().forEach(t => t.stop());
this.stream = undefined;
}
}
/**
* Resets the scanner and it's configurations.
*/
public reset(): void {
// stops the camera, preview and scan 🔴
this.stop();
if (this.videoElement) {
// first gives freedon to the element 🕊
if (typeof this.videoPlayEndedEventListener !== 'undefined') {
this.videoElement.removeEventListener('ended', this.videoPlayEndedEventListener);
}
if (typeof this.videoPlayingEventListener !== 'undefined') {
this.videoElement.removeEventListener('playing', this.videoPlayingEventListener);
}
if (typeof this.videoLoadedMetadataEventListener !== 'undefined') {
this.videoElement.removeEventListener('loadedmetadata', this.videoLoadedMetadataEventListener);
}
// then forgets about that element 😢
this.unbindVideoSrc(this.videoElement);
this.videoElement.removeAttribute('src');
this.videoElement = undefined;
}
if (this.imageElement) {
// first gives freedon to the element 🕊
if (undefined !== this.videoPlayEndedEventListener) {
this.imageElement.removeEventListener('load', this.imageLoadedEventListener);
}
// then forgets about that element 😢
this.imageElement.src = undefined;
this.imageElement.removeAttribute('src');
this.imageElement = undefined;
}
// cleans canvas references 🖌
this.canvasElementContext = undefined;
this.canvasElement = undefined;
}
/**
* Restarts the scanner.
*/
private restart(): void {
// reset
// start
this.decodeFromInputVideoDevice(undefined, this.deviceId, this.videoElement);
}
}