UNPKG

@privateid/ultra-web-sdk-alpha

Version:
207 lines 10.1 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; /* eslint-disable lines-between-class-members */ /* eslint-disable import/prefer-default-export */ import { detect } from 'detect-browser'; import { proxy } from 'comlink'; import { callbackTypeEnum, createCallback } from '../../../../createCallback'; import { ImageType } from '../../../../types'; import { getDebugType, getIsSIMD, printLogs, iOS } from '../../global/shared.utils'; import { createImages, releaseCanvas } from './document.utils'; import { DEBUG_TYPES_WITH_IMAGES, INPUT_IMAGE_FORMAT, CANVAS_ID, FINGERS_OVER_DOCUMENT_THRESHOLD, DOCUMENT_THRESHOLD_X, DOCUMENT_THRESHOLD_Y, DOCUMENT_TOO_CLOSE_THRESHOLD, DOCUMENT_TOO_FAR_THRESHOLD, CALCULATE_AGE_FROM_OCR_TEXT, ERROR_RESULT, ERROR_CODE, } from './document.constants'; export class DocumentService { constructor(wasmService, cameraService) { this.isSimd = false; this.debugType = getDebugType(); this.isDebugWithImages = DEBUG_TYPES_WITH_IMAGES.includes(this.debugType); this.frontScanCanvas = document.createElement('canvas'); this.backScanCanvas = document.createElement('canvas'); getIsSIMD().then((simd) => (this.isSimd = simd)); this.wasmService = wasmService; this.cameraService = cameraService; } scanFrontDocument(params) { return __awaiter(this, void 0, void 0, function* () { const { callback, config } = params; printLogs('________________ FRONT DOCUMENT SCAN ______________', ''); const videoEl = this.getVideoElement(); let configuration = { input_image_format: INPUT_IMAGE_FORMAT, fingers_over_document_threshold: FINGERS_OVER_DOCUMENT_THRESHOLD, }; if (config) { configuration = Object.assign(Object.assign({}, configuration), config); } if (!videoEl) { printLogs(`Capture need the video element id`, '', 'ERROR'); return { result: ERROR_RESULT }; } this.privid_wasm_result = createCallback({ type: callbackTypeEnum.frontScan, callbackFunction: callback, }); const cb = proxy(this.privid_wasm_result); const imageData = this.createImageData(videoEl); if (this.isDebugWithImages) { yield createImages([imageData], `${ImageType.original}_front_document_scan`, true); } try { configuration = JSON.stringify(configuration); return yield this.wasmService.scanFrontDocument(imageData, this.isSimd, configuration, cb); } catch (e) { printLogs(`isValidPhotoID error: `, e, 'ERROR'); return { result: ERROR_CODE, }; } }); } scanPassport(params) { return __awaiter(this, void 0, void 0, function* () { const { callback, config } = params; printLogs('________________ PASSPORT SCAN ______________', ''); const videoEl = this.getVideoElement(); let configuration = { input_image_format: INPUT_IMAGE_FORMAT, }; if (config) { configuration = Object.assign(Object.assign({}, configuration), config); } this.privid_wasm_result = createCallback({ type: callbackTypeEnum.passportScan, callbackFunction: callback }); if (!videoEl) { printLogs(`Capture need the video element id`, '', 'ERROR'); return { result: ERROR_RESULT }; } const cb = proxy(this.privid_wasm_result); const imageData = this.createImageData(videoEl); if (this.isDebugWithImages) { yield createImages([imageData], `${ImageType.original}_passport_scan`, true); } try { configuration = JSON.stringify(configuration); return yield this.wasmService.scanFrontDocument(imageData, this.isSimd, configuration, cb); } catch (e) { printLogs(`isValidPhotoID error: `, e, 'ERROR'); return { result: ERROR_CODE, }; } }); } scanBackDocument(params) { return __awaiter(this, void 0, void 0, function* () { const { callback, image, config } = params; printLogs('________________ DOCUMENT SCAN ______________', ''); const videoEl = this.getVideoElement(); let configuration = { input_image_format: INPUT_IMAGE_FORMAT, }; if (config) { configuration = Object.assign(Object.assign({}, configuration), config); } const { os } = detect(); if (iOS() || ['iOS', 'android', 'Android OS'].includes(os)) { this.privid_wasm_result = createCallback({ type: callbackTypeEnum.backScan, callbackFunction: callback, isMobile: true, }); } else if (configuration === null || configuration === void 0 ? void 0 : configuration.document_scan_barcode_only) { this.privid_wasm_result = createCallback({ type: callbackTypeEnum.backScan, callbackFunction: callback, isMobile: true, }); } else if (configuration === null || configuration === void 0 ? void 0 : configuration.detect_barcode_without_parsing) { this.privid_wasm_result = createCallback({ type: callbackTypeEnum.backScan, callbackFunction: callback, }); } else { configuration = Object.assign(Object.assign({}, configuration), { detect_document_only: true }); this.privid_wasm_result = createCallback({ type: callbackTypeEnum.backScan, callbackFunction: callback }); } configuration = JSON.stringify(configuration); if (!videoEl) { printLogs(`Capture need the video element id`, '', 'ERROR'); return { result: ERROR_RESULT }; } const imageData = this.createImageData(videoEl); if ((imageData === null || imageData === void 0 ? void 0 : imageData.width) === 0) return { result: ERROR_RESULT }; let result = null; if (this.isDebugWithImages) { yield createImages([imageData], ImageType.original, true); } try { printLogs(`Configuration: back scan: `, configuration); result = yield this.wasmService.scanBackDocument(imageData, this.isSimd, configuration, proxy(this.privid_wasm_result)); releaseCanvas(this.backScanCanvas); return Object.assign({}, result); } catch (e) { printLogs(`isValidPhotoID error: `, e, 'ERROR'); return { result: ERROR_CODE, }; } }); } documentOcr(params) { return __awaiter(this, void 0, void 0, function* () { printLogs('________________ DOCUMENT OCR ______________', ''); let configuration = { input_image_format: INPUT_IMAGE_FORMAT, calculate_age_from_ocr_text: CALCULATE_AGE_FROM_OCR_TEXT, threshold_doc_x: DOCUMENT_THRESHOLD_X, threshold_doc_y: DOCUMENT_THRESHOLD_Y, threshold_doc_too_close: DOCUMENT_TOO_CLOSE_THRESHOLD, threshold_doc_too_far: DOCUMENT_TOO_FAR_THRESHOLD, }; if (params.config) { configuration = Object.assign(Object.assign({}, configuration), params.config); } this.privid_wasm_result = createCallback({ type: callbackTypeEnum.documentOcr, callbackFunction: params.callback }); const cb = proxy(this.privid_wasm_result); if (this.isDebugWithImages) { yield createImages([params.inputImage], `${ImageType.original}_document_ocr`, true); } try { configuration = JSON.stringify(configuration); return yield this.wasmService.documentOcr(params.inputImage, configuration, cb); } catch (e) { printLogs(`isValidPhotoID error: `, e, 'ERROR'); return { result: ERROR_CODE, }; } }); } getVideoElement() { return this.cameraService.getVideoElementDOM(); } createImageData(videoElement) { const height = videoElement.videoHeight; const width = videoElement.videoWidth; this.frontScanCanvas.setAttribute('id', CANVAS_ID); this.frontScanCanvas.setAttribute('height', `${height}`); this.frontScanCanvas.setAttribute('width', `${width}`); const context = this.frontScanCanvas.getContext('2d', { willReadFrequently: true }); context.drawImage(videoElement, 0, 0); return context.getImageData(0, 0, width, height); } } //# sourceMappingURL=DocumentService.js.map