All files / src/lib barcodePickerGui.ts

93.11% Statements 365/392
79.55% Branches 105/132
87.76% Functions 43/49
93.51% Lines 360/385

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 73313x         13x   13x               13x 13x 13x 13x   13x               13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x                                                                       74x                         74x 74x 74x 74x 74x 74x   74x 74x 74x 74x 74x 74x   74x   74x 74x 74x 74x 74x 74x 74x   74x 11x 11x 11x 11x   11x   11x   63x 63x 63x 63x 63x 63x 63x 63x   63x 63x   63x 63x 63x   63x 63x     74x 1x 1x   73x     74x 74x     74x   74x     13x 46x 38x   46x 38x   46x 38x   46x 1x   46x 46x 46x     13x 61x     13x 16x 16x 16x 16x     13x 11x 11x 11x 11x     13x 10x     13x 81x 81x       2x 2x 2x 2x     81x   81x 75x 75x 61x 14x 1x     6x       13x 36x         32x     32x 9x   23x     4x       13x 25x 24x 9x   15x     24x 4x               13x 74x 1x     73x   62x 62x 62x 62x   5x 5x 5x 5x     6x 6x 6x 6x       13x 129x 129x 127x   129x 129x 129x 129x 129x 129x     13x 129x 129x 127x   129x 129x 129x 129x 129x 129x     13x 71x       71x   71x 5x 5x   66x 66x   66x         71x     13x 4x 2x 2x     4x 4x 4x 4x 4x   4x     13x 20x 17x 3x 1x       13x 1x   1x         1x                           13x 5x     13x 19x 17x   2x       13x 23x 1x   22x       13x 22x 22x 20x               13x 6x 3x 3x   3x 3x       13x 63x 63x 63x 63x 63x     13x 11x 11x 11x 11x 11x 11x 11x               11x 11x 11x 11x 11x 11x 11x 11x   11x 11x     13x 63x 63x   63x 63x   63x 63x   63x 63x     63x 63x 63x     13x 17x   17x 17x     13x 1x   1x 1x     13x 157x 16x   141x       13x 16x 16x   16x 16x     13x 141x 132x     9x 9x   9x 9x   9x   9x 3x 3x   3x 1x   2x     3x                 3x     6x 2x   4x     6x 6x   6x     13x 4x                           13x 9x     13x 3x       3x 3x 1x 1x   2x 1x 1x   1x 1x       6x     13x 3x 3x   3x 3x           3x   6x 3x     13x 4x 4x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x       3x     3x 3x       63x 63x 63x 63x 63x 63x 126x                                                   63x 63x 63x 63x 63x 63x 126x                 13x 73x                 73x 73x 73x 73x     13x 65x 64x   65x 64x       13x                                     13x  
import { ResizeObserver as ResizeObserverPolyfill } from "@juggle/resize-observer";
 
// tslint:disable-next-line: variable-name
const ResizeObserver: typeof ResizeObserverPolyfill =
  // tslint:disable-next-line: no-any
  "ResizeObserver" in window ? /* istanbul ignore next */ (<any>window).ResizeObserver : ResizeObserverPolyfill;
 
import {
  cameraImage,
  laserActiveImage,
  laserPausedImage,
  scanditLogoImage,
  switchCameraImage,
  toggleTorchImage
} from "./assets/base64assets";
import { BarcodePicker } from "./barcodePicker";
import { BrowserHelper } from "./browserHelper";
import { Camera } from "./camera";
import { CameraAccess } from "./cameraAccess";
import { CameraManager } from "./cameraManager";
import { ImageSettings } from "./imageSettings";
import { Scanner } from "./scanner";
import { ScanSettings } from "./scanSettings";
import { SearchArea } from "./searchArea";
 
/**
 * @hidden
 */
