veryfi-lens-wasm 1.2.2 → 1.2.3

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.3",
4
4
  "description": "Lens For Web SDK with WASM",
5
5
  "main": "veryfi-lens.js",
6
6
  "type": "module",
package/veryfi-lens.js CHANGED
@@ -697,7 +697,7 @@ const VeryfiLens = (function () {
697
697
  if (cardData.status == "AutoCaptureResultDone") {
698
698
  ShouldProcessCC = false;
699
699
  wasmWrapper.resetAutoCapture(true, true, true, true);
700
- wasmWrapper.releaseCallback();
700
+ // wasmWrapper.releaseCallback();
701
701
  }
702
702
  }
703
703
  }
@@ -1020,9 +1020,11 @@ const VeryfiLens = (function () {
1020
1020
  };
1021
1021
 
1022
1022
  const startUploadWasm = async (client_id) => {
1023
- await wasmWrapper.initialize(client_id);
1023
+ if (!wasmWrapper.loaded) {
1024
+ await wasmWrapper.initialize(client_id);
1025
+ }
1024
1026
  if (wasmWrapper) {
1025
- await wasmWrapper.setDocumentCallback(logDocument);
1027
+ await wasmWrapper.setDocumentCallback(logDocument);
1026
1028
  }
1027
1029
  };
1028
1030
 
@@ -1217,6 +1219,23 @@ const VeryfiLens = (function () {
1217
1219
  });
1218
1220
  };
1219
1221
 
1222
+ async function convertToRGBA(imageSrc) {
1223
+ const img = new Image();
1224
+ img.src = URL.createObjectURL(imageSrc);
1225
+ await new Promise((resolve) => {
1226
+ img.onload = resolve;
1227
+ });
1228
+
1229
+ const canvas = document.createElement("canvas");
1230
+ const ctx = canvas.getContext("2d");
1231
+ canvas.width = img.width;
1232
+ canvas.height = img.height;
1233
+ ctx.drawImage(img, 0, 0);
1234
+
1235
+ const rgbaImageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
1236
+ return rgbaImageData;
1237
+ }
1238
+
1220
1239
  return {
1221
1240
  init: async (session, client_id) => {
1222
1241
  userAgent = navigator.userAgent;
@@ -1402,7 +1421,7 @@ const VeryfiLens = (function () {
1402
1421
  finalImage = await cropWasm();
1403
1422
  setImage && setImage(finalImage);
1404
1423
  stopWasm();
1405
- wasmWrapper.releaseCallback();
1424
+ // wasmWrapper.releaseCallback();
1406
1425
  setIsEditing && setIsEditing(true);
1407
1426
  return finalImage;
1408
1427
  },
@@ -1413,48 +1432,51 @@ const VeryfiLens = (function () {
1413
1432
  setImage && setImage(finalImage);
1414
1433
  stopWasm();
1415
1434
  if (hasCoordinates) setIsDocument(true);
1416
- wasmWrapper.releaseCallback();
1435
+ // wasmWrapper.releaseCallback();
1417
1436
  setIsEditing && setIsEditing(true);
1418
1437
  return finalImage;
1419
1438
  },
1420
1439
 
1421
1440
  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];
1441
+ const RGBAImage = await convertToRGBA(imageData);
1442
+ return await createImageBitmap(RGBAImage).then(async (bitmap) => {
1443
+ if (wasmWrapper.documentDetectorLoaded) {
1444
+ const wasmOutput = await wasmWrapper.cropDocument(bitmap);
1445
+ // console.log(wasmOutput, "output");
1446
+ const { data, blurLevel, outputHeight, outputWidth } = wasmOutput;
1447
+
1448
+ if (outputWidth > 0 && outputHeight > 0) {
1449
+ const width = outputWidth;
1450
+ const height = outputHeight;
1451
+ const cropImgCanvas = document.createElement("canvas");
1452
+ cropImgCanvas.height = height;
1453
+ cropImgCanvas.width = width;
1454
+ const ctx = cropImgCanvas.getContext("2d");
1455
+ const imageData = new ImageData(data, width, height);
1456
+ ctx.putImageData(imageData, 0, 0);
1457
+ setBlurStatus(blurLevel);
1458
+ const imgString = cropImgCanvas.toDataURL("image/jpeg");
1459
+ image = cropImgCanvas;
1460
+ setIsDocument(true);
1461
+ coordinates = [];
1462
+ return imgString.split("data:image/jpeg;base64,")[1];
1463
+ } else {
1464
+ // Return the original, uncropped image
1465
+ return new Promise((resolve, reject) => {
1466
+ setIsDocument(false);
1467
+ const reader = new FileReader();
1468
+ reader.readAsDataURL(imageData);
1469
+ reader.onload = function () {
1470
+ const base64Image = reader.result.split(",")[1];
1471
+ resolve(base64Image);
1472
+ };
1473
+ reader.onerror = function (error) {
1474
+ reject(error);
1475
+ };
1476
+ });
1477
+ }
1443
1478
  } 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
- });
1479
+ console.log("Wasm not loaded");
1458
1480
  }
1459
1481
  });
1460
1482
  },
@@ -1552,7 +1574,6 @@ const VeryfiLens = (function () {
1552
1574
  frameRef && frameRef.remove();
1553
1575
  boxRef && boxRef.remove();
1554
1576
  cropImgRef && cropImgRef.remove();
1555
- // wasmWrapper && wasmWrapper.release();
1556
1577
  setIsDocument(false);
1557
1578
  },
1558
1579
 
@@ -1576,7 +1597,7 @@ const VeryfiLens = (function () {
1576
1597
  },
1577
1598
  releaseWasm: () => {
1578
1599
  wasmWrapper.release();
1579
- }
1600
+ },
1580
1601
  };
1581
1602
  })();
1582
1603