scandoc-ai-components 0.0.83 → 0.0.85

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +83 -103
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -11191,6 +11191,9 @@ class ExtractorVideo {
11191
11191
  this.unsupportedDocCount = 0;
11192
11192
  this.video = null;
11193
11193
  this.lastMessage = null;
11194
+ this.waitingForSecondSide = false;
11195
+ this.hasShownTurnMessage = false;
11196
+ this.awaitingNewPresence = false;
11194
11197
  }
11195
11198
  reset() {
11196
11199
  this.stopVideo();
@@ -11204,12 +11207,12 @@ class ExtractorVideo {
11204
11207
  this.unsupportedDocCount = 0;
11205
11208
  this.video = null;
11206
11209
  this.lastMessage = null;
11210
+ this.waitingForSecondSide = false;
11211
+ this.hasShownTurnMessage = false;
11212
+ this.awaitingNewPresence = false;
11207
11213
  }
11208
11214
  async analyzeVideoStream() {
11209
- if (!this.isRunning) {
11210
- return;
11211
- }
11212
- //
11215
+ if (!this.isRunning) return;
11213
11216
  const now = Date.now();
11214
11217
  const cfgValues = (0,_config__WEBPACK_IMPORTED_MODULE_1__.getScanDocAIConfigValues)();
11215
11218
  if (this.scanStartTime && now - this.scanStartTime > cfgValues.MAX_SCAN_DURATION_MS) {
@@ -11221,111 +11224,99 @@ class ExtractorVideo {
11221
11224
  });
11222
11225
  return;
11223
11226
  }
11224
- //
11225
11227
  const canvas = document.createElement("canvas");
11226
- let video = document.getElementById("ScanDocAIVideoElement");
11228
+ const video = document.getElementById("ScanDocAIVideoElement");
11227
11229
  if (video && video.videoWidth < video.videoHeight) {
11228
11230
  this.showMessage("Please rotate your device to landscape mode.");
11229
11231
  this.candidateImages = [];
11230
11232
  setTimeout(() => this.analyzeVideoStream(), ExtractorVideo.FREQUENCY_MS);
11231
11233
  return;
11232
11234
  }
11233
- //
11234
11235
  const fullImage = (0,_utils__WEBPACK_IMPORTED_MODULE_4__.videoImgToBase64)(video, canvas, 1280, 720, false);
11235
11236
  const validationImage = (0,_utils__WEBPACK_IMPORTED_MODULE_4__.videoImgToBase64)(video, canvas, ExtractorVideo.VALIDATION_IMG_WIDTH, ExtractorVideo.VALIDATION_IMG_HEIGHT, true);
11236
11237
  DOCUMENT_DETECTOR.isDocumentPresent(video).then(async isPresent => {
11237
- if (isPresent) {
11238
- this.candidateImages.push({
11239
- fullImg: fullImage,
11240
- validationImg: validationImage
11241
- });
11242
- const images = [...this.candidateImages];
11243
- if (images.length >= ExtractorVideo.VALIDATION_BATCH_SIZE) {
11244
- const [isValidationOk, response] = await (0,_requests_validation__WEBPACK_IMPORTED_MODULE_3__["default"])(images.map(e => e["validationImg"]), this.pastBlurValues, {});
11245
- if (isValidationOk) {
11246
- if (response["InfoCode"] === "1006") {
11247
- this.unsupportedDocCount += 1;
11248
- if (this.unsupportedDocCount >= cfgValues.MAX_UNSUPPORTED_ATTEMPTS) {
11249
- this.stopVideo();
11250
- this.onExtractedResults({
11251
- success: false,
11252
- code: "002",
11253
- info: "Document template not supported."
11254
- });
11255
- this.unsupportedDocCount = 0;
11256
- this.candidateImages = [];
11257
- return;
11258
- }
11259
- } else {
11260
- this.unsupportedDocCount = 0;
11261
- }
11262
- const imageSide = response["Side"];
11263
- const isAlreadyScanned = this.extractionImages[imageSide] !== undefined;
11264
- const scannedSides = Object.keys(this.extractionImages).length;
11265
-
11266
- // Show feedback only if both sides are not yet scanned
11267
- // Avoid overwriting "Turn to the other side" until second side appears
11268
- if (scannedSides < 1) {
11269
- this.showMessage(response["Info"]); // normal feedback before first side
11270
- } else if (!isAlreadyScanned && scannedSides === 1) {
11271
- // We are seeing a new side after 1 side has already been scanned
11272
- this.showMessage(response["Info"]); // allow resuming feedback (if desired)
11273
- }
11274
- //
11275
- if ("DetectedBlurValue" in response) {
11276
- this.pastBlurValues.push(response["DetectedBlurValue"]);
11277
- } else {
11278
- this.pastBlurValues = [];
11279
- }
11280
- //
11281
- if (response["InfoCode"] === "1007") {
11282
- const imageId = response["Index"];
11283
- const imageSide = response["Side"];
11284
- //
11285
- if (this.extractionImages[imageSide] !== undefined) {
11286
- this.candidateImages = [];
11287
- return;
11288
- }
11289
- this.extractionImages[imageSide] = images[imageId]["fullImg"];
11290
- const extractionImages = {
11291
- ...this.extractionImages
11292
- };
11293
- //
11294
- if (Object.keys(extractionImages).length === 1) {
11295
- this.scanStartTime = Date.now();
11296
- this.showMessage("Turn to the other side");
11297
- }
11298
- if (Object.keys(extractionImages).length === 2) {
11299
- this.showMessage("Validation successful");
11300
- const [isExtractionOk, extractionData] = await (0,_requests_extraction__WEBPACK_IMPORTED_MODULE_2__["default"])(extractionImages["FRONT"], extractionImages["BACK"], {
11301
- IgnoreBackImage: false
11302
- });
11303
- this.onExtraction(isExtractionOk, extractionData);
11304
- return;
11305
- }
11306
- } else if (response["InfoCode"] === "1000") {
11307
- this.showMessage("Validation successful");
11308
- this.showMessage("Extracting data");
11309
- const imageId = response["Index"];
11310
- const image = images[imageId]["fullImg"];
11311
- const [isExtractionOk, extractionData] = await (0,_requests_extraction__WEBPACK_IMPORTED_MODULE_2__["default"])(image, undefined, {
11312
- IgnoreBackImage: true
11313
- });
11314
- this.onExtraction(isExtractionOk, extractionData);
11315
- return;
11316
- }
11317
- }
11238
+ if (this.awaitingNewPresence && isPresent) {
11239
+ this.awaitingNewPresence = false;
11240
+ }
11241
+ if (!isPresent) return;
11242
+ this.candidateImages.push({
11243
+ fullImg: fullImage,
11244
+ validationImg: validationImage
11245
+ });
11246
+ const images = [...this.candidateImages];
11247
+ if (images.length < ExtractorVideo.VALIDATION_BATCH_SIZE) return;
11248
+ const [isValidationOk, response] = await (0,_requests_validation__WEBPACK_IMPORTED_MODULE_3__["default"])(images.map(e => e.validationImg), this.pastBlurValues, {});
11249
+ if (!isValidationOk) {
11250
+ this.candidateImages = [];
11251
+ return;
11252
+ }
11253
+ const infoCode = response["InfoCode"];
11254
+ const imageId = response["Index"];
11255
+ const imageSide = response["Side"];
11256
+ const message = response["Info"];
11257
+ const isNewSide = this.extractionImages[imageSide] === undefined;
11258
+ const numScannedSides = Object.keys(this.extractionImages).length;
11259
+ if (!this.waitingForSecondSide || isNewSide) {
11260
+ this.showMessage(message);
11261
+ }
11262
+ if ("DetectedBlurValue" in response) {
11263
+ this.pastBlurValues.push(response["DetectedBlurValue"]);
11264
+ } else {
11265
+ this.pastBlurValues = [];
11266
+ }
11267
+ if (infoCode === "1007") {
11268
+ if (!isNewSide) {
11318
11269
  this.candidateImages = [];
11270
+ return;
11271
+ }
11272
+ this.extractionImages[imageSide] = images[imageId].fullImg;
11273
+ if (Object.keys(this.extractionImages).length === 1) {
11274
+ this.waitingForSecondSide = true;
11275
+ this.hasShownTurnMessage = true;
11276
+ this.awaitingNewPresence = true;
11277
+ this.scanStartTime = Date.now();
11278
+ this.showMessage("Turn to the other side", true);
11279
+ } else if (Object.keys(this.extractionImages).length === 2) {
11280
+ this.waitingForSecondSide = false;
11281
+ this.hasShownTurnMessage = false;
11282
+ this.awaitingNewPresence = false;
11283
+ this.showMessage("Validation successful", true);
11284
+ const [isExtractionOk, extractionData] = await (0,_requests_extraction__WEBPACK_IMPORTED_MODULE_2__["default"])(this.extractionImages["FRONT"], this.extractionImages["BACK"], {
11285
+ IgnoreBackImage: false
11286
+ });
11287
+ this.onExtraction(isExtractionOk, extractionData);
11288
+ return;
11319
11289
  }
11290
+ } else if (infoCode === "1000") {
11291
+ this.waitingForSecondSide = false;
11292
+ this.hasShownTurnMessage = false;
11293
+ this.awaitingNewPresence = false;
11294
+ this.showMessage("Validation successful");
11295
+ this.showMessage("Extracting data");
11296
+ const image = images[imageId].fullImg;
11297
+ const [isExtractionOk, extractionData] = await (0,_requests_extraction__WEBPACK_IMPORTED_MODULE_2__["default"])(image, undefined, {
11298
+ IgnoreBackImage: true
11299
+ });
11300
+ this.onExtraction(isExtractionOk, extractionData);
11301
+ return;
11320
11302
  }
11303
+ this.candidateImages = [];
11321
11304
  }).finally(() => {
11322
11305
  setTimeout(() => this.analyzeVideoStream(), ExtractorVideo.FREQUENCY_MS);
11323
11306
  });
11324
11307
  }
11308
+ showMessage(message, force = false) {
11309
+ if (!message || this.lastMessage === message) return;
11310
+ if (this.hasShownTurnMessage && this.awaitingNewPresence && !force) return;
11311
+ this.lastMessage = message;
11312
+ const messageElement = document.getElementById("ScanDocAIMessage");
11313
+ if (messageElement) {
11314
+ messageElement.innerText = message;
11315
+ }
11316
+ }
11325
11317
  onExtraction(isExtractionOk, extractionData) {
11326
11318
  this.candidateImages = [];
11327
11319
  this.extractionImages = {};
11328
- this.lastMessage = null;
11329
11320
  this.stopVideo();
11330
11321
  if (isExtractionOk) {
11331
11322
  this.showMessage("Success - data extracted", "success");
@@ -11350,8 +11341,7 @@ class ExtractorVideo {
11350
11341
  async startVideo() {
11351
11342
  try {
11352
11343
  const serviceConfig = (0,_config__WEBPACK_IMPORTED_MODULE_1__.getScanDocAIConfig)();
11353
- await serviceConfig.getAccessToken(true); // this will throw if 401
11354
-
11344
+ await serviceConfig.getAccessToken(true);
11355
11345
  const videoElem = document.getElementById("ScanDocAIVideoElement");
11356
11346
  if (!videoElem) {
11357
11347
  throw new Error("Video element not found.");
@@ -11384,7 +11374,7 @@ class ExtractorVideo {
11384
11374
  });
11385
11375
  window.addEventListener('resize', () => this.adjustOverlayPosition());
11386
11376
  setTimeout(() => this.adjustOverlayPosition(), 500);
11387
- this.scanStartTime = Date.now(); // Reset timer
11377
+ this.scanStartTime = Date.now();
11388
11378
  setTimeout(() => this.analyzeVideoStream(), ExtractorVideo.FREQUENCY_MS);
11389
11379
  this.showMessage("Starting scanning");
11390
11380
  return true;
@@ -11402,7 +11392,6 @@ class ExtractorVideo {
11402
11392
  }
11403
11393
  stopVideo() {
11404
11394
  this.isRunning = false;
11405
- this.lastMessage = null;
11406
11395
  if (this.video) {
11407
11396
  this.video.pause();
11408
11397
  if (this.video.srcObject !== undefined && this.video.srcObject !== null) {
@@ -11411,15 +11400,6 @@ class ExtractorVideo {
11411
11400
  this.video.srcObject = null;
11412
11401
  }
11413
11402
  }
11414
- showMessage(message) {
11415
- if (!message || message.trim() === "") return;
11416
- if (this.lastMessage === message) return;
11417
- this.lastMessage = message;
11418
- const messageElement = document.getElementById("ScanDocAIMessage");
11419
- if (messageElement) {
11420
- messageElement.innerText = message;
11421
- }
11422
- }
11423
11403
  adjustOverlayPosition() {
11424
11404
  const video = this.video;
11425
11405
  const dot = document.querySelector(".centerGuideDot");
@@ -11450,7 +11430,7 @@ class ExtractorVideo {
11450
11430
  const isMobile = window.innerWidth < 768 || window.innerHeight > window.innerWidth;
11451
11431
  window.innerWidth < 768 || window.innerHeight > window.innerWidth && window.innerWidth < 1024;
11452
11432
  if (isMobile) {
11453
- // Mobile version with overlay feedback
11433
+ // Mobile version with overlay feedback
11454
11434
  return `
11455
11435
  <div class="mobileVideoArea">
11456
11436
  <video id="ScanDocAIVideoElement" class="mobileVideo" autoplay muted playsinline></video>
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "scandoc-ai-components",
3
3
  "author": "ScanDoc-AI",
4
- "version": "0.0.83",
4
+ "version": "0.0.85",
5
5
  "private": false,
6
6
  "description": "Pure JavaScript package for integrating ScanDoc-AI services.",
7
7
  "keywords": [