export class BarcodePickerGui {
  public static readonly grandParentElementClassName: string = "scandit scandit-container";
  public static readonly parentElementClassName: string = "scandit scandit-barcode-picker";
  public static readonly hiddenClassName: string = "scandit-hidden";
  public static readonly hiddenOpacityClassName: string = "scandit-hidden-opacity";
  public static readonly videoElementClassName: string = "scandit-video";
  public static readonly scanditLogoImageElementClassName: string = "scandit-logo";
  public static readonly laserContainerElementClassName: string = "scandit-laser";
  public static readonly viewfinderElementClassName: string = "scandit-viewfinder";
  public static readonly cameraSwitcherElementClassName: string = "scandit-camera-switcher";
  public static readonly torchTogglerElementClassName: string = "scandit-torch-toggle";
  public static readonly cameraUploadElementClassName: string = "scandit-camera-upload";
  public static readonly flashColorClassName: string = "scandit-flash-color";
  public static readonly flashWhiteClassName: string = "scandit-flash-white";
  public static readonly flashWhiteInsetClassName: string = "scandit-flash-white-inset";
  public static readonly opacityPulseClassName: string = "scandit-opacity-pulse";
  public static readonly mirroredClassName: string = "mirrored";
  public static readonly pausedClassName: string = "paused";
 
  public readonly videoElement: HTMLVideoElement;
  public readonly cameraSwitcherElement: HTMLImageElement;
  public readonly torchTogglerElement: HTMLImageElement;
 
  private readonly scanner: Scanner;
  private readonly singleImageMode: boolean;
  private readonly grandParentElement: HTMLDivElement;
  private readonly parentElement: HTMLDivElement;
  private readonly laserContainerElement: HTMLDivElement;
  private readonly laserActiveImageElement: HTMLImageElement;
  private readonly laserPausedImageElement: HTMLImageElement;
  private readonly viewfinderElement: HTMLDivElement;
  private readonly cameraUploadElement: HTMLDivElement;
  private readonly cameraUploadInputElement: HTMLInputElement;
  private readonly cameraUploadLabelElement: HTMLLabelElement;
  private readonly cameraUploadProgressElement: HTMLDivElement;
  private readonly videoImageCanvasContext: CanvasRenderingContext2D;
  private readonly visibilityListener: EventListenerOrEventListenerObject;
  private readonly videoResizeListener: EventListenerOrEventListenerObject;
  private readonly newScanSettingsListener: (scanSettings: ScanSettings) => void;
  private readonly licenseFeaturesReadyListener: (licenseFeatures: object) => void;
  private readonly resizeObserver: ResizeObserverPolyfill;
  private readonly cameraUploadCallback: () => Promise<void>;
  private readonly mirrorImageOverrides: Map<string, boolean>;
 
  private cameraManager?: CameraManager;
  private originElement: HTMLElement;
  private scanningPaused: boolean;
  private visible: boolean;
  private guiStyle: BarcodePicker.GuiStyle;
  private videoFit: BarcodePicker.ObjectFit;
  private customLaserArea?: SearchArea;
  private customViewfinderArea?: SearchArea;
 
