Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
set device(device: MediaDeviceInfo | null) {
if (!device && device !== null) {
throw new ArgumentException('The `device` must be a valid MediaDeviceInfo or null.');
}
if (this.isCurrentDevice(device)) {
console.warn('Setting the same device is not allowed.');
return;
}
if (this.isAutostarting) {
// do not allow setting devices during auto-start, since it will set one and emit it.
console.warn('Avoid setting a device during auto-start.');
return;
}
if (!this.hasPermission) {
console.warn('Permissions not set yet, waiting for them to be set to apply device change.');
// this.permissionResponse
private decode(
callbackFn: (result: Result) => any,
retryIfNotFound: boolean = true,
retryIfChecksumOrFormatError: boolean = true,
once = false
): void {
if (undefined === this.canvasElementContext) {
this.prepareCaptureCanvas();
}
this.canvasElementContext.drawImage(this.videoElement || this.imageElement, 0, 0);
// @note generates zone.js error when switching cameras
const luminanceSource = new HTMLCanvasElementLuminanceSource(this.canvasElement);
const binaryBitmap = new BinaryBitmap(new HybridBinarizer(luminanceSource));
try {
const result = this.readerDecode(binaryBitmap);
callbackFn(result);
if (!once && !!this.stream) {
setTimeout(() => this.decodeWithDelay(callbackFn), this.timeBetweenScansMillis);
}
} catch (re) {
console.log(retryIfChecksumOrFormatError, re);
if (retryIfNotFound && Exception.isOfType(re, Exception.NotFoundException)) {
console.warn('Not found, trying again...');
private decode(
callbackFn: (result: Result) => any,
retryIfNotFound: boolean = true,
retryIfChecksumOrFormatError: boolean = true,
once = false
): void {
if (undefined === this.canvasElementContext) {
this.prepareCaptureCanvas();
}
this.canvasElementContext.drawImage(this.videoElement || this.imageElement, 0, 0);
// @note generates zone.js error when switching cameras
const luminanceSource = new HTMLCanvasElementLuminanceSource(this.canvasElement);
const binaryBitmap = new BinaryBitmap(new HybridBinarizer(luminanceSource));
try {
const result = this.readerDecode(binaryBitmap);
callbackFn(result);
if (!once && !!this.stream) {
setTimeout(() => this.decodeWithDelay(callbackFn), this.timeBetweenScansMillis);
}
} catch (re) {
console.log(retryIfChecksumOrFormatError, re);
if (retryIfNotFound && Exception.isOfType(re, Exception.NotFoundException)) {
console.warn('Not found, trying again...');
this.scanFailure = new EventEmitter();
this.scanError = new EventEmitter();
this.scanComplete = new EventEmitter();
this.camerasFound = new EventEmitter();
this.camerasNotFound = new EventEmitter();
this.permissionResponse = new EventEmitter();
this.hasDevices = new EventEmitter();
this.deviceChange = new EventEmitter();
// computed data
this._hints = new Map();
this.hasNavigator = typeof navigator !== 'undefined';
this.isMediaDevicesSuported = this.hasNavigator && !!navigator.mediaDevices;
// will start codeReader if needed.
this.formats = [BarcodeFormat.QR_CODE];
}
this.scanSuccess = new EventEmitter();
this.scanFailure = new EventEmitter();
this.scanError = new EventEmitter();
this.scanComplete = new EventEmitter();
this.camerasFound = new EventEmitter();
this.camerasNotFound = new EventEmitter();
this.permissionResponse = new EventEmitter(true);
this.hasDevices = new EventEmitter();
this.deviceChange = new EventEmitter();
this._device = null;
this._enabled = true;
this._hints = new Map();
this.autofocusEnabled = true;
this.autostart = true;
this.formats = [BarcodeFormat.QR_CODE];
// computed data
this.hasNavigator = typeof navigator !== 'undefined';
this.isMediaDevicesSuported = this.hasNavigator && !!navigator.mediaDevices;
}
set device(device: MediaDeviceInfo | null) {
if (this.isAlreadyDefinedDevice(device)) {
return;
}
// in order to change the device the codeReader gotta be reseted
this.scannerStop();
if (!device && device !== null) {
throw new ArgumentException('The `device` must be a valid MediaDeviceInfo or null.');
}
if (!!device) {
// starts if enabled and set the current device
this.startScan(device);
}
}
set tryHarder(enable: boolean) {
if (enable) {
this.hints.set(DecodeHintType.TRY_HARDER, true);
} else {
this.hints.delete(DecodeHintType.TRY_HARDER);
}
// new instance with new hints.
this.scannerRestart();
}
if (!once && !!this.stream) {
setTimeout(() => this.decodeWithDelay(callbackFn), this.timeBetweenScansMillis);
}
} catch (re) {
console.log(retryIfChecksumOrFormatError, re);
if (retryIfNotFound && Exception.isOfType(re, Exception.NotFoundException)) {
console.warn('Not found, trying again...');
this.decodeWithDelay(callbackFn);
} else if (
retryIfChecksumOrFormatError &&
(
Exception.isOfType(re, Exception.ChecksumException) ||
Exception.isOfType(re, Exception.FormatException)
)
) {
console.log('Checksum or format error, trying again...', re);
this.decodeWithDelay(callbackFn);
}
}
}
const luminanceSource = new HTMLCanvasElementLuminanceSource(this.canvasElement);
const binaryBitmap = new BinaryBitmap(new HybridBinarizer(luminanceSource));
try {
const result = this.readerDecode(binaryBitmap);
callbackFn(result);
if (!once && !!this.stream) {
setTimeout(() => this.decodeWithDelay(callbackFn), this.timeBetweenScansMillis);
}
} catch (re) {
console.log(retryIfChecksumOrFormatError, re);
if (retryIfNotFound && Exception.isOfType(re, Exception.NotFoundException)) {
console.warn('Not found, trying again...');
this.decodeWithDelay(callbackFn);
} else if (
retryIfChecksumOrFormatError &&
(
Exception.isOfType(re, Exception.ChecksumException) ||
Exception.isOfType(re, Exception.FormatException)
)
) {
console.log('Checksum or format error, trying again...', re);
this.decodeWithDelay(callbackFn);
}
}
}
if (!once && !!this.stream) {
setTimeout(() => this.decodeWithDelay(callbackFn), this.timeBetweenScansMillis);
}
} catch (re) {
console.log(retryIfChecksumOrFormatError, re);
if (retryIfNotFound && Exception.isOfType(re, Exception.NotFoundException)) {
console.warn('Not found, trying again...');
this.decodeWithDelay(callbackFn);
} else if (
retryIfChecksumOrFormatError &&
(
Exception.isOfType(re, Exception.ChecksumException) ||
Exception.isOfType(re, Exception.FormatException)
)
) {
console.log('Checksum or format error, trying again...', re);
this.decodeWithDelay(callbackFn);
}
}
}