forked from mebjas/html5-qrcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
html5-qrcode.js
1034 lines (953 loc) · 41.4 KB
/
html5-qrcode.js
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @fileoverview
* HTML5 QR code scanning library.
* - Decode QR Code using web cam or smartphone camera
*
* @author mebjas <[email protected]>
*
* The word "QR Code" is registered trademark of DENSO WAVE INCORPORATED
* http://www.denso-wave.com/qrcode/faqpatent-e.html
*
* Note: ECMA Script is not supported by all browsers. Use minified/html5-qrcode.min.js for better
* browser support. Alternatively the transpiled code lives in transpiled/html5-qrcode.js
*/
class Html5Qrcode {
//#region static constants
static DEFAULT_WIDTH = 300;
static DEFAULT_WIDTH_OFFSET = 2;
static FILE_SCAN_MIN_HEIGHT = 300;
static SCAN_DEFAULT_FPS = 2;
static MIN_QR_BOX_SIZE = 50;
static SHADED_LEFT = 1;
static SHADED_RIGHT = 2;
static SHADED_TOP = 3;
static SHADED_BOTTOM = 4;
static SHADED_REGION_CLASSNAME = "qr-shaded-region";
static VERBOSE = false;
static BORDER_SHADER_DEFAULT_COLOR = "#ffffff";
static BORDER_SHADER_MATCH_COLOR = "rgb(90, 193, 56)";
//#endregion
/**
* Initialize QR Code scanner.
*
* @param {String} elementId - Id of the HTML element.
* @param {Boolean} verbose - Optional argument, if true, all logs
* would be printed to console.
*/
constructor(elementId, verbose) {
if (!getLazarSoftScanner) {
throw 'Use html5qrcode.min.js without edit, getLazarSoftScanner'
+ 'not found.';
}
this.qrcode = getLazarSoftScanner();
if (!this.qrcode) {
throw 'qrcode is not defined, use the minified/html5-qrcode.min.js'
+ ' for proper support';
}
this._elementId = elementId;
this._foreverScanTimeout = null;
this._localMediaStream = null;
this._shouldScan = true;
this._url
= window.URL || window.webkitURL || window.mozURL || window.msURL;
this._userMedia
= navigator.getUserMedia || navigator.webkitGetUserMedia
|| navigator.mozGetUserMedia || navigator.msGetUserMedia;
this._isScanning = false;
Html5Qrcode.VERBOSE = verbose === true;
}
/**
* Start scanning QR Code for given camera.
*
* @param {String or Object} identifier of the camera, it can either be the
* cameraId retrieved from {@code Html5Qrcode#getCameras()} method or
* object with facingMode constraint.
* Example values:
* - "a76afe74e95e3aba9fc1b69c39b8701cde2d3e29aa73065c9cd89438627b3bde"
* ^ This is 'deviceId' from camera retrieved from
* {@code Html5Qrcode#getCameras()}
* - { facingMode: "user" }
* - { facingMode: "environment" }
* - { facingMode: { exact: "environment" } }
* - { facingMode: { exact: "user" } }
* - { deviceId: { exact: "a76afe74e95e3....73065c9cd89438627b3bde" }
* - { deviceId: "a76afe74e95e3....73065c9cd89438627b3bde" }
* Reference: https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia#Syntax
* @param {Object} config extra configurations to tune QR code scanner.
* Supported Fields:
* - fps: expected framerate of qr code scanning. example { fps: 2 }
* means the scanning would be done every 500 ms.
* - qrbox: width of QR scanning box, this should be smaller than
* the width and height of the box. This would make the scanner
* look like this:
* ----------------------
* |********************|
* |******,,,,,,,,,*****| <--- shaded region
* |******| |*****| <--- non shaded region would be
* |******| |*****| used for QR code scanning.
* |******|_______|*****|
* |********************|
* |********************|
* ----------------------
* - aspectRatio: Optional, desired aspect ratio for the video feed.
* Ideal aspect ratios are 4:3 or 16:9. Passing very wrong aspect
* ratio could lead to video feed not showing up.
* - disableFlip: Optional, if {@code true} flipped QR Code won't be
* scanned. Only use this if you are sure the camera cannot give
* mirrored feed if you are facing performance constraints.
* - videoConstraints: {MediaTrackConstraints}, Optional
* @beta(this config is not well supported yet).
*
* Important: When passed this will override other parameters
* like 'cameraIdOrConfig' or configurations like 'aspectRatio'.
*
* videoConstraints should be of type {@code MediaTrackConstraints}
* as defined in
* https://developer.mozilla.org/en-US/docs/Web/API/MediaTrackConstraints
* and is used to specify a variety of video or camera controls
* like: aspectRatio, facingMode, frameRate, etc.
* @param {Function} qrCodeSuccessCallback callback on QR Code found.
* Example:
* function(qrCodeMessage) {}
* @param {Function} qrCodeErrorCallback callback on QR Code parse error.
* Example:
* function(errorMessage) {}
*
* @returns Promise for starting the scan. The Promise can fail if the user
* doesn't grant permission or some API is not supported by the browser.
*/
start(cameraIdOrConfig,
configuration,
qrCodeSuccessCallback,
qrCodeErrorCallback) {
if (!cameraIdOrConfig) {
throw "cameraIdOrConfig is required";
}
if (!qrCodeSuccessCallback
|| typeof qrCodeSuccessCallback != "function") {
throw "qrCodeSuccessCallback is required and should be a function."
}
if (!qrCodeErrorCallback) {
qrCodeErrorCallback = console.log;
}
// Cleanup.
this._clearElement();
const $this = this;
// Create configuration by merging default and input settings.
const config = configuration ? configuration : {};
config.fps = config.fps ? config.fps : Html5Qrcode.SCAN_DEFAULT_FPS;
// Check if videoConstraints is passed and valid
let videoConstraintsAvailableAndValid = false;
if (config.videoConstraints) {
if (!this._isMediaStreamConstraintsValid(config.videoConstraints)) {
Html5Qrcode._logError(
"'videoConstraints' is not valid 'MediaStreamConstraints, "
+ "it will be ignored.'",
/* experimental= */ true);
} else {
videoConstraintsAvailableAndValid = true;
}
}
const videoConstraintsEnabled = videoConstraintsAvailableAndValid;
// qr shaded box
const isShadedBoxEnabled = config.qrbox != undefined;
const element = document.getElementById(this._elementId);
const width = element.clientWidth
? element.clientWidth : Html5Qrcode.DEFAULT_WIDTH;
element.style.position = "relative";
this._shouldScan = true;
this._element = element;
this.qrcode.callback = qrCodeSuccessCallback;
// Validate before insertion
if (isShadedBoxEnabled) {
const qrboxSize = config.qrbox;
if (qrboxSize < Html5Qrcode.MIN_QR_BOX_SIZE) {
throw "minimum size of 'config.qrbox' is"
+ ` ${Html5Qrcode.MIN_QR_BOX_SIZE}px.`;
}
if (qrboxSize > width) {
throw "'config.qrbox' should not be greater than the "
+ "width of the HTML element.";
}
}
//#region local methods
/**
* Setups the UI elements, changes the state of this class.
*
* @param width derived width of viewfinder.
* @param height derived height of viewfinder.
*/
const setupUi = (width, height) => {
const qrboxSize = config.qrbox;
if (qrboxSize > height) {
// TODO(mebjas): Migrate to common logging.
console.warn("[Html5Qrcode] config.qrboxsize is greater "
+ "than video height. Shading will be ignored");
}
const shouldShadingBeApplied
= isShadedBoxEnabled && qrboxSize <= height;
const defaultQrRegion = {
x: 0,
y: 0,
width: width,
height: height
};
const qrRegion = shouldShadingBeApplied
? this._getShadedRegionBounds(width, height, qrboxSize)
: defaultQrRegion;
const canvasElement = this._createCanvasElement(
qrRegion.width, qrRegion.height);
const context = canvasElement.getContext('2d');
context.canvas.width = qrRegion.width;
context.canvas.height = qrRegion.height;
// Insert the canvas
element.append(canvasElement);
if (shouldShadingBeApplied) {
this._possiblyInsertShadingElement(element, width, height, qrboxSize);
}
// Update local states
$this._qrRegion = qrRegion;
$this._context = context;
$this._canvasElement = canvasElement;
}
/**
* Scans current context using the qrcode library.
*
* <p>This method call would result in callback being triggered by the
* qrcode library. This method also handles the border coloring.
*
* @returns true if scan match is found, false otherwise.
*/
const scanContext = () => {
try {
$this.qrcode.decode();
this._possiblyUpdateShaders(/* qrMatch= */ true);
return true;
} catch (exception) {
this._possiblyUpdateShaders(/* qrMatch= */ false);
qrCodeErrorCallback(
`QR code parse error, error = ${exception}`);
return false;
}
}
// Method that scans forever.
const foreverScan = () => {
if (!$this._shouldScan) {
// Stop scanning.
return;
}
if ($this._localMediaStream) {
// There is difference in size of rendered video and one that is
// considered by the canvas. Need to account for scaling factor.
const videoElement = $this._videoElement;
const widthRatio
= videoElement.videoWidth / videoElement.clientWidth;
const heightRatio
= videoElement.videoHeight / videoElement.clientHeight;
const sWidthOffset = $this._qrRegion.width * widthRatio;
const sHeightOffset = $this._qrRegion.height * heightRatio;
const sxOffset = $this._qrRegion.x * widthRatio;
const syOffset = $this._qrRegion.y * heightRatio;
// Only decode the relevant area, ignore the shaded area,
// More reference:
// https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/drawImage
$this._context.drawImage(
$this._videoElement,
/* sx= */ sxOffset,
/* sy= */ syOffset,
/* sWidth= */ sWidthOffset,
/* sHeight= */ sHeightOffset,
/* dx= */ 0,
/* dy= */ 0,
/* dWidth= */ $this._qrRegion.width,
/* dHeight= */ $this._qrRegion.height);
// Try scanning normal frame and in case of failure, scan
// the inverted context if not explictly disabled.
// TODO(mebjas): Move this logic to qrcode.js
if (!scanContext() && config.disableFlip !== true) {
// scan inverted context.
this._context.translate(this._context.canvas.width, 0);
this._context.scale(-1, 1);
scanContext();
}
}
$this._foreverScanTimeout = setTimeout(
foreverScan, Html5Qrcode._getTimeoutFps(config.fps));
}
// success callback when user media (Camera) is attached.
const onMediaStreamReceived = mediaStream => {
return new Promise((resolve, reject) => {
const setupVideo = () => {
const videoElement = this._createVideoElement(width);
$this._element.append(videoElement);
// Attach listeners to video.
videoElement.onabort = reject;
videoElement.onerror = reject;
videoElement.onplaying = () => {
const videoWidth = videoElement.clientWidth;
const videoHeight = videoElement.clientHeight;
setupUi(videoWidth, videoHeight);
// start scanning after video feed has started
foreverScan();
resolve();
}
videoElement.srcObject = mediaStream;
videoElement.play();
// Set state
$this._videoElement = videoElement;
}
$this._localMediaStream = mediaStream;
// If videoConstraints is passed, ignore all other configs.
if (videoConstraintsEnabled || !config.aspectRatio) {
setupVideo();
} else {
const constraints = {
aspectRatio : config.aspectRatio
}
const track = mediaStream.getVideoTracks()[0];
track.applyConstraints(constraints)
.then(_ => setupVideo())
.catch(error => {
// TODO(mebjas): Migrate to common logging.
console.log(
"[Warning] [Html5Qrcode] Constriants could not "
+ "be satisfied, ignoring constraints",
error);
setupVideo();
});
}
});
}
//#endregion
return new Promise((resolve, reject) => {
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
// Ignore all other video constraints if the videoConstraints
// is passed.
const videoConstraints = videoConstraintsEnabled
? config.videoConstraints
: $this._createVideoConstraints(cameraIdOrConfig);
navigator.mediaDevices.getUserMedia(
{
audio: false,
video: videoConstraints
}).then(stream => {
onMediaStreamReceived(stream)
.then(_ => {
$this._isScanning = true;
resolve();
})
.catch(reject);
})
.catch(err => {
reject(`Error getting userMedia, error = ${err}`);
});
} else if (navigator.getUserMedia) {
if (typeof cameraIdOrConfig != "string") {
throw "The device doesn't support navigator.mediaDevices"
+ ", only supported cameraIdOrConfig in this case is"
+ " deviceId parameter (string)."
}
const getCameraConfig = {
video: {
optional: [{
sourceId: cameraIdOrConfig
}]
}
};
navigator.getUserMedia(getCameraConfig,
stream => {
onMediaStreamReceived(stream)
.then(_ => {
$this._isScanning = true;
resolve();
})
.catch(reject);
}, err => {
reject(`Error getting userMedia, error = ${err}`);
});
} else {
reject("Web camera streaming not supported by the browser.");
}
});
}
/**
* Stops streaming QR Code video and scanning.
*
* @returns Promise for safely closing the video stream.
*/
stop() {
// TODO(mebjas): fail fast if the start() wasn't called.
this._shouldScan = false;
clearTimeout(this._foreverScanTimeout);
const $this = this;
return new Promise((resolve, /* ignore */ reject) => {
$this.qrcode.callback = null;
const tracksToClose
= $this._localMediaStream.getVideoTracks().length;
var tracksClosed = 0;
// Removes the shaded region if exists.
const removeQrRegion = () => {
while ($this._element.getElementsByClassName(
Html5Qrcode.SHADED_REGION_CLASSNAME).length) {
const shadedChild = $this._element.getElementsByClassName(
Html5Qrcode.SHADED_REGION_CLASSNAME)[0];
$this._element.removeChild(shadedChild);
}
}
const onAllTracksClosed = () => {
$this._localMediaStream = null;
$this._element.removeChild($this._videoElement);
$this._element.removeChild($this._canvasElement);
removeQrRegion();
$this._isScanning = false;
if ($this._qrRegion) {
$this._qrRegion = null;
}
if ($this._context) {
$this._context = null;
}
resolve(true);
}
$this._localMediaStream.getVideoTracks().forEach(videoTrack => {
videoTrack.stop();
++tracksClosed;
if (tracksClosed >= tracksToClose) {
onAllTracksClosed();
}
});
});
}
/**
* Scans an Image File for QR Code.
*
* This feature is mutually exclusive to camera based scanning, you should
* call stop() if the camera based scanning was ongoing.
*
* @param {File} imageFile a local file with Image content.
* @param {boolean} showImage if true the Image will be rendered on given
* element.
*
* @returns Promise with decoded QR code string on success and error message
* on failure. Failure could happen due to different reasons:
* 1. QR Code decode failed because enough patterns not found in
* image.
* 2. Input file was not image or unable to load the image or
* other image load errors.
*/
scanFile(imageFile, /* default=true */ showImage) {
const $this = this;
if (!imageFile || !(imageFile instanceof File)) {
throw "imageFile argument is mandatory and should be instance "
+ "of File. Use 'event.target.files[0]'";
}
showImage = showImage === undefined ? true : showImage;
if ($this._isScanning) {
throw "Close ongoing scan before scanning a file.";
}
const computeCanvasDrawConfig = (
imageWidth,
imageHeight,
containerWidth,
containerHeight) => {
if (imageWidth <= containerWidth
&& imageHeight <= containerHeight) {
// no downsampling needed.
const xoffset = (containerWidth - imageWidth) / 2;
const yoffset = (containerHeight - imageHeight) / 2;
return {
x: xoffset,
y: yoffset,
width: imageWidth,
height: imageHeight
};
} else {
const formerImageWidth = imageWidth;
const formerImageHeight = imageHeight;
if (imageWidth > containerWidth) {
imageHeight = (containerWidth / imageWidth) * imageHeight;
imageWidth = containerWidth;
}
if (imageHeight > containerHeight) {
imageWidth = (containerHeight / imageHeight) * imageWidth;
imageHeight = containerHeight;
}
Html5Qrcode._log(
"Image downsampled from "
+ `${formerImageWidth}X${formerImageHeight}`
+ ` to ${imageWidth}X${imageHeight}.`);
return computeCanvasDrawConfig(
imageWidth, imageHeight, containerWidth, containerHeight);
}
}
return new Promise((resolve, reject) => {
$this._possiblyCloseLastScanImageFile();
$this._clearElement();
$this._lastScanImageFile = imageFile;
const inputImage = new Image;
inputImage.onload = () => {
const imageWidth = inputImage.width;
const imageHeight = inputImage.height;
const element = document.getElementById($this._elementId);
const containerWidth = element.clientWidth
? element.clientWidth : Html5Qrcode.DEFAULT_WIDTH;
// No default height anymore.
const containerHeight = Math.max(
element.clientHeight ? element.clientHeight : imageHeight,
Html5Qrcode.FILE_SCAN_MIN_HEIGHT);
const config = computeCanvasDrawConfig(
imageWidth, imageHeight, containerWidth, containerHeight);
if (showImage) {
const visibleCanvas = $this._createCanvasElement(
containerWidth, containerHeight, 'qr-canvas-visible');
visibleCanvas.style.display = "inline-block";
element.appendChild(visibleCanvas);
const context = visibleCanvas.getContext('2d');
context.canvas.width = containerWidth;
context.canvas.height = containerHeight;
// More reference
// https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/drawImage
context.drawImage(
inputImage,
/* sx= */ 0,
/* sy= */ 0,
/* sWidth= */ imageWidth,
/* sHeight= */ imageHeight,
/* dx= */ config.x,
/* dy= */ config.y,
/* dWidth= */ config.width,
/* dHeight= */ config.height);
}
const hiddenCanvas = $this._createCanvasElement(config.width, config.height);
element.appendChild(hiddenCanvas);
const context = hiddenCanvas.getContext('2d');
context.canvas.width = config.width;
context.canvas.height = config.height;
context.drawImage(
inputImage,
/* sx= */ 0,
/* sy= */ 0,
/* sWidth= */ imageWidth,
/* sHeight= */ imageHeight,
/* dx= */ 0,
/* dy= */ 0,
/* dWidth= */ config.width,
/* dHeight= */ config.height);
try {
resolve($this.qrcode.decode());
} catch (exception) {
reject(`QR code parse error, error = ${exception}`);
}
}
inputImage.onerror = reject;
inputImage.onabort = reject;
inputImage.onstalled = reject;
inputImage.onsuspend = reject;
inputImage.src = URL.createObjectURL(imageFile);
});
}
/**
* Clears the existing canvas.
*
* Note: in case of ongoing web cam based scan, it needs to be explicitly
* closed before calling this method, else it will throw exception.
*/
clear() {
this._clearElement();
}
/**
* Returns a Promise with list of all cameras supported by the device.
*
* The returned object is a list of result object of type:
* [{
* id: String; // Id of the camera.
* label: String; // Human readable name of the camera.
* }]
*/
static getCameras() {
return new Promise((resolve, reject) => {
if (navigator.mediaDevices
&& navigator.mediaDevices.enumerateDevices
&& navigator.mediaDevices.getUserMedia) {
this._log("navigator.mediaDevices used");
navigator.mediaDevices.getUserMedia(
{ audio: false, video: true })
.then(stream => {
// hacky approach to close any active stream if they are
// active.
stream.oninactive
= _ => this._log("All streams closed");
const closeActiveStreams = stream => {
const tracks = stream.getVideoTracks();
for (var i = 0; i < tracks.length; i++) {
const track = tracks[i];
track.enabled = false;
track.stop();
stream.removeTrack(track);
}
}
navigator.mediaDevices.enumerateDevices()
.then(devices => {
const results = [];
for (var i = 0; i < devices.length; i++) {
const device = devices[i];
if (device.kind == "videoinput") {
results.push({
id: device.deviceId,
label: device.label
});
}
}
this._log(`${results.length} results found`);
closeActiveStreams(stream);
resolve(results);
})
.catch(err => {
reject(`${err.name} : ${err.message}`);
});
})
.catch(err => {
reject(`${err.name} : ${err.message}`);
})
} else if (MediaStreamTrack && MediaStreamTrack.getSources) {
this._log("MediaStreamTrack.getSources used");
const callback = sourceInfos => {
const results = [];
for (var i = 0; i !== sourceInfos.length; ++i) {
const sourceInfo = sourceInfos[i];
if (sourceInfo.kind === 'video') {
results.push({
id: sourceInfo.id,
label: sourceInfo.label
});
}
}
this._log(`${results.length} results found`);
resolve(results);
}
MediaStreamTrack.getSources(callback);
} else {
this._log("unable to query supported devices.");
reject("unable to query supported devices.");
}
});
}
/**
* Returns the capabilities of the running video track.
*
* @beta This is an experimental API
* @returns the capabilities of a running video track.
* @throws error if the scanning is not in running state.
*/
getRunningTrackCapabilities() {
if (this._localMediaStream == null) {
throw "Scanning is not in running state, call this API only when"
+ " QR code scanning using camera is in running state.";
}
if (this._localMediaStream.getVideoTracks().length == 0) {
throw "No video tracks found";
}
const videoTrack = this._localMediaStream.getVideoTracks()[0];
return videoTrack.getCapabilities();
}
/**
* Apply a video constraints on running video track from camera.
*
* Important:
* 1. Must be called only if the camera based scanning is in progress.
* 2. Changing aspectRatio while scanner is running is not yet supported.
*
* @beta This is an experimental API
* @param {MediaTrackConstraints} specifies a variety of video or camera
* controls as defined in
* https://developer.mozilla.org/en-US/docs/Web/API/MediaTrackConstraints
* @returns a Promise which succeeds if the passed constraints are applied,
* fails otherwise.
* @throws error if the scanning is not in running state.
*/
applyVideoConstraints(videoConstaints) {
if (!videoConstaints) {
throw "videoConstaints is required argument.";
} else if (!this._isMediaStreamConstraintsValid(videoConstaints)) {
throw "invalid videoConstaints passed, check logs for more details";
}
if (this._localMediaStream == null) {
throw "Scanning is not in running state, call this API only when"
+ " QR code scanning using camera is in running state.";
}
if (this._localMediaStream.getVideoTracks().length == 0) {
throw "No video tracks found";
}
return new Promise((resolve, reject) => {
if ("aspectRatio" in videoConstaints) {
reject("Chaning 'aspectRatio' in run-time is not yet "
+ "supported.");
return;
}
const videoTrack = this._localMediaStream.getVideoTracks()[0];
// TODO(mebjas): This can be simplified to just return the promise
// directly.
videoTrack.applyConstraints(videoConstaints)
.then(_ => {
resolve(_);
})
.catch(error => {
reject(error);
});
});
}
_clearElement() {
if (this._isScanning) {
throw 'Cannot clear while scan is ongoing, close it first.';
}
const element = document.getElementById(this._elementId);
element.innerHTML = "";
}
_createCanvasElement(width, height, customId) {
const canvasWidth = width;
const canvasHeight = height;
const canvasElement = document.createElement('canvas');
canvasElement.style.width = `${canvasWidth}px`;
canvasElement.style.height = `${canvasHeight}px`;
canvasElement.style.display = "none";
// This id is set by lazarsoft/jsqrcode
canvasElement.id = customId == undefined ? 'qr-canvas' : customId;
return canvasElement;
}
_createVideoElement(width) {
const videoElement = document.createElement('video');
videoElement.style.width = `${width}px`;
videoElement.muted = true;
videoElement.playsInline = true;
return videoElement;
}
_getShadedRegionBounds(width, height, qrboxSize) {
if (qrboxSize > width || qrboxSize > height) {
throw "'config.qrbox' should not be greater than the "
+ "width and height of the HTML element.";
}
return {
x: (width - qrboxSize) / 2,
y: (height - qrboxSize) / 2,
width: qrboxSize,
height: qrboxSize
};
}
_possiblyInsertShadingElement(element, width, height, qrboxSize) {
if ((width - qrboxSize) < 1 || (height - qrboxSize) < 1) {
return;
}
const shadingElement = document.createElement('div');
shadingElement.style.position = "absolute";
shadingElement.style.borderLeft = `${(width-qrboxSize)/2}px solid #0000007a`;
shadingElement.style.borderRight = `${(width-qrboxSize)/2}px solid #0000007a`;
shadingElement.style.borderTop = `${(height-qrboxSize)/2}px solid #0000007a`;
shadingElement.style.borderBottom = `${(height-qrboxSize)/2}px solid #0000007a`;
shadingElement.style.boxSizing = "border-box";
shadingElement.style.top = "0px";
shadingElement.style.bottom = "0px";
shadingElement.style.left = "0px";
shadingElement.style.right = "0px";
shadingElement.id = `${Html5Qrcode.SHADED_REGION_CLASSNAME}`;
// Check if div is too small for shadows. As there are two 5px width borders the needs to have a size above 10px.
if ((width - qrboxSize) < 11 || (height - qrboxSize) < 11) {
this.hasBorderShaders = false;
} else {
const smallSize = 5;
const largeSize = 40;
this._insertShaderBorders(shadingElement, largeSize, smallSize, -smallSize, 0, true);
this._insertShaderBorders(shadingElement, largeSize, smallSize, -smallSize, 0, false);
this._insertShaderBorders(shadingElement, largeSize, smallSize, qrboxSize+smallSize, 0, true);
this._insertShaderBorders(shadingElement, largeSize, smallSize, qrboxSize+smallSize, 0, false);
this._insertShaderBorders(shadingElement, smallSize, largeSize+smallSize, -smallSize, -smallSize, true);
this._insertShaderBorders(shadingElement, smallSize, largeSize+smallSize, qrboxSize+smallSize-largeSize, -smallSize, true);
this._insertShaderBorders(shadingElement, smallSize, largeSize+smallSize, -smallSize, -smallSize, false);
this._insertShaderBorders(shadingElement, smallSize, largeSize+smallSize, qrboxSize+smallSize-largeSize, -smallSize, false);
this.hasBorderShaders = true;
}
element.append(shadingElement);
}
_insertShaderBorders(shaderElem, width, height, top, side, isLeft) {
const elem = document.createElement("div");
elem.style.position = "absolute";
elem.style.backgroundColor = Html5Qrcode.BORDER_SHADER_DEFAULT_COLOR;
elem.style.width = `${width}px`;
elem.style.height = `${height}px`;
elem.style.top = `${top}px`;
if (isLeft) {
elem.style.left = `${side}px`;
} else {
elem.style.right = `${side}px`;
}
if (!this.borderShaders) {
this.borderShaders = [];
}
this.borderShaders.push(elem);
shaderElem.appendChild(elem);
}
_possiblyUpdateShaders(qrMatch) {
if (this.qrMatch === qrMatch) {
return;
}
if (this.hasBorderShaders
&& this.borderShaders
&& this.borderShaders.length) {
this.borderShaders.forEach(shader => {
shader.style.backgroundColor = qrMatch
? Html5Qrcode.BORDER_SHADER_MATCH_COLOR
: Html5Qrcode.BORDER_SHADER_DEFAULT_COLOR;
});
}
this.qrMatch = qrMatch;
}
_possiblyCloseLastScanImageFile() {
if (this._lastScanImageFile) {
URL.revokeObjectURL(this._lastScanImageFile);
this._lastScanImageFile = null;
}
}
//#region private method to create correct camera selection filter.
_createVideoConstraints(cameraIdOrConfig) {
if (typeof cameraIdOrConfig == "string") {
// If it's a string it should be camera device Id.
return { deviceId: { exact: cameraIdOrConfig } };
} else if (typeof cameraIdOrConfig == "object") {
const facingModeKey = "facingMode";
const deviceIdKey = "deviceId";
const allowedFacingModeValues
= { "user" : true, "environment" : true};
const exactKey = "exact";
const isValidFacingModeValue = value => {
if (value in allowedFacingModeValues) {
// Valid config
return true;
} else {
// Invalid config
throw "config has invalid 'facingMode' value = "
+ `'${value}'`;
}
};
const keys = Object.keys(cameraIdOrConfig);
if (keys.length != 1) {
throw "'cameraIdOrConfig' object should have exactly 1 key,"
+ ` if passed as an object, found ${keys.length} keys`;
}
const key = Object.keys(cameraIdOrConfig)[0];
if (key != facingModeKey && key != deviceIdKey) {
throw `Only '${facingModeKey}' and '${deviceIdKey}' `
+ " are supported for 'cameraIdOrConfig'";
}
if (key == facingModeKey) {
/**
* Supported scenarios:
* - { facingMode: "user" }
* - { facingMode: "environment" }
* - { facingMode: { exact: "environment" } }
* - { facingMode: { exact: "user" } }
*/
const facingMode = cameraIdOrConfig[key];
if (typeof facingMode == "string") {
if (isValidFacingModeValue(facingMode)) {
return { facingMode: facingMode };
}
} else if (typeof facingMode == "object") {
if (exactKey in facingMode) {
if (isValidFacingModeValue(facingMode[exactKey])) {
return {
facingMode: {
exact: facingMode[exactKey]
}
};
}
} else {
throw "'facingMode' should be string or object with"
+ ` ${exactKey} as key.`;
}
} else {
const type = (typeof facingMode);
throw `Invalid type of 'facingMode' = ${type}`;
}
} else {
/**
* key == deviceIdKey; Supported scenarios:
* - { deviceId: { exact: "a76afe74e95e3.....38627b3bde" }
* - { deviceId: "a76afe74e95e3....065c9cd89438627b3bde" }
*/
const deviceId = cameraIdOrConfig[key];
if (typeof deviceId == "string") {
return { deviceId: deviceId };
} else if (typeof deviceId == "object") {
if (exactKey in deviceId) {
return {
deviceId : { exact: deviceId[exactKey] }
};
} else {
throw "'deviceId' should be string or object with"
+ ` ${exactKey} as key.`;
}
} else {
const type = (typeof deviceId);
throw `Invalid type of 'deviceId' = ${type}`;
}
}
} else {
// invalid type
const type = (typeof cameraIdOrConfig);
throw `Invalid type of 'cameraIdOrConfig' = ${type}`;
}
}
//#endregion
//#region private method to check for valid videoConstraints
_isMediaStreamConstraintsValid(videoConstraints) {
if (!videoConstraints) {
Html5Qrcode._logError(
"Empty videoConstraints", /* experimental= */ true);
return false;
}
if (typeof videoConstraints !== "object") {
const typeofVideoConstraints = typeof videoConstraints;
Html5Qrcode._logError(
"videoConstraints should be of type object, the "
+ `object passed is of type ${typeofVideoConstraints}.`,
/* experimental= */ true);
return false;
}
// TODO(mebjas): Make this validity check more sophisticuated
// Following keys are audio controls, audio controls are not supported.
const bannedKeys = [
"autoGainControl",
"channelCount",
"echoCancellation",
"latency",
"noiseSuppression",
"sampleRate",
"sampleSize",