  public constructor(options: {
    scanner: Scanner;
    originElement: HTMLElement;
    singleImageMode: boolean;
    scanningPaused: boolean;
    visible: boolean;
    guiStyle: BarcodePicker.GuiStyle;
    videoFit: BarcodePicker.ObjectFit;
    hideLogo: boolean;
    laserArea?: SearchArea;
    viewfinderArea?: SearchArea;
    cameraUploadCallback(): Promise<void>;
  }) {
    this.scanner = options.scanner;
    this.originElement = options.originElement;
    this.singleImageMode = options.singleImageMode;
    this.scanningPaused = options.scanningPaused;
    this.cameraUploadCallback = options.cameraUploadCallback;
    this.mirrorImageOverrides = new Map<string, boolean>();
 
    this.grandParentElement = document.createElement("div");
    this.grandParentElement.className = BarcodePickerGui.grandParentElementClassName;
    this.originElement.appendChild(this.grandParentElement);
    this.parentElement = document.createElement("div");
    this.parentElement.className = BarcodePickerGui.parentElementClassName;
    this.grandParentElement.appendChild(this.parentElement);
 
    this.videoImageCanvasContext = <CanvasRenderingContext2D>document.createElement("canvas").getContext("2d");
 
    this.videoElement = document.createElement("video");
    this.cameraSwitcherElement = document.createElement("img");
    this.torchTogglerElement = document.createElement("img");
    this.laserContainerElement = document.createElement("div");
    this.laserActiveImageElement = document.createElement("img");
    this.laserPausedImageElement = document.createElement("img");
    this.viewfinderElement = document.createElement("div");
 
    if (options.singleImageMode) {
      this.cameraUploadElement = document.createElement("div");
      this.cameraUploadInputElement = document.createElement("input");
      this.cameraUploadLabelElement = document.createElement("label");
      this.cameraUploadProgressElement = document.createElement("div");
 
      this.setupCameraUploadGuiAssets();
 
      this.guiStyle = BarcodePicker.GuiStyle.NONE;
    } else {
      this.setupVideoElement();
      this.setupCameraSwitcher();
      this.setupTorchToggler();
      this.setupFullGuiAssets();
      this.setGuiStyle(options.guiStyle);
      this.setVideoFit(options.videoFit);
      this.setLaserArea(options.laserArea);
      this.setViewfinderArea(options.viewfinderArea);
 
      this.visibilityListener = this.checkAndRecoverPlayback.bind(this);
      document.addEventListener("visibilitychange", this.visibilityListener);
 
      this.newScanSettingsListener = this.handleNewScanSettings.bind(this);
      this.scanner.onNewScanSettings(this.newScanSettingsListener);
      this.handleNewScanSettings();
 
      this.videoResizeListener = this.handleVideoResize.bind(this);
      this.videoElement.addEventListener("resize", this.videoResizeListener);
    }
 
    if (options.hideLogo) {
      this.licenseFeaturesReadyListener = this.showScanditLogo.bind(this, options.hideLogo);
      this.scanner.onLicenseFeaturesReady(this.licenseFeaturesReadyListener);
    } else {
      this.showScanditLogo(false);
    }
 
    this.resize();
    this.resizeObserver = new ResizeObserver(() => {
      this.resize();
    });
    this.resizeObserver.observe(this.originElement);
 
    this.setVisible(options.visible);
  }
 
  public destroy(): void {
    if (this.visibilityListener != null) {
      document.removeEventListener("visibilitychange", this.visibilityListener);
    }
    if (this.newScanSettingsListener != null) {
      this.scanner.removeListener("newScanSettings", this.newScanSettingsListener);
    }
    if (this.videoResizeListener != null) {
      document.removeEventListener("resize", this.videoResizeListener);
    }
    if (this.licenseFeaturesReadyListener != null) {
      this.scanner.removeListener("licenseFeaturesReady", this.licenseFeaturesReadyListener);
    }
    this.resizeObserver.disconnect();
    this.grandParentElement.remove();
    this.originElement.classList.remove(BarcodePickerGui.hiddenClassName);
  }
 
  public setCameraManager(cameraManager: CameraManager): void {
    this.cameraManager = cameraManager;
  }
 
  public pauseScanning(): void {
    this.scanningPaused = true;
    this.laserActiveImageElement.classList.add(BarcodePickerGui.hiddenOpacityClassName);
    this.laserPausedImageElement.classList.remove(BarcodePickerGui.hiddenOpacityClassName);
    this.viewfinderElement.classList.add(BarcodePickerGui.pausedClassName);
  }
 
  public resumeScanning(): void {
    this.scanningPaused = false;
    this.laserPausedImageElement.classList.add(BarcodePickerGui.hiddenOpacityClassName);
    this.laserActiveImageElement.classList.remove(BarcodePickerGui.hiddenOpacityClassName);
    this.viewfinderElement.classList.remove(BarcodePickerGui.pausedClassName);
  }
 
  public isVisible(): boolean {
    return this.visible;
  }
 
