veryfi-lens-wasm 1.2.3 → 1.2.5

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/package.json +1 -1
  2. package/veryfi-lens.js +275 -22
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "veryfi-lens-wasm",
3
- "version": "1.2.3",
3
+ "version": "1.2.5",
4
4
  "description": "Lens For Web SDK with WASM",
5
5
  "main": "veryfi-lens.js",
6
6
  "type": "module",
package/veryfi-lens.js CHANGED
@@ -448,6 +448,80 @@ const VeryfiLens = (function () {
448
448
  }
449
449
  };
450
450
 
451
+ const getChecksVideo = async () => {
452
+ const isDesktop =
453
+ !/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
454
+ navigator.userAgent
455
+ );
456
+ const isAndroid = /Android/i.test(navigator.userAgent);
457
+ const isLandscape = window.innerWidth > window.innerHeight;
458
+
459
+ if (navigator) {
460
+ try {
461
+ const devices = await navigator.mediaDevices.enumerateDevices();
462
+ console.log(devices);
463
+ const videoDevices = devices.filter(
464
+ (device) => device.kind === "videoinput"
465
+ );
466
+
467
+ let useMainCamera = false;
468
+ let mainCameraDeviceId = null;
469
+ if (isAndroid) {
470
+ for (const device of videoDevices) {
471
+ if (device.label.includes("camera2 0")) {
472
+ console.log("Main camera found", device.deviceId);
473
+ mainCameraDeviceId = device.deviceId;
474
+ useMainCamera = true;
475
+ break;
476
+ }
477
+ }
478
+ }
479
+
480
+ let videoConfig = {
481
+ width: { ideal: 3840 },
482
+ height: { ideal: 2160 },
483
+ };
484
+
485
+ if (isDesktop) {
486
+ // Force portrait mode for desktop
487
+ videoConfig.aspectRatio = 9 / 16;
488
+ } else {
489
+ // For mobile, adapt to device orientation
490
+ videoConfig.aspectRatio = isLandscape ? 16 / 9 : 9 / 16;
491
+ }
492
+
493
+ if (useMainCamera) {
494
+ videoConfig.deviceId = { exact: mainCameraDeviceId };
495
+ } else {
496
+ videoConfig.facingMode = "environment";
497
+ }
498
+
499
+ const stream = await navigator.mediaDevices.getUserMedia({
500
+ video: videoConfig,
501
+ });
502
+ const video = videoRef;
503
+ video.srcObject = stream;
504
+ wasmWrapper.setDocumentCallback(logDocument);
505
+
506
+ // Add event listener to handle orientation changes on mobile
507
+ if (!isDesktop) {
508
+ window.addEventListener("orientationchange", async () => {
509
+ const isNewLandscape = window.innerWidth > window.innerHeight;
510
+ videoConfig.aspectRatio = isNewLandscape ? 16 / 9 : 9 / 16;
511
+ const newStream = await navigator.mediaDevices.getUserMedia({
512
+ video: videoConfig,
513
+ });
514
+ video.srcObject = newStream;
515
+ });
516
+ }
517
+ } catch (error) {
518
+ console.error("Error accessing the camera", error);
519
+ }
520
+ } else {
521
+ console.log("No navigator available");
522
+ }
523
+ };
524
+
451
525
  const loadImage = (src) => {
452
526
  return new Promise((resolve, reject) => {
453
527
  const img = new Image();
@@ -466,8 +540,8 @@ const VeryfiLens = (function () {
466
540
  if (videoRef) {
467
541
  let rCorners;
468
542
  const video = videoRef;
469
- let videoHeight = Number(video.videoHeight);
470
- let videoWidth = Number(video.videoWidth);
543
+ let videoHeight = video.videoHeight;
544
+ let videoWidth = video.videoWidth;
471
545
  const fullSizeCanvas = document.createElement("canvas");
472
546
  fullSizeCanvas.width = videoWidth;
473
547
  fullSizeCanvas.height = videoHeight;
@@ -484,9 +558,7 @@ const VeryfiLens = (function () {
484
558
  createImageBitmap(fullSizeImage).then((bitmap) => {
485
559
  fullSizeImage = bitmap;
486
560
  wasmWrapper.processDocument(bitmap, mode);
487
- rCorners = coordinates.map((corner) =>
488
- corner.map((cord) => cord / scale)
489
- );
561
+ rCorners = coordinates.map((corner) => corner);
490
562
  if (
491
563
  rCorners.length &&
492
564
  rCorners.some((subArr) => subArr.reduce((a, b) => a + b, 0) !== 0)
@@ -509,7 +581,6 @@ const VeryfiLens = (function () {
509
581
  }
510
582
 
511
583
  releaseCanvas(fullSizeCanvas);
512
- releaseCanvas(boxRef);
513
584
  }
514
585
  }
515
586
  };
@@ -636,14 +707,22 @@ const VeryfiLens = (function () {
636
707
  console.log(
637
708
  `Detector Result: ${detectorResult}, Documents Detected: ${nDocs}`
638
709
  );
639
- for (let i = 0; i < nDocs; i++) {
640
- const offset = i * 4;
641
- coordinates = [
642
- [corners[offset].x, corners[offset].y],
643
- [corners[offset + 1].x, corners[offset + 1].y],
644
- [corners[offset + 2].x, corners[offset + 2].y],
645
- [corners[offset + 3].x, corners[offset + 3].y],
646
- ];
710
+ if (nDocs > 0) {
711
+ for (let i = 0; i < nDocs; i++) {
712
+ const offset = i * 4;
713
+ const coordinates = [
714
+ [corners[offset].x, corners[offset].y],
715
+ [corners[offset + 1].x, corners[offset + 1].y],
716
+ [corners[offset + 2].x, corners[offset + 2].y],
717
+ [corners[offset + 3].x, corners[offset + 3].y],
718
+ ];
719
+
720
+ // Call drawContours with the obtained coordinates
721
+ drawContours(coordinates);
722
+ }
723
+ } else {
724
+ // No documents detected, clear the canvas
725
+ drawContours([]);
647
726
  }
648
727
  }
649
728
 
@@ -706,24 +785,62 @@ const VeryfiLens = (function () {
706
785
  const drawContours = (contours) => {
707
786
  const video = videoRef;
708
787
  const BoxCanvas = boxRef;
709
-
710
788
  if (video && BoxCanvas) {
711
789
  BoxCanvas.width = video.videoWidth;
712
790
  BoxCanvas.height = video.videoHeight;
713
791
  let ctx = BoxCanvas.getContext("2d");
714
-
715
- if (ctx) {
792
+
793
+ if (ctx && contours && contours.length === 4) {
794
+ ctx.clearRect(0, 0, BoxCanvas.width, BoxCanvas.height);
716
795
  ctx.save();
717
796
  ctx.beginPath();
718
- ctx.moveTo(contours[0][0], contours[0][1]);
719
- ctx.lineTo(contours[1][0], contours[1][1]);
720
- ctx.lineTo(contours[2][0], contours[2][1]);
721
- ctx.lineTo(contours[3][0], contours[3][1]);
797
+
798
+ const scaleX = BoxCanvas.width / video.videoWidth;
799
+ const scaleY = BoxCanvas.height / video.videoHeight;
800
+
801
+ // Calculate the bounding box of the contours
802
+ let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
803
+ contours.forEach(([x, y]) => {
804
+ minX = Math.min(minX, x);
805
+ minY = Math.min(minY, y);
806
+ maxX = Math.max(maxX, x);
807
+ maxY = Math.max(maxY, y);
808
+ });
809
+
810
+ // Expand the bounding box slightly to ensure 100% coverage
811
+ const expandFactor = 1;
812
+ const centerX = (minX + maxX) / 2;
813
+ const centerY = (minY + maxY) / 2;
814
+ minX = centerX - (centerX - minX) * expandFactor;
815
+ maxX = centerX + (maxX - centerX) * expandFactor;
816
+ minY = centerY - (centerY - minY) * expandFactor;
817
+ maxY = centerY + (maxY - centerY) * expandFactor;
818
+
819
+ // Ensure the expanded box doesn't go outside the canvas
820
+ minX = Math.max(0, minX * scaleX);
821
+ minY = Math.max(0, minY * scaleY);
822
+ maxX = Math.min(BoxCanvas.width, maxX * scaleX);
823
+ maxY = Math.min(BoxCanvas.height, maxY * scaleY);
824
+
825
+ // Draw the expanded rectangle
826
+ ctx.moveTo(minX, minY);
827
+ ctx.lineTo(maxX, minY);
828
+ ctx.lineTo(maxX, maxY);
829
+ ctx.lineTo(minX, maxY);
830
+ ctx.closePath();
831
+
722
832
  ctx.fillStyle = boxColor;
723
833
  ctx.fill();
724
- setCoordinates(contours);
834
+
835
+ // Update coordinates with the expanded box
836
+ setCoordinates([[minX, minY], [maxX, minY], [maxX, maxY], [minX, maxY]]);
725
837
  setHasCoordinates(true);
726
838
  ctx.restore();
839
+ } else {
840
+ // Handle the case where no valid contours are provided
841
+ ctx.clearRect(0, 0, BoxCanvas.width, BoxCanvas.height);
842
+ setCoordinates([]);
843
+ setHasCoordinates(false);
727
844
  }
728
845
  }
729
846
  };
@@ -1048,6 +1165,22 @@ const VeryfiLens = (function () {
1048
1165
  }
1049
1166
  };
1050
1167
 
1168
+ const startWasmChecks = async (client_id) => {
1169
+ if (!wasmWrapper.loaded) {
1170
+ await wasmWrapper.initialize(client_id);
1171
+ }
1172
+ if (wasmWrapper) {
1173
+ getChecksVideo();
1174
+ requestAnimationFrame(displayVideo);
1175
+ intervalRef = setInterval(() => {
1176
+ sendWasm("Document");
1177
+ }, INTERVAL);
1178
+ return () => {
1179
+ clearInterval(intervalRef);
1180
+ };
1181
+ }
1182
+ };
1183
+
1051
1184
  const setBlurStatus = (variance) => {
1052
1185
  if (variance >= 9) {
1053
1186
  blurStatus = false;
@@ -1219,6 +1352,97 @@ const VeryfiLens = (function () {
1219
1352
  });
1220
1353
  };
1221
1354
 
1355
+ const createCheckCanvases = () => {
1356
+ const container = document.getElementById("veryfi-container");
1357
+ let isDesktop = window.innerWidth >= 1024;
1358
+
1359
+ const setElementStyles = (element, styles) => {
1360
+ Object.assign(element.style, styles);
1361
+ };
1362
+
1363
+ const createElementWithStyles = (tag, id, styles, parent) => {
1364
+ const element = document.createElement(tag);
1365
+ element.id = id;
1366
+ setElementStyles(element, styles);
1367
+ parent.appendChild(element);
1368
+ return element;
1369
+ };
1370
+
1371
+ const updateElementsSize = () => {
1372
+ isDesktop = window.innerWidth >= 1024;
1373
+ const isPortrait = window.innerHeight > window.innerWidth;
1374
+
1375
+ let width, height;
1376
+ if (isDesktop) {
1377
+ width = "500px";
1378
+ height = "100vh";
1379
+ } else if (isPortrait) {
1380
+ width = "100vw";
1381
+ height = "100vh";
1382
+ } else {
1383
+ width = "100vw";
1384
+ height = "100vh";
1385
+ }
1386
+
1387
+ const elementStyles = {
1388
+ width: width,
1389
+ height: height,
1390
+ };
1391
+
1392
+ setElementStyles(container, elementStyles);
1393
+ [cropImgRef, videoRef, boxRef, frameRef].forEach((el) => {
1394
+ if (el) setElementStyles(el, elementStyles);
1395
+ });
1396
+ };
1397
+
1398
+ const generalStyles = {
1399
+ position: "absolute",
1400
+ top: 0,
1401
+ left: 0,
1402
+ };
1403
+
1404
+ cropImgRef = createElementWithStyles(
1405
+ "canvas",
1406
+ "veryfi-crop-img-ref",
1407
+ { ...generalStyles, zIndex: 30 },
1408
+ container
1409
+ );
1410
+
1411
+ frameRef = createElementWithStyles(
1412
+ "canvas",
1413
+ "veryfi-frame-ref",
1414
+ { ...generalStyles, display: "none" },
1415
+ container
1416
+ );
1417
+
1418
+ videoRef = createElementWithStyles(
1419
+ "video",
1420
+ "veryfi-video-ref",
1421
+ { ...generalStyles, objectFit: "cover" },
1422
+ container
1423
+ );
1424
+
1425
+ boxRef = createElementWithStyles(
1426
+ "canvas",
1427
+ "veryfi-box-ref",
1428
+ { ...generalStyles, zIndex: 10 },
1429
+ container
1430
+ );
1431
+
1432
+ updateElementsSize();
1433
+
1434
+ window.addEventListener("resize", updateElementsSize);
1435
+ window.addEventListener("orientationchange", updateElementsSize);
1436
+
1437
+ return {
1438
+ cleanup: () => {
1439
+ window.removeEventListener("resize", updateElementsSize);
1440
+ window.removeEventListener("orientationchange", updateElementsSize);
1441
+ },
1442
+ };
1443
+ };
1444
+
1445
+
1222
1446
  async function convertToRGBA(imageSrc) {
1223
1447
  const img = new Image();
1224
1448
  img.src = URL.createObjectURL(imageSrc);
@@ -1377,6 +1601,34 @@ const VeryfiLens = (function () {
1377
1601
  }
1378
1602
  },
1379
1603
 
1604
+ initChecks: async (session, client_id) => {
1605
+ isDocumentProcess = true;
1606
+ userAgent = navigator.userAgent;
1607
+ device_uuid = new DeviceUUID(userAgent).get();
1608
+
1609
+ if (session) {
1610
+ lensSessionKey = session;
1611
+ createCheckCanvases();
1612
+ videoRef = document.getElementById("veryfi-video-ref");
1613
+ const video = videoRef;
1614
+ video.playsInline = true;
1615
+ video.preload = "metadata";
1616
+ video.autoplay = true;
1617
+ frameRef = document.getElementById("veryfi-frame-ref");
1618
+ boxRef = document.getElementById("veryfi-box-ref");
1619
+ cropImgRef = document.getElementById("veryfi-crop-img-ref");
1620
+ if (client_id) {
1621
+ startWasmChecks(client_id);
1622
+ } else {
1623
+ console.log("No client id provided");
1624
+ return;
1625
+ }
1626
+ } else {
1627
+ console.log("No session token provided");
1628
+ return;
1629
+ }
1630
+ },
1631
+
1380
1632
  startCamera: () => {
1381
1633
  console.log("[EVENT] startCamera");
1382
1634
  startLens();
@@ -1575,6 +1827,7 @@ const VeryfiLens = (function () {
1575
1827
  boxRef && boxRef.remove();
1576
1828
  cropImgRef && cropImgRef.remove();
1577
1829
  setIsDocument(false);
1830
+ // wasmWrapper.releaseCallback()
1578
1831
  },
1579
1832
 
1580
1833
  getDeviceData: async () => {