veryfi-lens-wasm 1.2.2 → 1.2.4

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "veryfi-lens-wasm",
3
- "version": "1.2.2",
3
+ "version": "1.2.4",
4
4
  "description": "Lens For Web SDK with WASM",
5
5
  "main": "veryfi-lens.js",
6
6
  "type": "module",
package/veryfi-lens.js CHANGED
@@ -636,14 +636,22 @@ const VeryfiLens = (function () {
636
636
  console.log(
637
637
  `Detector Result: ${detectorResult}, Documents Detected: ${nDocs}`
638
638
  );
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
- ];
639
+ if (nDocs > 0) {
640
+ for (let i = 0; i < nDocs; i++) {
641
+ const offset = i * 4;
642
+ const coordinates = [
643
+ [corners[offset].x, corners[offset].y],
644
+ [corners[offset + 1].x, corners[offset + 1].y],
645
+ [corners[offset + 2].x, corners[offset + 2].y],
646
+ [corners[offset + 3].x, corners[offset + 3].y],
647
+ ];
648
+
649
+ // Call drawContours with the obtained coordinates
650
+ drawContours(coordinates);
651
+ }
652
+ } else {
653
+ // No documents detected, clear the canvas
654
+ drawContours([]);
647
655
  }
648
656
  }
649
657
 
@@ -697,7 +705,7 @@ const VeryfiLens = (function () {
697
705
  if (cardData.status == "AutoCaptureResultDone") {
698
706
  ShouldProcessCC = false;
699
707
  wasmWrapper.resetAutoCapture(true, true, true, true);
700
- wasmWrapper.releaseCallback();
708
+ // wasmWrapper.releaseCallback();
701
709
  }
702
710
  }
703
711
  }
@@ -706,24 +714,34 @@ const VeryfiLens = (function () {
706
714
  const drawContours = (contours) => {
707
715
  const video = videoRef;
708
716
  const BoxCanvas = boxRef;
709
-
710
717
  if (video && BoxCanvas) {
711
718
  BoxCanvas.width = video.videoWidth;
712
719
  BoxCanvas.height = video.videoHeight;
713
720
  let ctx = BoxCanvas.getContext("2d");
714
721
 
715
722
  if (ctx) {
716
- ctx.save();
717
- 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]);
722
- ctx.fillStyle = boxColor;
723
- ctx.fill();
724
- setCoordinates(contours);
725
- setHasCoordinates(true);
726
- ctx.restore();
723
+ // Clear the canvas before drawing new contours
724
+ 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
+ }
727
745
  }
728
746
  }
729
747
  };
@@ -1020,9 +1038,11 @@ const VeryfiLens = (function () {
1020
1038
  };
1021
1039
 
1022
1040
  const startUploadWasm = async (client_id) => {
1023
- await wasmWrapper.initialize(client_id);
1041
+ if (!wasmWrapper.loaded) {
1042
+ await wasmWrapper.initialize(client_id);
1043
+ }
1024
1044
  if (wasmWrapper) {
1025
- await wasmWrapper.setDocumentCallback(logDocument);
1045
+ await wasmWrapper.setDocumentCallback(logDocument);
1026
1046
  }
1027
1047
  };
1028
1048
 
@@ -1217,6 +1237,23 @@ const VeryfiLens = (function () {
1217
1237
  });
1218
1238
  };
1219
1239
 