  public setVisible(visible: boolean): void {
    const browserName: string | undefined = BrowserHelper.userAgentInfo.getBrowser().name;
    if (browserName != null && browserName.includes("Safari") && this.visible != null && !this.visible && visible) {
      // Safari behaves very weirdly when displaying the video element again after being hidden:
      // it undetectably reuses video frames "buffered" from the video just before it was hidden.
      // We do this to avoid reusing old data
      this.videoElement.pause();
      this.videoElement.currentTime = 0;
      this.videoElement.load();
      this.playVideo();
    }
 
    this.visible = visible;
 
    if (visible) {
      this.originElement.classList.remove(BarcodePickerGui.hiddenClassName);
      if (this.guiStyle === BarcodePicker.GuiStyle.LASER) {
        this.laserActiveImageElement.classList.remove(BarcodePickerGui.flashColorClassName);
      } else if (this.guiStyle === BarcodePicker.GuiStyle.VIEWFINDER) {
        this.viewfinderElement.classList.remove(BarcodePickerGui.flashWhiteClassName);
      }
    } else {
      this.originElement.classList.add(BarcodePickerGui.hiddenClassName);
    }
  }
 
  public isMirrorImageEnabled(): boolean {
    if (
      this.cameraManager != null &&
      this.cameraManager.selectedCamera != null &&
      this.cameraManager.activeCamera != null
    ) {
      const mirrorImageOverride: boolean | undefined = this.mirrorImageOverrides.get(
        this.cameraManager.activeCamera.deviceId + this.cameraManager.activeCamera.label
      );
      if (mirrorImageOverride != null) {
        return mirrorImageOverride;
      } else {
        return this.cameraManager.activeCamera.cameraType === Camera.Type.FRONT;
      }
    } else {
      return false;
    }
  }
 
  public setMirrorImageEnabled(enabled: boolean, override: boolean): void {
    if (this.cameraManager != null && this.cameraManager.selectedCamera != null) {
      if (enabled) {
        this.videoElement.classList.add(BarcodePickerGui.mirroredClassName);
      } else {
        this.videoElement.classList.remove(BarcodePickerGui.mirroredClassName);
      }
 
      if (override) {
        this.mirrorImageOverrides.set(
          this.cameraManager.selectedCamera.deviceId + this.cameraManager.selectedCamera.label,
          enabled
        );
      }
    }
  }
 
  public setGuiStyle(guiStyle: BarcodePicker.GuiStyle): void {
    if (this.singleImageMode) {
      return;
    }
 
    switch (guiStyle) {
      case BarcodePicker.GuiStyle.LASER:
        this.guiStyle = guiStyle;
        this.laserContainerElement.classList.remove(BarcodePickerGui.hiddenClassName);
        this.viewfinderElement.classList.add(BarcodePickerGui.hiddenClassName);
        break;
      case BarcodePicker.GuiStyle.VIEWFINDER:
        this.guiStyle = guiStyle;
        this.laserContainerElement.classList.add(BarcodePickerGui.hiddenClassName);
        this.viewfinderElement.classList.remove(BarcodePickerGui.hiddenClassName);
        break;
      case BarcodePicker.GuiStyle.NONE:
      default:
        this.guiStyle = BarcodePicker.GuiStyle.NONE;
        this.laserContainerElement.classList.add(BarcodePickerGui.hiddenClassName);
        this.viewfinderElement.classList.add(BarcodePickerGui.hiddenClassName);
        break;
    }
  }
 
  public setLaserArea(area?: SearchArea): void {
    this.customLaserArea = area;
    if (area == null) {
      area = this.scanner.getScanSettings().getSearchArea();
    }
    const borderPercentage: number = 0.025;
    const usablePercentage: number = 1 - borderPercentage * 2;
    this.laserContainerElement.style.left = `${(borderPercentage + area.x * usablePercentage) * 100}%`;
    this.laserContainerElement.style.width = `${area.width * usablePercentage * 100}%`;
    this.laserContainerElement.style.top = `${(borderPercentage + area.y * usablePercentage) * 100}%`;
    this.laserContainerElement.style.height = `${area.height * usablePercentage * 100}%`;
  }
 
