veryfi-lens-wasm 1.2.4 → 1.2.6

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 +304 -39
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "veryfi-lens-wasm",
3
- "version": "1.2.4",
3
+ "version": "1.2.6",
4
4
  "description": "Lens For Web SDK with WASM",
5
5
  "main": "veryfi-lens.js",
6
6
  "type": "module",
package/veryfi-lens.js CHANGED
@@ -46,6 +46,7 @@ const VeryfiLens = (function () {
46
46
  let userAgent = null;
47
47
  let device_uuid = null;
48
48
  let fullSizeImage;
49
+ let torchTrack = null
49
50
  // let wasmWrapper = null;
50
51
  let finalImage;
51
52
 
@@ -237,6 +238,7 @@ const VeryfiLens = (function () {
237
238
  aspectRatio: isDesktop ? 9 / 16 : 16 / 9,
238
239
  width: { ideal: 3840 },
239
240
  height: { ideal: 2160 },
241
+ advanced: [{ torch: true }]
240
242
  };
241
243
 
242
244
  if (isAndroid) {
@@ -273,6 +275,7 @@ const VeryfiLens = (function () {
273
275
  .catch((err) => {
274
276
  console.log(`[Event] Error: ${err}`);
275
277
  });
278
+ torchTrack = stream.getVideoTracks()[0];
276
279
  } catch (error) {
277
280
  console.error("Error accessing the camera", error);
278
281
  }
@@ -296,6 +299,7 @@ const VeryfiLens = (function () {
296
299
  aspectRatio: isDesktop ? 9 / 16 : 16 / 9,
297
300
  width: { ideal: 2560 },
298
301
  height: { ideal: 1440 },
302
+ advanced: [{ torch: true }]
299
303
  };
300
304
 
301
305
  if (isAndroid) {
@@ -329,10 +333,12 @@ const VeryfiLens = (function () {
329
333
  const video = videoRef;
330
334
  video.srcObject = stream;
331
335
  wasmWrapper.setStitcherCallback(logLongDocument);
336
+ torchTrack = stream.getVideoTracks()[0];
332
337
  })
333
338
  .catch((err) => {
334
339
  console.log(`[Event] Error: ${err}`);
335
340
  });
341
+
336
342
  } catch (error) {
337
343
  console.error("Error accessing the camera", error);
338
344
  }
@@ -369,6 +375,7 @@ const VeryfiLens = (function () {
369
375
  aspectRatio: isDesktop ? 9 / 16 : 16 / 9,
370
376
  width: { ideal: 3840 },
371
377
  height: { ideal: 2160 },
378
+ advanced: [{ torch: true }]
372
379
  };
373
380
 
374
381
  if (useMainCamera) {
@@ -377,16 +384,13 @@ const VeryfiLens = (function () {
377
384
  videoConfig.facingMode = "environment";
378
385
  }
379
386
 
380
- await navigator.mediaDevices
381
- .getUserMedia({ video: videoConfig })
382
- .then((stream) => {
383
- const video = videoRef;
384
- video.srcObject = stream;
385
- wasmWrapper.setDocumentCallback(logDocument);
386
- })
387
- .catch((err) => {
388
- console.log(`[Event] Error: ${err}`);
389
- });
387
+ const stream = await navigator.mediaDevices.getUserMedia({ video: videoConfig });
388
+ const video = videoRef;
389
+ video.srcObject = stream;
390
+ wasmWrapper.setDocumentCallback(logDocument);
391
+
392
+ // Store the video track for torch control
393
+ torchTrack = stream.getVideoTracks()[0];
390
394
  } catch (error) {
391
395
  console.error("Error accessing the camera", error);
392
396
  }
@@ -423,6 +427,7 @@ const VeryfiLens = (function () {
423
427
  aspectRatio: isDesktop ? 9 / 16 : 16 / 9,
424
428
  width: { ideal: 3840 },
425
429
  height: { ideal: 2160 },
430
+ advanced: [{ torch: true }]
426
431
  };
427
432
 
428
433
  if (useMainCamera) {
@@ -436,6 +441,7 @@ const VeryfiLens = (function () {
436
441
  const video = videoRef;
437
442
  video.srcObject = stream;
438
443
  wasmWrapper.setCardDetectorCallback(logCard);
444
+ torchTrack = stream.getVideoTracks()[0];
439
445
  })
440
446
  .catch((err) => {
441
447
  console.log(`[Event] Error: ${err}`);
@@ -448,6 +454,101 @@ const VeryfiLens = (function () {
448
454
  }
449
455
  };
450
456
 
457
+ const getChecksVideo = async () => {
458
+ const isDesktop =
459
+ !/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
460
+ navigator.userAgent
461
+ );
462
+ const isAndroid = /Android/i.test(navigator.userAgent);
463
+ const isLandscape = window.innerWidth > window.innerHeight;
464
+
465
+ if (navigator) {
466
+ try {
467
+ const devices = await navigator.mediaDevices.enumerateDevices();
468
+ console.log(devices);
469
+ const videoDevices = devices.filter(
470
+ (device) => device.kind === "videoinput"
471
+ );
472
+
473
+ let useMainCamera = false;
474
+ let mainCameraDeviceId = null;
475
+ if (isAndroid) {
476
+ for (const device of videoDevices) {
477
+ if (device.label.includes("camera2 0")) {
478
+ console.log("Main camera found", device.deviceId);
479
+ mainCameraDeviceId = device.deviceId;
480
+ useMainCamera = true;
481
+ break;
482
+ }
483
+ }
484
+ }
485
+
486
+ let videoConfig = {
487
+ width: { ideal: 3840 },
488
+ height: { ideal: 2160 },
489
+ advanced: [{ torch: true }]
490
+ };
491
+
492
+ if (isDesktop) {
493
+ // Force portrait mode for desktop
494
+ videoConfig.aspectRatio = 9 / 16;
495
+ } else {
496
+ // For mobile, adapt to device orientation
497
+ videoConfig.aspectRatio = isLandscape ? 16 / 9 : 9 / 16;
498
+ }
499
+
500
+ if (useMainCamera) {
501
+ videoConfig.deviceId = { exact: mainCameraDeviceId };
502
+ } else {
503
+ videoConfig.facingMode = "environment";
504
+ }
505
+
506
+ const stream = await navigator.mediaDevices.getUserMedia({
507
+ video: videoConfig,
508
+ });
509
+ const video = videoRef;
510
+ video.srcObject = stream;
511
+ wasmWrapper.setDocumentCallback(logDocument);
512
+
513
+ // Add event listener to handle orientation changes on mobile
514
+ if (!isDesktop) {
515
+ window.addEventListener("orientationchange", async () => {
516
+ const isNewLandscape = window.innerWidth > window.innerHeight;
517
+ videoConfig.aspectRatio = isNewLandscape ? 16 / 9 : 9 / 16;
518
+ const newStream = await navigator.mediaDevices.getUserMedia({
519
+ video: videoConfig,
520
+ });
521
+ video.srcObject = newStream;
522
+ });
523
+ }
524
+ torchTrack = stream.getVideoTracks()[0];
525
+ } catch (error) {
526
+ console.error("Error accessing the camera", error);
527
+ }
528
+ } else {
529
+ console.log("No navigator available");
530
+ }
531
+ };
532
+
533
+ const toggleTorchLight = async () => {
534
+ if (torchTrack) {
535
+ const capabilities = torchTrack.getCapabilities();
536
+ if (capabilities.torch) {
537
+ try {
538
+ await torchTrack.applyConstraints({
539
+ advanced: [{ torch: !torchTrack.getConstraints().advanced?.[0]?.torch }]
540
+ });
541
+ } catch (err) {
542
+ console.error("Error toggling torch:", err);
543
+ }
544
+ } else {
545
+ console.log("Torch not supported on this device");
546
+ }
547
+ } else {
548
+ console.log("Camera not initialized");
549
+ }
550
+ };
551
+
451
552
  const loadImage = (src) => {
452
553
  return new Promise((resolve, reject) => {
453
554
  const img = new Image();
@@ -466,8 +567,8 @@ const VeryfiLens = (function () {
466
567
  if (videoRef) {
467
568
  let rCorners;
468
569
  const video = videoRef;
469
- let videoHeight = Number(video.videoHeight);
470
- let videoWidth = Number(video.videoWidth);
570
+ let videoHeight = video.videoHeight;
571
+ let videoWidth = video.videoWidth;
471
572
  const fullSizeCanvas = document.createElement("canvas");
472
573
  fullSizeCanvas.width = videoWidth;
473
574
  fullSizeCanvas.height = videoHeight;
@@ -484,9 +585,7 @@ const VeryfiLens = (function () {
484
585
  createImageBitmap(fullSizeImage).then((bitmap) => {
485
586
  fullSizeImage = bitmap;
486
587
  wasmWrapper.processDocument(bitmap, mode);
487
- rCorners = coordinates.map((corner) =>
488
- corner.map((cord) => cord / scale)
489
- );
588
+ rCorners = coordinates.map((corner) => corner);
490
589
  if (
491
590
  rCorners.length &&
492
591
  rCorners.some((subArr) => subArr.reduce((a, b) => a + b, 0) !== 0)
@@ -509,7 +608,6 @@ const VeryfiLens = (function () {
509
608
  }
510
609
 
511
610
  releaseCanvas(fullSizeCanvas);
512
- releaseCanvas(boxRef);
513
611
  }
514
612
  }
515
613
  };
@@ -718,30 +816,58 @@ const VeryfiLens = (function () {
718
816
  BoxCanvas.width = video.videoWidth;
719
817
  BoxCanvas.height = video.videoHeight;
720
818
  let ctx = BoxCanvas.getContext("2d");
721
-
722
- if (ctx) {
723
- // Clear the canvas before drawing new contours
819
+
820
+ if (ctx && contours && contours.length === 4) {
724
821
  ctx.clearRect(0, 0, BoxCanvas.width, BoxCanvas.height);
725
-
726
- if (contours && contours.length === 4) {
727
- ctx.save();
728
- ctx.beginPath();
729
- ctx.moveTo(contours[0][0], contours[0][1]);
730
- ctx.lineTo(contours[1][0], contours[1][1]);
731
- ctx.lineTo(contours[2][0], contours[2][1]);
732
- ctx.lineTo(contours[3][0], contours[3][1]);
733
- ctx.closePath(); // Close the path to complete the contour
734
- ctx.fillStyle = boxColor;
735
- ctx.fill();
736
- setCoordinates(contours);
737
- setHasCoordinates(true);
738
- ctx.restore();
739
- } else {
740
- // Handle the case where no valid contours are provided
741
- // Clear any previous coordinates
742
- setCoordinates([]);
743
- setHasCoordinates(false);
744
- }
822
+ ctx.save();
823
+ ctx.beginPath();
824
+
825
+ const scaleX = BoxCanvas.width / video.videoWidth;
826
+ const scaleY = BoxCanvas.height / video.videoHeight;
827
+
828
+ // Calculate the bounding box of the contours
829
+ let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
830
+ contours.forEach(([x, y]) => {
831
+ minX = Math.min(minX, x);
832
+ minY = Math.min(minY, y);
833
+ maxX = Math.max(maxX, x);
834
+ maxY = Math.max(maxY, y);
835
+ });
836
+
837
+ // Expand the bounding box slightly to ensure 100% coverage
838
+ const expandFactor = 1;
839
+ const centerX = (minX + maxX) / 2;
840
+ const centerY = (minY + maxY) / 2;
841
+ minX = centerX - (centerX - minX) * expandFactor;
842
+ maxX = centerX + (maxX - centerX) * expandFactor;
843
+ minY = centerY - (centerY - minY) * expandFactor;
844
+ maxY = centerY + (maxY - centerY) * expandFactor;
845
+
846
+ // Ensure the expanded box doesn't go outside the canvas
847
+ minX = Math.max(0, minX * scaleX);
848
+ minY = Math.max(0, minY * scaleY);
849
+ maxX = Math.min(BoxCanvas.width, maxX * scaleX);
850
+ maxY = Math.min(BoxCanvas.height, maxY * scaleY);
851
+
852
+ // Draw the expanded rectangle
853
+ ctx.moveTo(minX, minY);
854
+ ctx.lineTo(maxX, minY);
855
+ ctx.lineTo(maxX, maxY);
856
+ ctx.lineTo(minX, maxY);
857
+ ctx.closePath();
858
+
859
+ ctx.fillStyle = boxColor;
860
+ ctx.fill();
861
+
862
+ // Update coordinates with the expanded box
863
+ setCoordinates([[minX, minY], [maxX, minY], [maxX, maxY], [minX, maxY]]);
864
+ setHasCoordinates(true);
865
+ ctx.restore();
866
+ } else {
867
+ // Handle the case where no valid contours are provided
868
+ ctx.clearRect(0, 0, BoxCanvas.width, BoxCanvas.height);
869
+ setCoordinates([]);
870
+ setHasCoordinates(false);
745
871
  }
746
872
  }
747
873
  };
@@ -1066,6 +1192,22 @@ const VeryfiLens = (function () {
1066
1192
  }
1067
1193
  };
1068
1194
 
1195
+ const startWasmChecks = async (client_id) => {
1196
+ if (!wasmWrapper.loaded) {
1197
+ await wasmWrapper.initialize(client_id);
1198
+ }
1199
+ if (wasmWrapper) {
1200
+ getChecksVideo();
1201
+ requestAnimationFrame(displayVideo);
1202
+ intervalRef = setInterval(() => {
1203
+ sendWasm("Document");
1204
+ }, INTERVAL);
1205
+ return () => {
1206
+ clearInterval(intervalRef);
1207
+ };
1208
+ }
1209
+ };
1210
+
1069
1211
  const setBlurStatus = (variance) => {
1070
1212
  if (variance >= 9) {
1071
1213
  blurStatus = false;
@@ -1237,6 +1379,97 @@ const VeryfiLens = (function () {
1237
1379
  });
1238
1380
  };
1239
1381
 
1382
+ const createCheckCanvases = () => {
1383
+ const container = document.getElementById("veryfi-container");
1384
+ let isDesktop = window.innerWidth >= 1024;
1385
+
1386
+ const setElementStyles = (element, styles) => {
1387
+ Object.assign(element.style, styles);
1388
+ };
1389
+
1390
+ const createElementWithStyles = (tag, id, styles, parent) => {
1391
+ const element = document.createElement(tag);
1392
+ element.id = id;
1393
+ setElementStyles(element, styles);
1394
+ parent.appendChild(element);
1395
+ return element;
1396
+ };
1397
+
1398
+ const updateElementsSize = () => {
1399
+ isDesktop = window.innerWidth >= 1024;
1400
+ const isPortrait = window.innerHeight > window.innerWidth;
1401
+
1402
+ let width, height;
1403
+ if (isDesktop) {
1404
+ width = "500px";
1405
+ height = "100vh";
1406
+ } else if (isPortrait) {
1407
+ width = "100vw";
1408
+ height = "100vh";
1409
+ } else {
1410
+ width = "100vw";
1411
+ height = "100vh";
1412
+ }
1413
+
1414
+ const elementStyles = {
1415
+ width: width,
1416
+ height: height,
1417
+ };
1418
+
1419
+ setElementStyles(container, elementStyles);
1420
+ [cropImgRef, videoRef, boxRef, frameRef].forEach((el) => {
1421
+ if (el) setElementStyles(el, elementStyles);
1422
+ });
1423
+ };
1424
+
1425
+ const generalStyles = {
1426
+ position: "absolute",
1427
+ top: 0,
1428
+ left: 0,
1429
+ };
1430
+
1431
+ cropImgRef = createElementWithStyles(
1432
+ "canvas",
1433
+ "veryfi-crop-img-ref",
1434
+ { ...generalStyles, zIndex: 30 },
1435
+ container
1436
+ );
1437
+
1438
+ frameRef = createElementWithStyles(
1439
+ "canvas",
1440
+ "veryfi-frame-ref",
1441
+ { ...generalStyles, display: "none" },
1442
+ container
1443
+ );
1444
+
1445
+ videoRef = createElementWithStyles(
1446
+ "video",
1447
+ "veryfi-video-ref",
1448
+ { ...generalStyles, objectFit: "cover" },
1449
+ container
1450
+ );
1451
+
1452
+ boxRef = createElementWithStyles(
1453
+ "canvas",
1454
+ "veryfi-box-ref",
1455
+ { ...generalStyles, zIndex: 10 },
1456
+ container
1457
+ );
1458
+
1459
+ updateElementsSize();
1460
+
1461
+ window.addEventListener("resize", updateElementsSize);
1462
+ window.addEventListener("orientationchange", updateElementsSize);
1463
+
1464
+ return {
1465
+ cleanup: () => {
1466
+ window.removeEventListener("resize", updateElementsSize);
1467
+ window.removeEventListener("orientationchange", updateElementsSize);
1468
+ },
1469
+ };
1470
+ };
1471
+
1472
+
1240
1473
  async function convertToRGBA(imageSrc) {
1241
1474
  const img = new Image();
1242
1475
  img.src = URL.createObjectURL(imageSrc);
@@ -1395,6 +1628,34 @@ const VeryfiLens = (function () {
1395
1628
  }
1396
1629
  },
1397
1630
 
1631
+ initChecks: async (session, client_id) => {
1632
+ isDocumentProcess = true;
1633
+ userAgent = navigator.userAgent;
1634
+ device_uuid = new DeviceUUID(userAgent).get();
1635
+
1636
+ if (session) {
1637
+ lensSessionKey = session;
1638
+ createCheckCanvases();
1639
+ videoRef = document.getElementById("veryfi-video-ref");
1640
+ const video = videoRef;
1641
+ video.playsInline = true;
1642
+ video.preload = "metadata";
1643
+ video.autoplay = true;
1644
+ frameRef = document.getElementById("veryfi-frame-ref");
1645
+ boxRef = document.getElementById("veryfi-box-ref");
1646
+ cropImgRef = document.getElementById("veryfi-crop-img-ref");
1647
+ if (client_id) {
1648
+ startWasmChecks(client_id);
1649
+ } else {
1650
+ console.log("No client id provided");
1651
+ return;
1652
+ }
1653
+ } else {
1654
+ console.log("No session token provided");
1655
+ return;
1656
+ }
1657
+ },
1658
+
1398
1659
  startCamera: () => {
1399
1660
  console.log("[EVENT] startCamera");
1400
1661
  startLens();
@@ -1617,6 +1878,10 @@ const VeryfiLens = (function () {
1617
1878
  releaseWasm: () => {
1618
1879
  wasmWrapper.release();
1619
1880
  },
1881
+
1882
+ toggleTorch: () => {
1883
+ toggleTorchLight()
1884
+ }
1620
1885
  };
1621
1886
  })();
1622
1887