1240
+ async function convertToRGBA(imageSrc) {
1241
+ const img = new Image();
1242
+ img.src = URL.createObjectURL(imageSrc);
1243
+ await new Promise((resolve) => {
1244
+ img.onload = resolve;
1245
+ });
1246
+
1247
+ const canvas = document.createElement("canvas");
1248
+ const ctx = canvas.getContext("2d");
1249
+ canvas.width = img.width;
1250
+ canvas.height = img.height;
1251
+ ctx.drawImage(img, 0, 0);
1252
+
1253
+ const rgbaImageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
1254
+ return rgbaImageData;
1255
+ }
1256
+
1220
1257
  return {
1221
1258
  init: async (session, client_id) => {
1222
1259
  userAgent = navigator.userAgent;
@@ -1402,7 +1439,7 @@ const VeryfiLens = (function () {
1402
1439
  finalImage = await cropWasm();
1403
1440
  setImage && setImage(finalImage);
1404
1441
  stopWasm();
1405
- wasmWrapper.releaseCallback();
1442
+ // wasmWrapper.releaseCallback();
1406
1443
  setIsEditing && setIsEditing(true);
1407
1444
  return finalImage;
1408
1445
  },
@@ -1413,48 +1450,51 @@ const VeryfiLens = (function () {
1413
1450
  setImage && setImage(finalImage);
1414
1451
  stopWasm();
1415
1452
  if (hasCoordinates) setIsDocument(true);
1416
- wasmWrapper.releaseCallback();
1453
+ // wasmWrapper.releaseCallback();
1417
1454
  setIsEditing && setIsEditing(true);
1418
1455
  return finalImage;
1419
1456
  },
1420
1457
 
1421
1458
  captureUploaded: async (imageData) => {
1422
- return await createImageBitmap(imageData).then(async (bitmap) => {
1423
- const wasmOutput = await wasmWrapper.cropDocument(bitmap);
1424
- const { data, blurLevel, outputHeight, outputWidth } = wasmOutput;
1425
- // console.log(outputWidth, outputHeight, "output")
1426
-
1427
- if (outputWidth > 0 && outputHeight > 0) {
1428
- const width = outputWidth;
1429
- const height = outputHeight;
1430
- const cropImgCanvas = document.createElement("canvas");
1431
- cropImgCanvas.height = height;
1432
- cropImgCanvas.width = width;
1433
- const ctx = cropImgCanvas.getContext("2d");
1434
- const imageData = new ImageData(data, width, height);
1435
- ctx.putImageData(imageData, 0, 0);
1436
- setBlurStatus(blurLevel);
1437
- wasmWrapper.releaseCallback()
1438
- const imgString = cropImgCanvas.toDataURL("image/jpeg");
1439
- image = cropImgCanvas;
1440
- setIsDocument(true);
1441
- coordinates = [];
1442
- return imgString.split("data:image/jpeg;base64,")[1];
1459
+ const RGBAImage = await convertToRGBA(imageData);
1460
+ return await createImageBitmap(RGBAImage).then(async (bitmap) => {
1461
+ if (wasmWrapper.documentDetectorLoaded) {
1462
+ const wasmOutput = await wasmWrapper.cropDocument(bitmap);
1463
+ // console.log(wasmOutput, "output");
1464
+ const { data, blurLevel, outputHeight, outputWidth } = wasmOutput;
1465
+
1466
+ if (outputWidth > 0 && outputHeight > 0) {
1467
+ const width = outputWidth;
1468
+ const height = outputHeight;
1469
+ const cropImgCanvas = document.createElement("canvas");
1470
+ cropImgCanvas.height = height;
1471
+ cropImgCanvas.width = width;
1472
+ const ctx = cropImgCanvas.getContext("2d");
1473
+ const imageData = new ImageData(data, width, height);
1474
+ ctx.putImageData(imageData, 0, 0);
1475
+ setBlurStatus(blurLevel);
1476
+ const imgString = cropImgCanvas.toDataURL("image/jpeg");
1477
+ image = cropImgCanvas;
1478
+ setIsDocument(true);
1479
+ coordinates = [];
1480
+ return imgString.split("data:image/jpeg;base64,")[1];
1481
+ } else {
1482
+ // Return the original, uncropped image
1483
+ return new Promise((resolve, reject) => {
1484
+ setIsDocument(false);
1485
+ const reader = new FileReader();
1486
+ reader.readAsDataURL(imageData);
1487
+ reader.onload = function () {
1488
+ const base64Image = reader.result.split(",")[1];
1489
+ resolve(base64Image);
1490
+ };
1491
+ reader.onerror = function (error) {
1492
+ reject(error);
1493
+ };
1494
+ });
1495
+ }
1443
1496
  } else {
1444
- // Return the original, uncropped image
1445
- return new Promise((resolve, reject) => {
1446
- setIsDocument(false);
1447
- const reader = new FileReader();
1448
- reader.readAsDataURL(imageData);
1449
- reader.onload = function () {
1450
- const base64Image = reader.result.split(",")[1];
1451
- resolve(base64Image);
1452
- };
1453
- reader.onerror = function (error) {
1454
- reject(error);
1455
- };
1456
- // stopWasm()
1457
- });
1497
+ console.log("Wasm not loaded");
1458
1498
  }
1459
1499
  });
1460
1500
  },
@@ -1552,8 +1592,8 @@ const VeryfiLens = (function () {
1552
1592
  frameRef && frameRef.remove();
1553
1593
  boxRef && boxRef.remove();
1554
1594
  cropImgRef && cropImgRef.remove();
1555
- // wasmWrapper && wasmWrapper.release();
1556
1595
  setIsDocument(false);
1596
+ // wasmWrapper.releaseCallback()
1557
1597
  },
1558
1598
 
1559
1599
  getDeviceData: async () => {
@@ -1576,7 +1616,7 @@ const VeryfiLens = (function () {
1576
1616
  },
1577
1617
  releaseWasm: () => {
1578
1618
  wasmWrapper.release();
1579
- }
1619
+ },
1580
1620
  };
1581
1621
  })();
1582
1622