  public setViewfinderArea(area?: SearchArea): void {
    this.customViewfinderArea = area;
    if (area == null) {
      area = this.scanner.getScanSettings().getSearchArea();
    }
    const borderPercentage: number = 0.025;
    const usablePercentage: number = 1 - borderPercentage * 2;
    this.viewfinderElement.style.left = `${(borderPercentage + area.x * usablePercentage) * 100}%`;
    this.viewfinderElement.style.width = `${area.width * usablePercentage * 100}%`;
    this.viewfinderElement.style.top = `${(borderPercentage + area.y * usablePercentage) * 100}%`;
    this.viewfinderElement.style.height = `${area.height * usablePercentage * 100}%`;
  }
 
  public setVideoFit(objectFit: BarcodePicker.ObjectFit): void {
    Iif (this.singleImageMode) {
      return;
    }
 
    this.videoFit = objectFit;
 
    if (objectFit === BarcodePicker.ObjectFit.COVER) {
      this.videoElement.style.objectFit = "cover";
      this.videoElement.dataset.objectFit = "cover"; // used by "objectFitPolyfill" library
    } else {
      this.videoElement.style.objectFit = "contain";
      this.videoElement.dataset.objectFit = "contain"; // used by "objectFitPolyfill" library
 
      this.scanner.applyScanSettings(
        this.scanner.getScanSettings().setBaseSearchArea({ x: 0, y: 0, width: 1.0, height: 1.0 })
      );
    }
 
    this.resize();
  }
 
  public reassignOriginElement(originElement: HTMLElement): void {
    if (!this.visible) {
      this.originElement.classList.remove(BarcodePickerGui.hiddenClassName);
      originElement.classList.add(BarcodePickerGui.hiddenClassName);
    }
 
    originElement.appendChild(this.grandParentElement);
    this.checkAndRecoverPlayback();
    this.resize();
    this.resizeObserver.disconnect();
    this.resizeObserver.observe(originElement);
 
    this.originElement = originElement;
  }
 
  public flashGUI(): void {
    if (this.guiStyle === BarcodePicker.GuiStyle.LASER) {
      this.flashLaser();
    } else if (this.guiStyle === BarcodePicker.GuiStyle.VIEWFINDER) {
      this.flashViewfinder();
    }
  }
 
  public getVideoImageData(): Uint8ClampedArray | undefined {
    Eif (!this.singleImageMode) {
      // This could happen in very weird situations and should be temporary
      Eif (
        this.videoElement.readyState !== 4 ||
        this.videoImageCanvasContext.canvas.width <= 2 ||
        this.videoImageCanvasContext.canvas.height <= 2
      ) {
        return undefined;
      }
 
      this.videoImageCanvasContext.drawImage(this.videoElement, 0, 0);
    }
 
    return this.videoImageCanvasContext.getImageData(
      0,
      0,
      this.videoImageCanvasContext.canvas.width,
      this.videoImageCanvasContext.canvas.height
    ).data;
  }
 
  public getVideoCurrentTime(): number {
    return this.videoElement.currentTime;
  }
 
  public setCameraSwitcherVisible(visible: boolean): void {
    if (visible) {
      this.cameraSwitcherElement.classList.remove(BarcodePickerGui.hiddenClassName);
    } else {
      this.cameraSwitcherElement.classList.add(BarcodePickerGui.hiddenClassName);
    }
  }
 
  public setTorchTogglerVisible(visible: boolean): void {
    if (visible) {
      this.torchTogglerElement.classList.remove(BarcodePickerGui.hiddenClassName);
    } else {
      this.torchTogglerElement.classList.add(BarcodePickerGui.hiddenClassName);
    }
  }
 
  public playVideo(): void {
    const playPromise: Promise<void> = this.videoElement.play();
    if (playPromise != null) {
      playPromise.catch(
        /* istanbul ignore next */ () => {
          // Can sometimes cause an incorrect rejection (all is good, ignore).
        }
      );
    }
  }
 
  private setCameraUploadGuiAvailable(available: boolean): void {
    if (available) {
      this.cameraUploadProgressElement.classList.add(BarcodePickerGui.flashWhiteInsetClassName);
      this.cameraUploadElement.classList.remove(BarcodePickerGui.opacityPulseClassName);
    } else {
      this.cameraUploadProgressElement.classList.remove(BarcodePickerGui.flashWhiteInsetClassName);
      this.cameraUploadElement.classList.add(BarcodePickerGui.opacityPulseClassName);
    }
  }
 
