veryfi-lens-wasm 3.2.1 → 3.2.2

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.
@@ -1,5 +1,21 @@
1
1
  import { createLayoutManager } from './croppedImageLayouts.js';
2
2
  import { state } from '../core/state.js';
3
+ import { shouldRunFieldDetection } from '../core/field-detection-policy.js';
4
+ import {
5
+ applyCropPreviewLayoutRect,
6
+ resolveCropPreviewLayout,
7
+ rememberCropPreviewLayout,
8
+ } from './previewLayout.js';
9
+
10
+ export const hideCropPreview = (previewImg) => {
11
+ state.cropPreviewRevealed = false;
12
+ const img = previewImg ?? document.getElementById('veryfi-crop-img-preview');
13
+ if (!img) {
14
+ return;
15
+ }
16
+ img.style.display = 'none';
17
+ img.style.visibility = 'hidden';
18
+ };
3
19
 
4
20
  export const revealCropPreview = (previewImg) => {
5
21
  state.cropPreviewRevealed = true;
@@ -7,10 +23,17 @@ export const revealCropPreview = (previewImg) => {
7
23
  if (!img) {
8
24
  return;
9
25
  }
10
- img.style.visibility = '';
11
- if (img.style.display === 'none') {
12
- img.style.display = '';
26
+ const cropRef = document.getElementById('veryfi-crop-img-ref');
27
+ const parent = img.parentElement ?? cropRef?.parentElement;
28
+ const layout = resolveCropPreviewLayout(cropRef, parent);
29
+ if (layout) {
30
+ applyCropPreviewLayoutRect(img, layout);
31
+ rememberCropPreviewLayout(cropRef, layout);
32
+ } else if (cropRef?.width > 0 && cropRef?.height > 0) {
33
+ cropRef.setRectangleDisplay?.();
13
34
  }
35
+ img.style.visibility = '';
36
+ img.style.setProperty('display', 'var(--cropped-image-display, block)');
14
37
  };
15
38
 
16
39
  export const createCroppedImage = (container, options = {}) => {
@@ -20,6 +43,7 @@ export const createCroppedImage = (container, options = {}) => {
20
43
 
21
44
  const previewImg = document.createElement("img");
22
45
  previewImg.id = "veryfi-crop-img-preview";
46
+ previewImg.style.display = 'none';
23
47
 
24
48
  let forceLandscapeCheckPreview = options.forceLandscapeCheckPreview || false;
25
49
  const isChecksFlavor = typeof options.isChecksFlavor === 'function' ? options.isChecksFlavor : () => false;
@@ -47,15 +71,12 @@ export const createCroppedImage = (container, options = {}) => {
47
71
 
48
72
  const updatePreviewImg = () => {
49
73
  if (cropImgCanvas.width > 0 && cropImgCanvas.height > 0) {
50
- previewImg.src = cropImgCanvas.toDataURL("image/jpeg", 0.97);
51
- const holdForFieldDetection = state.documentFieldsToDetect?.length > 0;
74
+ const holdForFieldDetection = shouldRunFieldDetection();
52
75
  if (holdForFieldDetection) {
53
- state.cropPreviewRevealed = false;
54
- previewImg.style.visibility = 'hidden';
55
- if (previewImg.style.display === 'none') {
56
- previewImg.style.display = '';
57
- }
58
- } else {
76
+ hideCropPreview(previewImg);
77
+ }
78
+ previewImg.src = cropImgCanvas.toDataURL("image/jpeg", 0.97);
79
+ if (!holdForFieldDetection) {
59
80
  revealCropPreview(previewImg);
60
81
  }
61
82
  }
@@ -1,25 +1,61 @@
1
+ import {
2
+ applyCropPreviewLayoutRect,
3
+ getCropPreviewLayoutRectFromCanvas,
4
+ rememberCropPreviewLayout,
5
+ } from './previewLayout.js';
6
+
7
+ const MOBILE_BREAKPOINT = 768;
8
+ const MOBILE_PREVIEW_SCREEN_FRACTION = 0.8;
9
+
10
+ const isMobileViewport = () =>
11
+ window.innerWidth <= MOBILE_BREAKPOINT || window.innerHeight <= MOBILE_BREAKPOINT;
12
+
13
+ /** Fit crop aspect ratio inside a max box (fraction of viewport on mobile). */
14
+ export const computeRectangleDimensions = (aspectRatio) => {
15
+ const isMobile = isMobileViewport();
16
+ const baseMaxWidth = isMobile
17
+ ? window.innerWidth * MOBILE_PREVIEW_SCREEN_FRACTION
18
+ : Math.min(500, window.innerWidth * 0.9);
19
+ const baseMaxHeight = isMobile
20
+ ? window.innerHeight * MOBILE_PREVIEW_SCREEN_FRACTION
21
+ : window.innerHeight * 0.6;
22
+
23
+ if (aspectRatio > baseMaxWidth / baseMaxHeight) {
24
+ const width = baseMaxWidth;
25
+ return { width, height: width / aspectRatio };
26
+ }
27
+ const height = baseMaxHeight;
28
+ return { width: height * aspectRatio, height };
29
+ };
30
+
31
+ const applyRectangleLayoutFromCanvas = (previewImg, cropImgCanvas, container, defaultStyles) => {
32
+ const layout = getCropPreviewLayoutRectFromCanvas(cropImgCanvas, container);
33
+ if (!layout) {
34
+ return null;
35
+ }
36
+ rememberCropPreviewLayout(cropImgCanvas, layout);
37
+ applyCropPreviewLayoutRect(previewImg, layout, defaultStyles);
38
+ return layout;
39
+ };
40
+
1
41
  export const createLayoutManager = (previewImg, cropImgCanvas, container, defaultStyles) => {
2
42
  let isSpecialDisplayMode = false;
3
43
 
4
44
  const handleResize = () => {
5
45
  if (isSpecialDisplayMode) return;
6
46
 
7
- const isMobile = window.innerWidth <= 768 || window.innerHeight <= 768;
47
+ const isMobile = isMobileViewport();
8
48
  const isLandscape = window.innerWidth > window.innerHeight;
9
49
  const isBottleCapsMode = container.querySelector('#veryfi-bottle-cap-overlay') !== null;
10
50
 
11
51
  if (isBottleCapsMode) {
12
52
  setSquareDisplay();
53
+ } else if (isMobile && cropImgCanvas.width > 0 && cropImgCanvas.height > 0) {
54
+ applyRectangleLayoutFromCanvas(previewImg, cropImgCanvas, container, defaultStyles);
13
55
  } else if (isMobile) {
14
- const mobileMaxWidth = isLandscape ? 'var(--cropped-image-mobile-landscape-max-width, 45vw)' : 'var(--cropped-image-mobile-max-width, 90vw)';
15
- previewImg.style.setProperty('width', 'auto');
16
- previewImg.style.setProperty('height', 'auto');
17
- previewImg.style.setProperty('max-width', mobileMaxWidth);
18
- previewImg.style.setProperty('max-height', '80vh');
19
- previewImg.style.setProperty('position', 'var(--cropped-image-mobile-position, fixed)');
20
- previewImg.style.top = '';
21
- previewImg.style.left = '';
22
- previewImg.style.transform = '';
56
+ previewImg.style.display = 'none';
57
+ } else if (!cropImgCanvas.width || !cropImgCanvas.height) {
58
+ previewImg.style.display = 'none';
23
59
  } else {
24
60
  previewImg.style.setProperty('width', 'auto');
25
61
  previewImg.style.setProperty('height', 'auto');
@@ -49,29 +85,7 @@ export const createLayoutManager = (previewImg, cropImgCanvas, container, defaul
49
85
  };
50
86
 
51
87
  const setRectangleDisplay = () => {
52
- const aspectRatio = cropImgCanvas.width / cropImgCanvas.height;
53
- const baseMaxWidth = Math.min(500, window.innerWidth * 0.9);
54
- const baseMaxHeight = window.innerHeight * 0.6;
55
- let width, height;
56
- if (aspectRatio > baseMaxWidth / baseMaxHeight) {
57
- width = baseMaxWidth;
58
- height = width / aspectRatio;
59
- } else {
60
- height = baseMaxHeight;
61
- width = height * aspectRatio;
62
- }
63
- previewImg.style.setProperty('width', `var(--cropped-image-rectangle-width, ${width}px)`);
64
- previewImg.style.setProperty('height', `var(--cropped-image-rectangle-height, ${height}px)`);
65
- previewImg.style.setProperty('max-width', `var(--cropped-image-rectangle-max-width, ${width}px)`);
66
- previewImg.style.setProperty('max-height', `var(--cropped-image-rectangle-max-height, ${height}px)`);
67
- previewImg.style.setProperty('position', 'var(--cropped-image-rectangle-position, fixed)');
68
- previewImg.style.setProperty('top', 'var(--cropped-image-rectangle-top, 50%)');
69
- previewImg.style.setProperty('left', 'var(--cropped-image-rectangle-left, 50%)');
70
- previewImg.style.setProperty('transform', 'var(--cropped-image-rectangle-transform, translate(-50%, -50%))');
71
- previewImg.style.setProperty('object-fit', 'var(--cropped-image-object-fit, contain)');
72
- previewImg.style.setProperty('display', 'var(--cropped-image-display, block)');
73
- previewImg.style.setProperty('margin', 'var(--cropped-image-margin, auto)');
74
- previewImg.style.setProperty('border-radius', `var(--cropped-image-border-radius-rectangle, ${defaultStyles.borderRadiusRectangle})`);
88
+ applyRectangleLayoutFromCanvas(previewImg, cropImgCanvas, container, defaultStyles);
75
89
  };
76
90
 
77
91
  const setCheckDisplay = () => {
@@ -1,6 +1,7 @@
1
1
  import { state } from '../core/state.js';
2
2
  import { createFieldLabelPill, createWarningPill } from './fieldDetectionPill.js';
3
3
  import { createSafeResizeObserver } from '../core/safe-resize-observer.js';
4
+ import { getPreviewLayoutRect } from './previewLayout.js';
4
5
 
5
6
  const FIELD_LABELS = { vendor: 'Vendor', date: 'Date', total: 'Total' };
6
7
  const ALL_FIELDS = ['vendor', 'date', 'total'];
@@ -22,23 +23,20 @@ const getMissingFields = (fieldDetection) => {
22
23
  };
23
24
 
24
25
  const positionMessages = (el, previewImg) => {
25
- const parent = el.parentElement;
26
- if (!parent || !previewImg) {
26
+ const layout = getPreviewLayoutRect(previewImg);
27
+ if (!layout) {
27
28
  return;
28
29
  }
29
30
 
30
31
  const gap = 12;
31
- const imageTop = previewImg.offsetTop;
32
- const imageBottom = imageTop + previewImg.clientHeight;
33
- const imageCenter = previewImg.offsetLeft + previewImg.clientWidth / 2;
34
32
  const messageHeight = el.offsetHeight || 32;
35
33
 
36
- el.style.left = `${imageCenter}px`;
34
+ el.style.left = `${layout.centerX}px`;
37
35
  el.style.transform = 'translateX(-50%)';
38
36
  el.style.right = 'auto';
39
37
  el.style.bottom = 'auto';
40
- el.style.maxWidth = `min(92%, ${Math.max(previewImg.clientWidth, 280)}px)`;
41
- el.style.top = `${Math.max(imageBottom - messageHeight - gap, imageTop + gap)}px`;
38
+ el.style.maxWidth = `min(92%, ${Math.max(layout.width, 280)}px)`;
39
+ el.style.top = `${Math.max(layout.bottom - messageHeight - gap, layout.top + gap)}px`;
42
40
  };
43
41
 
44
42
  /**
@@ -4,6 +4,7 @@ import {
4
4
  } from './fieldDetectionMessages.js';
5
5
  import { createFieldLabelPill, measureFieldLabelPill } from './fieldDetectionPill.js';
6
6
  import { createSafeResizeObserver } from '../core/safe-resize-observer.js';
7
+ import { getPreviewLayoutRect } from './previewLayout.js';
7
8
 
8
9
  const FIELD_LABELS = { vendor: 'Vendor', date: 'Date', total: 'Total' };
9
10
  const FIELD_GREEN = '#04581a';
@@ -57,10 +58,14 @@ const inflateRect = (rect, scale) => {
57
58
  const boxCornerRadius = (rect) => Math.min(rect.width, rect.height) / 2;
58
59
 
59
60
  const syncOverlayGeometry = (overlay, previewImg) => {
60
- overlay.style.left = `${previewImg.offsetLeft}px`;
61
- overlay.style.top = `${previewImg.offsetTop}px`;
62
- overlay.style.width = `${previewImg.clientWidth}px`;
63
- overlay.style.height = `${previewImg.clientHeight}px`;
61
+ const layout = getPreviewLayoutRect(previewImg);
62
+ if (!layout || layout.width <= 0 || layout.height <= 0) {
63
+ return;
64
+ }
65
+ overlay.style.left = `${layout.left}px`;
66
+ overlay.style.top = `${layout.top}px`;
67
+ overlay.style.width = `${layout.width}px`;
68
+ overlay.style.height = `${layout.height}px`;
64
69
  };
65
70
 
66
71
  const renderOverlay = (overlay, previewImg, fieldDetection) => {
@@ -1,4 +1,5 @@
1
1
  import { createSafeResizeObserver } from '../core/safe-resize-observer.js';
2
+ import { getCropPreviewLayoutRectFromCanvas, getPreviewLayoutRect } from './previewLayout.js';
2
3
 
3
4
  const SCAN_GREEN = '#00AA00';
4
5
  const SCAN_GREEN_GLOW = 'rgba(0, 170, 0, 0.85)';
@@ -159,29 +160,43 @@ const buildScanShell = (imageSrc, borderRadius) => {
159
160
  return shell;
160
161
  };
161
162
 
162
- const syncScanRootGeometry = (root, caption, shell, previewImg) => {
163
- const borderRadius = getComputedStyle(previewImg).borderRadius || '10px';
164
- const shellWidth = previewImg.clientWidth;
165
- const shellHeight = previewImg.clientHeight;
166
- if (shellWidth <= 0 || shellHeight <= 0) {
163
+ const syncScanRootGeometry = (root, caption, shell, previewImg, layoutRect) => {
164
+ const layout =
165
+ layoutRect ??
166
+ getPreviewLayoutRect(previewImg) ??
167
+ getCropPreviewLayoutRectFromCanvas(
168
+ document.getElementById('veryfi-crop-img-ref'),
169
+ previewImg?.parentElement
170
+ );
171
+ if (!layout || layout.width <= 0 || layout.height <= 0) {
167
172
  return;
168
173
  }
169
174
 
170
- shell.style.width = `${shellWidth}px`;
171
- shell.style.height = `${shellHeight}px`;
175
+ const borderRadius = previewImg
176
+ ? getComputedStyle(previewImg).borderRadius || '10px'
177
+ : '10px';
178
+ shell.style.width = `${layout.width}px`;
179
+ shell.style.height = `${layout.height}px`;
172
180
  shell.style.borderRadius = borderRadius;
173
- root.style.width = `${shellWidth}px`;
174
181
 
175
- const captionGap = 8;
176
- const captionHeight = caption.offsetHeight || 20;
177
- root.style.left = `${previewImg.offsetLeft}px`;
178
- root.style.top = `${Math.max(previewImg.offsetTop - captionHeight - captionGap, 4)}px`;
182
+ root.style.position = 'absolute';
183
+ root.style.left = `${layout.left}px`;
184
+ root.style.top = `${layout.top}px`;
185
+ root.style.width = `${layout.width}px`;
186
+ root.style.transform = 'none';
187
+ root.style.margin = '0';
188
+
189
+ caption.style.position = 'absolute';
190
+ caption.style.left = '0';
191
+ caption.style.right = '0';
192
+ caption.style.bottom = '100%';
193
+ caption.style.margin = '0 0 8px 0';
179
194
  };
180
195
 
181
196
  /**
182
197
  * Scanner-style top-to-bottom sweep on the crop preview while OCR runs.
183
198
  */
184
- export const showFieldDetectionScanOverlay = (previewImg, imageSrc) => {
199
+ export const showFieldDetectionScanOverlay = (previewImg, imageSrc, layoutRect = null) => {
185
200
  hideFieldDetectionScanOverlay();
186
201
  if (!previewImg?.parentElement) {
187
202
  return;
@@ -191,6 +206,13 @@ export const showFieldDetectionScanOverlay = (previewImg, imageSrc) => {
191
206
 
192
207
  const src = imageSrc || previewImg.src;
193
208
  const borderRadius = getComputedStyle(previewImg).borderRadius || '10px';
209
+ const resolveScanLayout = () =>
210
+ layoutRect ??
211
+ getCropPreviewLayoutRectFromCanvas(
212
+ document.getElementById('veryfi-crop-img-ref'),
213
+ previewImg.parentElement
214
+ ) ??
215
+ getPreviewLayoutRect(previewImg);
194
216
 
195
217
  const root = document.createElement('div');
196
218
  root.id = 'veryfi-field-detection-scan';
@@ -207,9 +229,13 @@ export const showFieldDetectionScanOverlay = (previewImg, imageSrc) => {
207
229
  previewImg.parentElement.appendChild(root);
208
230
  activeScanRoot = root;
209
231
 
210
- const render = () => syncScanRootGeometry(root, caption, shell, previewImg);
232
+ const render = () => syncScanRootGeometry(root, caption, shell, previewImg, resolveScanLayout());
211
233
  const resizeObserver = createSafeResizeObserver(render);
212
234
  resizeObserver?.observe(previewImg);
235
+ const cropCanvas = document.getElementById('veryfi-crop-img-ref');
236
+ if (cropCanvas) {
237
+ resizeObserver?.observe(cropCanvas);
238
+ }
213
239
  window.addEventListener('resize', render);
214
240
 
215
241
  root._cleanup = () => {
@@ -0,0 +1,98 @@
1
+ import { computeRectangleDimensions } from './croppedImageLayouts.js';
2
+
3
+ /**
4
+ * Visual layout of the crop preview relative to its offset parent.
5
+ * Use instead of offsetLeft/offsetTop when the preview is fixed + transform-centered.
6
+ */
7
+
8
+ /** Layout rect for a centered crop preview from canvas size (preview may stay display:none). */
9
+ export const getCropPreviewLayoutRectFromCanvas = (canvas, parentEl) => {
10
+ if (!canvas?.width || !canvas?.height || !parentEl) {
11
+ return null;
12
+ }
13
+
14
+ const { width, height } = computeRectangleDimensions(canvas.width / canvas.height);
15
+ const parentRect = parentEl.getBoundingClientRect();
16
+ const previewLeft = (window.innerWidth - width) / 2;
17
+ const previewTop = (window.innerHeight - height) / 2;
18
+ const left = previewLeft - parentRect.left;
19
+ const top = previewTop - parentRect.top;
20
+
21
+ return {
22
+ left,
23
+ top,
24
+ width,
25
+ height,
26
+ centerX: left + width / 2,
27
+ bottom: top + height,
28
+ };
29
+ };
30
+
31
+ export const rememberCropPreviewLayout = (canvas, layout) => {
32
+ if (canvas && layout) {
33
+ canvas._previewLayoutRect = layout;
34
+ }
35
+ };
36
+
37
+ export const resolveCropPreviewLayout = (canvas, parentEl) => {
38
+ if (canvas?._previewLayoutRect) {
39
+ return canvas._previewLayoutRect;
40
+ }
41
+ return getCropPreviewLayoutRectFromCanvas(canvas, parentEl);
42
+ };
43
+
44
+ /**
45
+ * Pin the preview to an exact rect (same coordinates as the field-detection scan shell).
46
+ */
47
+ export const applyCropPreviewLayoutRect = (previewImg, layout, defaultStyles = {}) => {
48
+ if (!previewImg || !layout) {
49
+ return null;
50
+ }
51
+
52
+ const borderRadius = defaultStyles.borderRadiusRectangle ?? '5px';
53
+ previewImg.style.setProperty('position', 'absolute');
54
+ previewImg.style.setProperty('left', `${layout.left}px`);
55
+ previewImg.style.setProperty('top', `${layout.top}px`);
56
+ previewImg.style.setProperty('width', `${layout.width}px`);
57
+ previewImg.style.setProperty('height', `${layout.height}px`);
58
+ previewImg.style.setProperty('max-width', `${layout.width}px`);
59
+ previewImg.style.setProperty('max-height', `${layout.height}px`);
60
+ previewImg.style.setProperty('transform', 'none');
61
+ previewImg.style.setProperty('margin', '0');
62
+ previewImg.style.setProperty('box-sizing', 'border-box');
63
+ previewImg.style.setProperty('object-fit', 'var(--cropped-image-object-fit, contain)');
64
+ previewImg.style.setProperty(
65
+ 'border-radius',
66
+ `var(--cropped-image-border-radius-rectangle, ${borderRadius})`
67
+ );
68
+ return layout;
69
+ };
70
+
71
+ export const getPreviewLayoutRect = (previewImg) => {
72
+ const parent = previewImg?.parentElement;
73
+ if (!parent) {
74
+ return null;
75
+ }
76
+
77
+ const previewRect = previewImg.getBoundingClientRect();
78
+ const parentRect = parent.getBoundingClientRect();
79
+ const left = previewRect.left - parentRect.left;
80
+ const top = previewRect.top - parentRect.top;
81
+ const width = previewRect.width;
82
+ const height = previewRect.height;
83
+
84
+ return {
85
+ left,
86
+ top,
87
+ width,
88
+ height,
89
+ centerX: left + width / 2,
90
+ bottom: top + height,
91
+ };
92
+ };
93
+
94
+ /** Wait for layout after setRectangleDisplay / similar style updates. */
95
+ export const afterPreviewLayout = () =>
96
+ new Promise((resolve) => {
97
+ requestAnimationFrame(() => requestAnimationFrame(resolve));
98
+ });
package/core/api.js CHANGED
@@ -3,6 +3,7 @@ import { flattenObject, parseUA, getBrowserData } from './utils.js';
3
3
  import { getDeviceData } from './device.js';
4
4
  import { createSpinner } from '../components/spinner.js';
5
5
  import { API_URLS } from './constants.js';
6
+ import { shouldRunFieldDetection } from './field-detection-policy.js';
6
7
 
7
8
  export const validatePartner = async (client_id) => {
8
9
  try {
@@ -342,7 +343,7 @@ export const handleSubmit = async (image) => {
342
343
  state.updateHandler?.({ status: 'submission_completed' });
343
344
  const result = await response.json();
344
345
 
345
- if (documentFieldsToDetect.length && localFieldDetection) {
346
+ if (shouldRunFieldDetection(lensFlavor) && documentFieldsToDetect.length && localFieldDetection) {
346
347
  result.field_detection = localFieldDetection;
347
348
  }
348
349
 
@@ -104,4 +104,6 @@ export const handleAnydocPageCapture = () => {
104
104
  const showControls = state.enableSubmit || state.packageMode;
105
105
  showControls && state.anydocPageControls.show(pageNum, canAddMore, state.anydocPages, state.cropImgRef);
106
106
  }
107
+
108
+ state.cropImgRef?.setRectangleDisplay?.();
107
109
  };
@@ -7,6 +7,7 @@ import { newStartLens, removeOverlay } from '../lifecycle.js';
7
7
  import { createSubmitButton } from '../../components/submitButton.js';
8
8
  import { createCheckCaptureModal } from '../../components/checkCaptureModal.js';
9
9
  import { createCheckImages } from '../../components/checkImages.js';
10
+ import { revealCropPreview } from '../../components/croppedImage.js';
10
11
 
11
12
  export const handleChecksFrontAccepted = async () => {
12
13
  state.blurStatus = false;
@@ -81,6 +82,8 @@ export const handleChecksFrontAccepted = async () => {
81
82
  },
82
83
  state.enforceBothSides ? null : () => {
83
84
  hideVideoOverlay();
85
+ state.cropImgRef?.setCheckDisplay?.();
86
+ revealCropPreview(state.cropImgRef?.previewImg);
84
87
  if (state.isDocument) {
85
88
  state.submitButton = createSubmitButton(state.videoOverlay.overlay, async () => {
86
89
  if (typeof state.customSubmitHandler === 'function') {
package/core/constants.js CHANGED
@@ -32,7 +32,7 @@ export const setApiEnvironment = (useDevApi) => {
32
32
  };
33
33
 
34
34
  export const DEFAULTS = {
35
- SDK_VERSION: '3.2.1',
35
+ SDK_VERSION: '3.2.2',
36
36
  BLUR_THRESHOLD: 0.7,
37
37
  LCD_THRESHOLD: 0.5,
38
38
  BOX_COLOR: 'rgba(84, 192, 139, 0.6)',
@@ -1,6 +1,7 @@
1
1
  import { state, refs } from './state.js';
2
2
  import { detectDocumentFieldsFromCropCanvas } from './field-detection.js';
3
- import { revealCropPreview } from '../components/croppedImage.js';
3
+ import { shouldRunFieldDetection } from './field-detection-policy.js';
4
+ import { hideCropPreview, revealCropPreview } from '../components/croppedImage.js';
4
5
  import {
5
6
  hideFieldDetectionOverlay,
6
7
  showFieldDetectionOverlay,
@@ -10,11 +11,16 @@ import {
10
11
  hideFieldDetectionScanOverlay,
11
12
  showFieldDetectionScanOverlay,
12
13
  } from '../components/fieldDetectionScanOverlay.js';
14
+ import {
15
+ applyCropPreviewLayoutRect,
16
+ getCropPreviewLayoutRectFromCanvas,
17
+ rememberCropPreviewLayout,
18
+ } from '../components/previewLayout.js';
13
19
 
14
20
  let cropDetectionGeneration = 0;
15
21
 
16
22
  const shouldHoldSubmitForFieldDetection = () =>
17
- state.disableSubmitUntilFieldDetectionDone && state.documentFieldsToDetect?.length > 0;
23
+ state.disableSubmitUntilFieldDetectionDone && shouldRunFieldDetection();
18
24
 
19
25
  export const holdSubmitForFieldDetectionIfNeeded = () => {
20
26
  if (shouldHoldSubmitForFieldDetection()) {
@@ -33,10 +39,10 @@ const releaseSubmitAfterFieldDetection = () => {
33
39
  * on the crop preview before the user submits.
34
40
  */
35
41
  export const onCropPreviewReady = async () => {
36
- const documentFieldsToDetect = state.documentFieldsToDetect;
37
- if (!documentFieldsToDetect?.length) {
42
+ if (!shouldRunFieldDetection()) {
38
43
  return;
39
44
  }
45
+ const documentFieldsToDetect = state.documentFieldsToDetect;
40
46
 
41
47
  const canvas = state.cropImgRef;
42
48
  const previewImg =
@@ -46,13 +52,18 @@ export const onCropPreviewReady = async () => {
46
52
  return;
47
53
  }
48
54
 
55
+ const scanLayout = getCropPreviewLayoutRectFromCanvas(canvas, previewImg?.parentElement);
56
+ rememberCropPreviewLayout(canvas, scanLayout);
57
+ if (previewImg && scanLayout) {
58
+ applyCropPreviewLayoutRect(previewImg, scanLayout);
59
+ }
49
60
  const generation = ++cropDetectionGeneration;
50
61
  const scanImageSrc =
51
62
  previewImg?.src ||
52
63
  (canvas.width && canvas.height ? canvas.toDataURL('image/jpeg', 0.92) : null);
53
64
  if (previewImg && scanImageSrc) {
54
- previewImg.style.visibility = 'hidden';
55
- showFieldDetectionScanOverlay(previewImg, scanImageSrc);
65
+ hideCropPreview(previewImg);
66
+ showFieldDetectionScanOverlay(previewImg, scanImageSrc, scanLayout);
56
67
  }
57
68
 
58
69
  try {
@@ -122,6 +133,24 @@ const revealCropPreviewAndOverlay = (previewImg, fieldDetection) => {
122
133
  }
123
134
  };
124
135
 
136
+ /**
137
+ * Show the crop preview behind blur / document / LCD modals without running field-detection OCR.
138
+ */
139
+ export const showCropPreviewForValidationModal = () => {
140
+ const canvas = state.cropImgRef;
141
+ const previewImg =
142
+ canvas?.previewImg ?? document.getElementById('veryfi-crop-img-preview');
143
+ if (!previewImg || !canvas?.width) {
144
+ return;
145
+ }
146
+ cropDetectionGeneration += 1;
147
+ hideFieldDetectionScanOverlay();
148
+ hideFieldDetectionMessages();
149
+ hideFieldDetectionOverlay();
150
+ canvas.setRectangleDisplay?.();
151
+ revealCropPreview(previewImg);
152
+ };
153
+
125
154
  export const cancelCropFieldDetection = () => {
126
155
  cropDetectionGeneration += 1;
127
156
  hideFieldDetectionScanOverlay();
@@ -0,0 +1,10 @@
1
+ import { state } from './state.js';
2
+
3
+ /** Receipt and long-receipt flavors only (not checks, anydocs, etc.). */
4
+ const FIELD_DETECTION_FLAVORS = new Set(['document', 'long_document']);
5
+
6
+ export const isFieldDetectionFlavor = (flavor = state.lensFlavor) =>
7
+ FIELD_DETECTION_FLAVORS.has(flavor);
8
+
9
+ export const shouldRunFieldDetection = (flavor = state.lensFlavor) =>
10
+ isFieldDetectionFlavor(flavor) && !!state.documentFieldsToDetect?.length;
@@ -1,6 +1,10 @@
1
1
  import { state, refs } from './state.js';
2
2
  import { recognizeCanvas, tesseractDataToOcrPayload } from './tesseract-ocr.js';
3
3
 
4
+ import { shouldRunFieldDetection } from './field-detection-policy.js';
5
+
6
+ export { isFieldDetectionFlavor, shouldRunFieldDetection } from './field-detection-policy.js';
7
+
4
8
  const VALID_FIELDS = new Set(['vendor', 'date', 'total']);
5
9
 
6
10
  export const filterFieldsForRequest = (result, requestedFields) => {
@@ -24,7 +28,12 @@ export const detectDocumentFieldsFromCropCanvas = async (canvas, context = {}) =
24
28
  const documentFieldsToDetect =
25
29
  context.documentFieldsToDetect ?? state.documentFieldsToDetect;
26
30
 
27
- if (!documentFieldsToDetect?.length || !canvas?.width || !canvas?.height) {
31
+ if (
32
+ !shouldRunFieldDetection() ||
33
+ !documentFieldsToDetect?.length ||
34
+ !canvas?.width ||
35
+ !canvas?.height
36
+ ) {
28
37
  return null;
29
38
  }
30
39
  if (!wasmWrapper) {
@@ -68,7 +77,7 @@ export const detectDocumentFieldsFromCropCanvas = async (canvas, context = {}) =
68
77
  };
69
78
 
70
79
  export const initFieldDetectorIfNeeded = () => {
71
- if (!state.documentFieldsToDetect?.length) {
80
+ if (!shouldRunFieldDetection()) {
72
81
  return false;
73
82
  }
74
83
  if (!refs.wasmWrapper?.loaded) {
package/core/lifecycle.js CHANGED
@@ -15,6 +15,8 @@ import { createCheckOverlay } from '../components/checkOverlay.js';
15
15
  import { DEFAULTS } from './constants.js';
16
16
  import { cleanupPrescriptionLabelRecording } from './capture/prescription_label.js';
17
17
  import { installDesktopModeWatchdog, uninstallDesktopModeWatchdog } from '../components/desktopModeModal.js';
18
+ import { shouldRunFieldDetection } from './field-detection-policy.js';
19
+ import { hideCropPreview } from '../components/croppedImage.js';
18
20
 
19
21
  export const newStartLens = async () => {
20
22
  state.started = true;
@@ -75,13 +77,15 @@ export const newStartLens = async () => {
75
77
  if (state.cropImgRef) {
76
78
  const ctx = state.cropImgRef.getContext('2d');
77
79
  ctx?.clearRect(0, 0, state.cropImgRef.width, state.cropImgRef.height);
80
+ delete state.cropImgRef._previewLayoutRect;
81
+ hideCropPreview(state.cropImgRef.previewImg);
78
82
  }
79
83
 
80
84
  if (state.lensFlavor !== "caps" && state.lensFlavor !== "code_strips" && state.lensFlavor !== "prescription_label") {
81
85
  await initializeWasmWrapper();
82
86
  }
83
87
 
84
- if (state.documentFieldsToDetect?.length) {
88
+ if (shouldRunFieldDetection()) {
85
89
  bootstrapTesseract();
86
90
  }
87
91
 
@@ -1,5 +1,6 @@
1
1
  import { createWorker } from 'tesseract.js';
2
2
  import { state } from './state.js';
3
+ import { shouldRunFieldDetection } from './field-detection-policy.js';
3
4
 
4
5
  let workerPromise = null;
5
6
  let workerLang = null;
@@ -16,7 +17,7 @@ export const bootstrapTesseract = (lang = 'eng+spa') => {
16
17
  if (tesseractBootstrapStarted) {
17
18
  return;
18
19
  }
19
- if (!state.documentFieldsToDetect?.length) {
20
+ if (!shouldRunFieldDetection()) {
20
21
  return;
21
22
  }
22
23
 
@@ -1,4 +1,5 @@
1
1
  import { state } from './state.js';
2
+ import { showCropPreviewForValidationModal } from './crop-field-detection.js';
2
3
 
3
4
  export const checkIfDocumentOnCapture = () => {
4
5
  if (!state.isDocument) {
@@ -7,6 +8,7 @@ export const checkIfDocumentOnCapture = () => {
7
8
 
8
9
  if (state.isDocumentModal && state.documentModal) {
9
10
  state.documentModal.show();
11
+ showCropPreviewForValidationModal();
10
12
  state.updateHandler?.({ status: 'not_a_document' });
11
13
  state.cropImgRef?.showBorder?.();
12
14
  } else {
@@ -23,6 +25,7 @@ export const checkIfBlurOnCapture = () => {
23
25
  if (state.isBlurModal && state.blurModal) {
24
26
  state.isBlurrySetter?.(true);
25
27
  state.blurModal.show();
28
+ showCropPreviewForValidationModal();
26
29
  state.updateHandler?.({ status: 'blur_detected' });
27
30
  state.cropImgRef?.showBorder?.();
28
31
  return true;
@@ -42,6 +45,7 @@ export const checkIfLcdOnCapture = () => {
42
45
  state.isLcdSetter?.(true);
43
46
  state.debug_mode && console.log("Showing LCD modal");
44
47
  state.lcdModal.show();
48
+ showCropPreviewForValidationModal();
45
49
  state.updateHandler?.({ status: 'lcd_detected' });
46
50
  state.cropImgRef?.showBorder?.();
47
51
  return true;
@@ -3,6 +3,7 @@ import { drawContours, expandCoordinatesWithMargin } from './drawing.js';
3
3
  import { releaseCanvas, setCoordinates, setHasCoordinates, setBlurStatus } from './utils.js';
4
4
  import { DEFAULTS, WASM } from './constants.js';
5
5
  import { bootstrapTesseract } from './tesseract-ocr.js';
6
+ import { shouldRunFieldDetection } from './field-detection-policy.js';
6
7
 
7
8
  let _autoCaptureFrameCount = 0;
8
9
  let _lastAutoCaptureStatus = null;
@@ -21,7 +22,7 @@ export const bootstrapFieldDetector = () => {
21
22
  if (_fieldDetectorBootstrapStarted) {
22
23
  return;
23
24
  }
24
- if (!state.documentFieldsToDetect?.length || !refs.wasmWrapper?.loaded) {
25
+ if (!shouldRunFieldDetection() || !refs.wasmWrapper?.loaded) {
25
26
  return;
26
27
  }
27
28
  _fieldDetectorBootstrapStarted = true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "veryfi-lens-wasm",
3
- "version": "3.2.1",
3
+ "version": "3.2.2",
4
4
  "description": "Lens For Web SDK with WASM",
5
5
  "main": "veryfi-lens.js",
6
6
  "type": "module",
package/veryfi-lens.js CHANGED
@@ -5,6 +5,7 @@ import { generateDeviceIDifNeeded, setLCDDetectionSettings, setFrontBackDetectio
5
5
  import {
6
6
  detectDocumentFieldsFromCropCanvas,
7
7
  getFieldDetectionResult,
8
+ shouldRunFieldDetection,
8
9
  } from './core/field-detection.js';
9
10
  import { toggleTorchLight, mirrorVideo, switchCamera } from './core/camera.js';
10
11
  import { checkIfDocumentOnCapture } from './core/validation.js';
@@ -284,8 +285,10 @@ const VeryfiLens = {
284
285
  );
285
286
  if (state.documentFieldsToDetect.length) {
286
287
  state.debug_mode && console.log('[FieldDetection] Fields to detect:', state.documentFieldsToDetect);
287
- bootstrapFieldDetector();
288
- bootstrapTesseract();
288
+ if (shouldRunFieldDetection()) {
289
+ bootstrapFieldDetector();
290
+ bootstrapTesseract();
291
+ }
289
292
  }
290
293
  } else {
291
294
  console.warn('documentFieldsToDetect must be an array of vendor/date/total. Ignoring invalid value.');