  private setupVideoElement(): void {
    this.videoElement.setAttribute("autoplay", "autoplay");
    this.videoElement.setAttribute("playsinline", "true");
    this.videoElement.setAttribute("muted", "muted");
    this.videoElement.className = BarcodePickerGui.videoElementClassName;
    this.parentElement.appendChild(this.videoElement);
  }
 
  private setupCameraUploadGuiAssets(): void {
    this.cameraUploadElement.className = BarcodePickerGui.cameraUploadElementClassName;
    this.parentElement.appendChild(this.cameraUploadElement);
    this.cameraUploadInputElement.type = "file";
    this.cameraUploadInputElement.accept = "image/*";
    this.cameraUploadInputElement.setAttribute("capture", "environment");
    this.cameraUploadInputElement.addEventListener("change", this.cameraUploadFile.bind(this));
    this.cameraUploadInputElement.addEventListener(
      "click",
      /* istanbul ignore next */ event => {
        if (this.scanningPaused || this.scanner.isBusyProcessing()) {
          event.preventDefault();
        }
      }
    );
    this.cameraUploadLabelElement.appendChild(this.cameraUploadInputElement);
    this.cameraUploadElement.appendChild(this.cameraUploadLabelElement);
    const cameraUploadImageElement: HTMLImageElement = document.createElement("img");
    cameraUploadImageElement.src = cameraImage;
    this.cameraUploadLabelElement.appendChild(cameraUploadImageElement);
    const cameraUploadTextElement: HTMLDivElement = document.createElement("div");
    cameraUploadTextElement.innerText = "Scan from Camera";
    this.cameraUploadLabelElement.appendChild(cameraUploadTextElement);
 
    this.cameraUploadProgressElement.classList.add("radial-progress");
    this.cameraUploadElement.appendChild(this.cameraUploadProgressElement);
  }
 
  private setupFullGuiAssets(): void {
    this.laserActiveImageElement.src = laserActiveImage;
    this.laserContainerElement.appendChild(this.laserActiveImageElement);
 
    this.laserPausedImageElement.src = laserPausedImage;
    this.laserContainerElement.appendChild(this.laserPausedImageElement);
 
    this.laserContainerElement.className = BarcodePickerGui.laserContainerElementClassName;
    this.parentElement.appendChild(this.laserContainerElement);
 
    this.viewfinderElement.className = BarcodePickerGui.viewfinderElementClassName;
    this.parentElement.appendChild(this.viewfinderElement);
 
    // Show inactive GUI, as for now the scanner isn't ready yet
    this.laserActiveImageElement.classList.add(BarcodePickerGui.hiddenOpacityClassName);
    this.laserPausedImageElement.classList.remove(BarcodePickerGui.hiddenOpacityClassName);
    this.viewfinderElement.classList.add(BarcodePickerGui.pausedClassName);
  }
 
  private flashLaser(): void {
    this.laserActiveImageElement.classList.remove(BarcodePickerGui.flashColorClassName);
    // tslint:disable-next-line:no-unused-expression
    this.laserActiveImageElement.offsetHeight; // NOSONAR // Trigger reflow to restart animation
    this.laserActiveImageElement.classList.add(BarcodePickerGui.flashColorClassName);
  }
 
  private flashViewfinder(): void {
    this.viewfinderElement.classList.remove(BarcodePickerGui.flashWhiteClassName);
    // tslint:disable-next-line:no-unused-expression
    this.viewfinderElement.offsetHeight; // NOSONAR // Trigger reflow to restart animation
    this.viewfinderElement.classList.add(BarcodePickerGui.flashWhiteClassName);
  }
 
  private resize(): void {
    if (this.singleImageMode) {
      this.resizeCameraUpload();
    } else {
      this.resizeVideo();
    }
  }
 
  private resizeCameraUpload(): void {
    const width: number = this.originElement.clientWidth;
    const height: number = this.originElement.clientHeight;
 
    this.cameraUploadLabelElement.style.transform = `scale(${Math.min(1, width / 500, height / 300)})`;
    this.cameraUploadProgressElement.style.transform = `scale(${Math.min(1, width / 500, height / 300)})`;
  }
 
  private resizeVideo(): void {
    if (this.videoElement.videoWidth <= 2 || this.videoElement.videoHeight <= 2) {
      return;
    }
 
    this.parentElement.style.maxWidth = null;
    this.parentElement.style.maxHeight = null;
 
    let width: number = this.originElement.clientWidth;
    let height: number = this.originElement.clientHeight;
 
    const videoRatio: number = this.videoElement.videoWidth / this.videoElement.videoHeight;
 
    if (this.videoFit === BarcodePicker.ObjectFit.COVER) {
      let widthPercentage: number = 1;
      let heightPercentage: number = 1;
 
      if (videoRatio < width / height) {
        heightPercentage = Math.min(1, height / (width / videoRatio));
      } else {
        widthPercentage = Math.min(1, width / (height * videoRatio));
      }
 
      this.scanner.applyScanSettings(
        this.scanner.getScanSettings().setBaseSearchArea({
          x: (1 - widthPercentage) / 2,
          y: (1 - heightPercentage) / 2,
          width: widthPercentage,
          height: heightPercentage
        })
      );
 
      return;
    }
 
    if (videoRatio > width / height) {
      height = width / videoRatio;
    } else {
      width = height * videoRatio;
    }
 
    this.parentElement.style.maxWidth = `${Math.ceil(width)}px`;
    this.parentElement.style.maxHeight = `${Math.ceil(height)}px`;
 
    window.objectFitPolyfill(this.videoElement);
  }
 
  private checkAndRecoverPlayback(): void {
    Iif (
      this.cameraManager != null &&
      this.cameraManager.activeCamera != null &&
      this.videoElement != null &&
      this.videoElement.srcObject != null
    ) {
      if (!(<MediaStream>this.videoElement.srcObject).active) {
        this.cameraManager.reinitializeCamera();
      } else {
        this.playVideo();
      }
    }
  }
 
  private updateCameraUploadProgress(progressPercentageValue: string): void {
    this.cameraUploadProgressElement.setAttribute("data-progress", progressPercentageValue);
  }
 
  private async cameraUploadImageLoad(image: HTMLImageElement): Promise<void> {
    this.updateCameraUploadProgress("100");
 
    let resizedImageWidth: number;
    let resizedImageHeight: number;
    const resizedImageSizeLimit: number = 1440;
    if (image.naturalWidth <= resizedImageSizeLimit && image.naturalHeight <= resizedImageSizeLimit) {
      resizedImageWidth = image.naturalWidth;
      resizedImageHeight = image.naturalHeight;
    } else {
      if (image.naturalWidth > image.naturalHeight) {
        resizedImageWidth = resizedImageSizeLimit;
        resizedImageHeight = Math.round((image.naturalHeight / image.naturalWidth) * resizedImageSizeLimit);
      } else {
        resizedImageWidth = Math.round((image.naturalWidth / image.naturalHeight) * resizedImageSizeLimit);
        resizedImageHeight = resizedImageSizeLimit;
      }
    }
 
    await this.cameraUploadFileProcess(image, resizedImageWidth, resizedImageHeight);
  }
 
  private async cameraUploadFileProcess(image: HTMLImageElement, width: number, height: number): Promise<void> {
    this.videoImageCanvasContext.canvas.width = width;
    this.videoImageCanvasContext.canvas.height = height;
 
    this.videoImageCanvasContext.drawImage(image, 0, 0, width, height);
    this.scanner.applyImageSettings({
      width,
      height,
      format: ImageSettings.Format.RGBA_8U
    });
 
    this.setCameraUploadGuiAvailable(false);
 
    await this.cameraUploadCallback();
    this.setCameraUploadGuiAvailable(true);
  }
 
  private cameraUploadFile(): void {
    const files: FileList | null = this.cameraUploadInputElement.files;
    if (files != null && files.length !== 0) {
      const image: HTMLImageElement = new Image();
      const fileReader: FileReader = new FileReader();
      fileReader.onload = () => {
        this.cameraUploadInputElement.value = "";
        Eif (fileReader.result != null) {
          image.onload = this.cameraUploadImageLoad.bind(this, image);
          image.onprogress = event2 => {
            Eif (event2.lengthComputable) {
              const progress: number = Math.round((event2.loaded / event2.total) * 20) * 5;
              Eif (progress <= 100) {
                this.updateCameraUploadProgress(progress.toString());
              }
            }
          };
          image.src = <string>fileReader.result;
        }
      };
      this.updateCameraUploadProgress("0");
      fileReader.readAsDataURL(files[0]);
    }
  }
 
  private setupCameraSwitcher(): void {
    this.cameraSwitcherElement.src = switchCameraImage;
    this.cameraSwitcherElement.className = BarcodePickerGui.cameraSwitcherElementClassName;
    this.cameraSwitcherElement.classList.add(BarcodePickerGui.hiddenClassName);
    this.parentElement.appendChild(this.cameraSwitcherElement);
    ["touchstart", "mousedown"].forEach(eventName => {
      this.cameraSwitcherElement.addEventListener(eventName, async event => {
        if (this.cameraManager != null) {
          const cameraManager: CameraManager = this.cameraManager;
          event.preventDefault();
          try {
            const cameras: Camera[] = await CameraAccess.getCameras();
            const newCameraIndex: number =
              (cameras.findIndex(camera => {
                return (
                  camera.deviceId ===
                  (cameraManager.activeCamera == null ? camera.deviceId : cameraManager.activeCamera.deviceId)
                );
              }) +
                1) %
              cameras.length;
            cameraManager
              .initializeCameraWithSettings(cameras[newCameraIndex], cameraManager.activeCameraSettings)
              .catch(console.error);
          } catch (error) {
            console.error(error);
          }
        }
      });
    });
  }
 
  private setupTorchToggler(): void {
    this.torchTogglerElement.src = toggleTorchImage;
    this.torchTogglerElement.className = BarcodePickerGui.torchTogglerElementClassName;
    this.torchTogglerElement.classList.add(BarcodePickerGui.hiddenClassName);
    this.parentElement.appendChild(this.torchTogglerElement);
    ["touchstart", "mousedown"].forEach(eventName => {
      this.torchTogglerElement.addEventListener(eventName, event => {
        if (this.cameraManager != null) {
          event.preventDefault();
          this.cameraManager.toggleTorch();
        }
      });
    });
  }
 
  private showScanditLogo(hideLogo: boolean, licenseFeatures?: { hiddenScanditLogoAllowed?: boolean }): void {
    Iif (
      hideLogo &&
      licenseFeatures != null &&
      licenseFeatures.hiddenScanditLogoAllowed != null &&
      licenseFeatures.hiddenScanditLogoAllowed
    ) {
      return;
    }
 
    const scanditLogoImageElement: HTMLImageElement = document.createElement("img");
    scanditLogoImageElement.src = scanditLogoImage;
    scanditLogoImageElement.className = BarcodePickerGui.scanditLogoImageElementClassName;
    this.parentElement.appendChild(scanditLogoImageElement);
  }
 
  private handleNewScanSettings(): void {
    if (this.customLaserArea == null) {
      this.setLaserArea();
    }
    if (this.customViewfinderArea == null) {
      this.setViewfinderArea();
    }
  }
 
  private handleVideoResize(): void {
    if (
      this.videoImageCanvasContext.canvas.width === this.videoElement.videoWidth &&
      this.videoImageCanvasContext.canvas.height === this.videoElement.videoHeight
    ) {
      return;
    }
 
    this.videoImageCanvasContext.canvas.width = this.videoElement.videoWidth;
    this.videoImageCanvasContext.canvas.height = this.videoElement.videoHeight;
 
    this.scanner.applyImageSettings({
      width: this.videoElement.videoWidth,
      height: this.videoElement.videoHeight,
      format: ImageSettings.Format.RGBA_8U
    });
 
    this.resize();
  }
}