veryfi-lens-wasm 3.5.2 → 3.5.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/README.md CHANGED
@@ -126,6 +126,7 @@ VeryfiLens.onFailure((error) => { /* error occurred */ });
126
126
  | `mirrorButton` | `boolean` | `true` | Show mirror/flip video button |
127
127
  | `mirrorOnDesktop` | `boolean` | `true` | Auto-mirror video on desktop (front-facing webcam) |
128
128
  | `exitButton` | `boolean` | `false` | Show exit/close button |
129
+ | `showCameraPermissionDeniedMessage` | `boolean` | `true` | Show the in-lens message when camera access is denied. Set `false` to render it on your own error screen. A denial always hides every on-camera control and is reported via `onUpdate` as `{ status: "camera_permission_denied", message, error }` (never via `onFailure`) |
129
130
  | `onClose` | `function` | — | Callback when lens is closed |
130
131
  | `onCloseRedirectUrl` | `string` | — | URL to redirect to on close |
131
132
  | `boxColor` | `string` | `"rgba(84, 192, 139, 0.6)"` | Document detection bounding box color |
@@ -200,6 +201,8 @@ VeryfiLens.onFailure((error) => { /* error occurred */ });
200
201
 
201
202
  After each accepted capture, the `document`, `anydocs`, and `prescription_label` flavors show a unified **review gallery** where the user can review, retake, delete, add pages/documents, and submit. For single-document/single-page captures it collapses to a simple Retake / Submit preview. On submit, one package (a zip of its pages) is uploaded per document via `/api/document`, then `finishBatch()` runs.
202
203
 
204
+ When `multiSubmission` is set or `maxDocuments` is not `1`, `init()` asks the backend for a reusable session so every document in the batch can be posted with it; `finishBatch()` then releases that session once the last document is submitted.
205
+
203
206
  | Option | Type | Default | Description |
204
207
  |---|---|---|---|
205
208
  | `maxPagesPerDocument` | `number\|null` | flavor-specific | Max pages per document (`null` = unlimited). `1` hides "Add Page". Defaults: document `1`, anydocs `null`, prescription boxes = box max sides |
@@ -211,6 +214,9 @@ After each accepted capture, the `document`, `anydocs`, and `prescription_label`
211
214
  | `reviewDoneText` | `string` | `"Done"` | Done action text |
212
215
  | `reviewRetakeText` | `string` | `"Retake"` | Retake action text |
213
216
  | `reviewDeleteText` | `string` | `"Delete"` | Delete action text |
217
+ | `reviewShowQualityBadges` | `boolean` | `true` | Badge pages that failed a quality check. Applies to multi-document frames, which are committed with the failing pages flagged instead of blocked |
218
+ | `reviewIssueBlurText` | `string` | `"Blurry"` | Badge text for a blurry page |
219
+ | `reviewIssueLcdText` | `string` | `"Screen"` | Badge text for a screen/LCD page |
214
220
  | `reviewSubmitButtonColor` | `string` | `"#00AA00"` | Submit button color (also used as the crop editor accent) |
215
221
  | `reviewSubmitButtonTextColor` | `string` | `"#FFFFFF"` | Submit button text color (also the crop editor Apply button) |
216
222
 
@@ -286,6 +292,7 @@ After each accepted capture, the `document`, `anydocs`, and `prescription_label`
286
292
  | `lcdModalMessage` | `string` | `"LCD screen detected. Please capture a printed document instead"` |
287
293
  | `submitButtonText` | `string` | `"Submit Anyway"` |
288
294
  | `retakeButtonText` | `string` | `"Retake a picture"` |
295
+ | `cameraPermissionDeniedMessage` | `string` | `"Camera access was blocked. Please allow camera access in your browser settings and try again."` |
289
296
  | `checkEnforcedModalMessage` | `string` | `"Please flip the check and capture the back side"` |
290
297
  | `checkOptionalModalMessage` | `string` | `"Do you want to capture the back side of the check?"` |
291
298
  | `wrongSideFrontMessage` | `string` | `"This looks like the back of the check..."` |
@@ -0,0 +1,76 @@
1
+ import { LIQUID_GLASS } from './liquid-glass.js';
2
+
3
+ const STYLE_ID = 'veryfi-camera-permission-message-styles';
4
+ const BODY_CLASS = 'veryfi-camera-permission-denied';
5
+ const PANEL_ID = 'veryfi-camera-permission-message';
6
+ const BLOCKED_CONTROL_IDS = [
7
+ 'veryfi-capture-button',
8
+ 'veryfi-stitching-button',
9
+ 'veryfi-torch-button',
10
+ 'veryfi-switch-camera-button',
11
+ 'veryfi-mirror-button',
12
+ 'veryfi-prescription-overlay-toggle',
13
+ ];
14
+
15
+ const ensureStyles = () => {
16
+ if (document.getElementById(STYLE_ID)) return;
17
+ const style = document.createElement('style');
18
+ style.id = STYLE_ID;
19
+ const selector = BLOCKED_CONTROL_IDS.map((id) => `body.${BODY_CLASS} #${id}`).join(',\n');
20
+ style.textContent = `${selector} {\n display: none !important;\n}`;
21
+ document.head.appendChild(style);
22
+ };
23
+
24
+ export const suppressOnCameraControls = () => {
25
+ ensureStyles();
26
+ document.body.classList.add(BODY_CLASS);
27
+ };
28
+
29
+ export const restoreOnCameraControls = () => {
30
+ document.body.classList.remove(BODY_CLASS);
31
+ };
32
+
33
+ export const createCameraPermissionMessage = (container, options = {}) => {
34
+ const panel = document.createElement('div');
35
+ panel.id = PANEL_ID;
36
+ panel.setAttribute('role', 'alert');
37
+ panel.textContent = options.message || '';
38
+
39
+ Object.assign(panel.style, {
40
+ position: 'absolute',
41
+ top: '50%',
42
+ left: '50%',
43
+ transform: 'translate(-50%, -50%)',
44
+ width: '300px',
45
+ maxWidth: 'calc(100vw - 48px)',
46
+ padding: '22px 24px',
47
+ boxSizing: 'border-box',
48
+ backgroundColor: LIQUID_GLASS.panelBackground,
49
+ backdropFilter: LIQUID_GLASS.blur,
50
+ WebkitBackdropFilter: LIQUID_GLASS.blur,
51
+ border: LIQUID_GLASS.border,
52
+ boxShadow: LIQUID_GLASS.panelBoxShadow,
53
+ color: LIQUID_GLASS.panelText,
54
+ borderRadius: '20px',
55
+ fontFamily: "system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif",
56
+ fontSize: '15px',
57
+ lineHeight: '1.45',
58
+ textAlign: 'center',
59
+ whiteSpace: 'pre-line',
60
+ zIndex: '1006',
61
+ pointerEvents: 'auto',
62
+ });
63
+
64
+ container.appendChild(panel);
65
+
66
+ return {
67
+ element: panel,
68
+ show: () => { panel.style.display = 'block'; },
69
+ hide: () => { panel.style.display = 'none'; },
70
+ remove: () => {
71
+ if (panel.parentNode) {
72
+ panel.parentNode.removeChild(panel);
73
+ }
74
+ },
75
+ };
76
+ };
@@ -99,6 +99,8 @@ export const createReviewGallery = (container, callbacks = {}, options = {}) =>
99
99
  pageLabels: Array.isArray(options.pageLabels) ? options.pageLabels : null,
100
100
  allowAddPage: options.allowAddPage !== false,
101
101
  allowAddDocument: options.allowAddDocument !== false,
102
+ showQualityBadges: options.showQualityBadges !== false,
103
+ issueLabels: options.issueLabels || { blur: 'Blurry', lcd: 'Screen' },
102
104
  showDocumentStrip: options.showDocumentStrip !== false,
103
105
  showClose: options.showClose !== false,
104
106
  ...options,
@@ -138,11 +140,14 @@ export const createReviewGallery = (container, callbacks = {}, options = {}) =>
138
140
 
139
141
  // Field-detection visuals for a focused image page (receipt flow): the scanning
140
142
  // animation runs while OCR is in flight, then vendor/date/total boxes are drawn
141
- // over the image. Tracked by page id so a superseded scan doesn't paint a stale
142
- // result. detailImgEl/detailPage point at the currently rendered detail image.
143
- let fieldScan = { active: false, result: null, pageId: null };
143
+ // over the image. Results live on the page itself (page.fieldDetection), so a
144
+ // multi-document frame can have one per page and switching pages shows the
145
+ // right boxes; this set only tracks which pages are still scanning.
146
+ const scanningPageIds = new Set();
144
147
  let detailImgEl = null;
145
148
  let detailPage = null;
149
+ // The quality badge drawn over the detail image, kept so a resize can re-pin it.
150
+ let detailIssueBadge = null;
146
151
  let submitting = false;
147
152
  let loadingOverlayEl = null;
148
153
  let loadingMessageEl = null;
@@ -152,21 +157,20 @@ export const createReviewGallery = (container, callbacks = {}, options = {}) =>
152
157
  hideFieldDetectionOverlay();
153
158
  };
154
159
 
155
- // Paint the current field-scan state (scan animation or result boxes) onto the
156
- // detail image once it is laid out. No-op unless the detail image matches the
157
- // tracked page.
160
+ // Paint the field-detection state of the focused page (scan animation or result
161
+ // boxes) onto the detail image once it is laid out.
158
162
  const applyFieldDetectionToDetail = () => {
159
163
  clearFieldOverlays();
160
164
  const imgEl = detailImgEl;
161
165
  const page = detailPage;
162
166
  if (!imgEl || !page || page.kind === 'video') return;
163
- if (!fieldScan.active && !fieldScan.result) return;
164
- if (fieldScan.pageId && page.id !== fieldScan.pageId) return;
167
+ const scanning = scanningPageIds.has(page.id);
168
+ if (!scanning && !page.fieldDetection) return;
165
169
  const run = () => {
166
170
  if (imgEl !== detailImgEl) return;
167
- if (fieldScan.result) {
168
- showFieldDetectionOverlay(imgEl, fieldScan.result);
169
- } else if (fieldScan.active) {
171
+ if (page.fieldDetection) {
172
+ showFieldDetectionOverlay(imgEl, page.fieldDetection);
173
+ } else if (scanning) {
170
174
  showFieldDetectionScanOverlay(
171
175
  imgEl,
172
176
  `data:image/jpeg;base64,${page.data}`,
@@ -216,7 +220,12 @@ export const createReviewGallery = (container, callbacks = {}, options = {}) =>
216
220
  };
217
221
 
218
222
  applyGalleryLayout();
219
- const onResize = () => applyGalleryLayout();
223
+ const onResize = () => {
224
+ applyGalleryLayout();
225
+ if (detailIssueBadge) {
226
+ placeIssueBadgeOverImage(detailIssueBadge.el, detailIssueBadge.imgEl);
227
+ }
228
+ };
220
229
  window.addEventListener('resize', onResize);
221
230
 
222
231
  // Top bar: back on the left, page actions on the right.
@@ -891,6 +900,26 @@ export const createReviewGallery = (container, callbacks = {}, options = {}) =>
891
900
  detailPage = page;
892
901
  applyFieldDetectionToDetail();
893
902
 
903
+ const issueBadge = buildIssueBadge(page, { large: true });
904
+ if (issueBadge) {
905
+ // Hidden until measured so it never flashes in the wrong spot.
906
+ issueBadge.style.visibility = 'hidden';
907
+ media.appendChild(issueBadge);
908
+ const place = () => {
909
+ placeIssueBadgeOverImage(issueBadge, thumb);
910
+ issueBadge.style.visibility = 'visible';
911
+ };
912
+ if (thumb.tagName !== 'VIDEO' && thumb.complete && thumb.naturalWidth > 0) {
913
+ requestAnimationFrame(place);
914
+ } else {
915
+ thumb.addEventListener('load', () => requestAnimationFrame(place), { once: true });
916
+ thumb.addEventListener('loadedmetadata', () => requestAnimationFrame(place), {
917
+ once: true,
918
+ });
919
+ }
920
+ detailIssueBadge = { el: issueBadge, imgEl: thumb };
921
+ }
922
+
894
923
  return cell;
895
924
  };
896
925
 
@@ -898,6 +927,56 @@ export const createReviewGallery = (container, callbacks = {}, options = {}) =>
898
927
  // 1-based page number.
899
928
  const pageLabelFor = (index) => (o.pageLabels && o.pageLabels[index]) || String(index + 1);
900
929
 
930
+ // Badge marking a page the quality checks flagged. Multi-document frames
931
+ // commit every crop and flag the bad ones here rather than blocking the whole
932
+ // capture, so the user can retake just the page that needs it.
933
+ const buildIssueBadge = (page, { large = false } = {}) => {
934
+ if (!o.showQualityBadges) return null;
935
+ const labels = (page?.issues || [])
936
+ .map((issue) => o.issueLabels[issue])
937
+ .filter(Boolean);
938
+ if (!labels.length) return null;
939
+
940
+ const badge = document.createElement('span');
941
+ badge.textContent = labels.join(' · ');
942
+ Object.assign(badge.style, {
943
+ position: 'absolute',
944
+ background: 'var(--veryfi-danger-color, #C62828)',
945
+ color: 'white',
946
+ whiteSpace: 'nowrap',
947
+ pointerEvents: 'none',
948
+ ...(large
949
+ ? {
950
+ padding: '7px 16px',
951
+ borderRadius: '16px',
952
+ fontSize: '16px',
953
+ fontWeight: '700',
954
+ // Sits over the capture, which can be light or busy.
955
+ boxShadow: '0 2px 8px rgba(0, 0, 0, 0.35)',
956
+ }
957
+ : {
958
+ right: '6px',
959
+ top: '6px',
960
+ padding: '2px 8px',
961
+ borderRadius: '10px',
962
+ fontSize: '11px',
963
+ fontWeight: '600',
964
+ }),
965
+ });
966
+ return badge;
967
+ };
968
+
969
+ // Pin the detail badge to the top of the rendered image rather than the corner
970
+ // of the surrounding area: the capture is centered and rarely fills the region,
971
+ // so a corner-anchored badge floats off the document.
972
+ const placeIssueBadgeOverImage = (badge, imgEl) => {
973
+ const rect = getPreviewLayoutRect(imgEl);
974
+ if (!rect?.width) return;
975
+ badge.style.left = `${rect.centerX}px`;
976
+ badge.style.top = `${rect.top + 12}px`;
977
+ badge.style.transform = 'translateX(-50%)';
978
+ };
979
+
901
980
  // Grid tile for the multi-page overview: tapping it opens the page detail.
902
981
  const buildGridTile = (page, pageIndex) => {
903
982
  const cell = document.createElement('div');
@@ -938,6 +1017,9 @@ export const createReviewGallery = (container, callbacks = {}, options = {}) =>
938
1017
  });
939
1018
  cell.appendChild(num);
940
1019
 
1020
+ const issueBadge = buildIssueBadge(page);
1021
+ if (issueBadge) cell.appendChild(issueBadge);
1022
+
941
1023
  // Quick retake / delete without opening the detail view. The buttons stop
942
1024
  // propagation so the rest of the tile still opens the detail on tap.
943
1025
  const controls = document.createElement('div');
@@ -1017,6 +1099,7 @@ export const createReviewGallery = (container, callbacks = {}, options = {}) =>
1017
1099
  clearFieldOverlays();
1018
1100
  detailImgEl = null;
1019
1101
  detailPage = null;
1102
+ detailIssueBadge = null;
1020
1103
  gridEl.innerHTML = '';
1021
1104
  const activeDoc = docs[activeIndex];
1022
1105
  const pages = activeDoc?.pages || [];
@@ -1128,7 +1211,7 @@ export const createReviewGallery = (container, callbacks = {}, options = {}) =>
1128
1211
  submitButton.disabled = submitting;
1129
1212
  if (submitting) {
1130
1213
  clearFieldOverlays();
1131
- } else if (fieldScan.result) {
1214
+ } else {
1132
1215
  applyFieldDetectionToDetail();
1133
1216
  }
1134
1217
  if (loadingOverlayEl) {
@@ -1147,23 +1230,20 @@ export const createReviewGallery = (container, callbacks = {}, options = {}) =>
1147
1230
  // Start the field-detection scanning animation over the focused image of the
1148
1231
  // given page while OCR runs.
1149
1232
  startFieldScan: (pageId) => {
1150
- fieldScan = { active: true, result: null, pageId: pageId ?? null };
1233
+ if (!pageId) return;
1234
+ scanningPageIds.add(pageId);
1151
1235
  applyFieldDetectionToDetail();
1152
1236
  },
1153
- // Finish the scan for the given page: draw the vendor/date/total boxes when a
1154
- // result is provided, otherwise just clear the animation. Ignored if a newer
1155
- // scan (different page) has superseded this one.
1156
- finishFieldScan: (pageId, result) => {
1157
- if (fieldScan.pageId && pageId !== fieldScan.pageId) return;
1158
- fieldScan = {
1159
- active: false,
1160
- result: result || null,
1161
- pageId: result ? fieldScan.pageId : null,
1162
- };
1163
- if (result) applyFieldDetectionToDetail();
1164
- else clearFieldOverlays();
1237
+ // Finish the scan for the given page. The result has already been stored on
1238
+ // the page, so this just stops the animation and repaints — drawing the
1239
+ // vendor/date/total boxes when that page is the one in view.
1240
+ finishFieldScan: (pageId) => {
1241
+ if (!pageId) return;
1242
+ scanningPageIds.delete(pageId);
1243
+ applyFieldDetectionToDetail();
1165
1244
  },
1166
1245
  remove: () => {
1246
+ scanningPageIds.clear();
1167
1247
  clearFieldOverlays();
1168
1248
  document.removeEventListener('keydown', onKeyDown);
1169
1249
  window.removeEventListener('resize', onResize);
package/core/api.js CHANGED
@@ -18,7 +18,7 @@ export const getQrSessionId = () => {
18
18
  return new URLSearchParams(window.location.search).get('session');
19
19
  };
20
20
 
21
- export const validatePartner = async (client_id) => {
21
+ export const validatePartner = async (client_id, multi = false) => {
22
22
  try {
23
23
  state.updateHandler?.({ status: 'validation_started' });
24
24
  const response = await fetch(
@@ -28,6 +28,7 @@ export const validatePartner = async (client_id) => {
28
28
  headers: {
29
29
  'Content-Type': 'application/json',
30
30
  'CLIENT-ID': client_id,
31
+ ...(multi ? { 'MULTI-SUBMISSION': 'true' } : {})
31
32
  },
32
33
  });
33
34
 
@@ -284,7 +285,11 @@ export const fireResultSetter = async (payload, { autoUpload = true } = {}) => {
284
285
  return false;
285
286
  };
286
287
 
287
- export const handleSubmit = async (image) => {
288
+ // `fieldDetection` is the result computed for the document being submitted. It
289
+ // is passed explicitly because a session can hold several documents, each with
290
+ // its own result; state.fieldDetectionResult is only the last one computed and
291
+ // serves as the fallback for the legacy single-document path.
292
+ export const handleSubmit = async (image, { fieldDetection } = {}) => {
288
293
  // Snapshot state into locals before any await — the submit button click handler
289
294
  // fires removeOverlay()/resetState() concurrently with this flow.
290
295
  const lensFlavor = state.lensFlavor;
@@ -297,8 +302,9 @@ export const handleSubmit = async (image) => {
297
302
  const lastJsonData = state.packageScans?.[state.packageScans.length - 1]?.jsonData;
298
303
  const documentFieldsToDetect = state.documentFieldsToDetect?.slice() ?? [];
299
304
  const successHandler = state.successHandler;
300
- const localFieldDetection = state.fieldDetectionResult
301
- ? structuredClone(state.fieldDetectionResult)
305
+ const resolvedFieldDetection = fieldDetection ?? state.fieldDetectionResult;
306
+ const localFieldDetection = resolvedFieldDetection
307
+ ? structuredClone(resolvedFieldDetection)
302
308
  : null;
303
309
 
304
310
  state.submitInProgress = true;
@@ -398,12 +404,16 @@ export const handleSubmit = async (image) => {
398
404
  }
399
405
  };
400
406
 
407
+ // QR batches finish so the backend can deliver the batch.ready webhook; embedded
408
+ // batches finish only to release the session early, since the partner already
409
+ // received each document from handleSubmit.
401
410
  export const finishBatch = async () => {
402
411
  const clientId = state.clientId;
412
+ const sessionId = state.clientData?.session;
403
413
  const qrSessionId = getQrSessionId();
404
414
 
405
- if (!clientId || !qrSessionId) {
406
- throw new Error('CLIENT-ID and QR session are required to finish the batch');
415
+ if (!clientId || !(qrSessionId || sessionId)) {
416
+ throw new Error('CLIENT-ID and a QR or partner session are required to finish the batch');
407
417
  }
408
418
 
409
419
  state.updateHandler?.({ status: 'batch_finish_started' });
@@ -412,7 +422,8 @@ export const finishBatch = async () => {
412
422
  method: 'POST',
413
423
  headers: {
414
424
  'CLIENT-ID': clientId,
415
- 'QR-SESSION-ID': qrSessionId,
425
+ ...(sessionId ? { 'SESSION-ID': sessionId } : {}),
426
+ ...(qrSessionId ? { 'QR-SESSION-ID': qrSessionId } : {})
416
427
  },
417
428
  });
418
429
 
package/core/camera.js CHANGED
@@ -1,4 +1,33 @@
1
1
  import { state, refs, actions } from './state.js';
2
+ import {
3
+ createCameraPermissionMessage,
4
+ suppressOnCameraControls,
5
+ restoreOnCameraControls,
6
+ } from '../components/cameraPermissionMessage.js';
7
+
8
+ // Browsers report a rejected camera prompt (or a blocked site permission) with
9
+ // these DOMException names.
10
+ const PERMISSION_ERROR_NAMES = ['NotAllowedError', 'PermissionDeniedError', 'SecurityError'];
11
+
12
+ const handleCameraPermissionDenied = () => {
13
+ suppressOnCameraControls();
14
+ if (!state.showCameraPermissionDeniedMessage || !state.container) return;
15
+ if (state.cameraPermissionMessage) {
16
+ state.cameraPermissionMessage.show();
17
+ return;
18
+ }
19
+ state.cameraPermissionMessage = createCameraPermissionMessage(state.container, {
20
+ message: state.cameraPermissionDeniedMessage,
21
+ });
22
+ };
23
+
24
+ export const clearCameraPermissionDenied = () => {
25
+ restoreOnCameraControls();
26
+ if (state.cameraPermissionMessage) {
27
+ state.cameraPermissionMessage.remove();
28
+ state.cameraPermissionMessage = null;
29
+ }
30
+ };
2
31
 
3
32
  export const displayVideo = () => {
4
33
  const video = state.videoRef;
@@ -55,6 +84,10 @@ export const getVideo = async (lensFlavor) => {
55
84
  const stream = await navigator.mediaDevices.getUserMedia(constraints);
56
85
  state.updateHandler?.({ status: 'camera_ready' });
57
86
 
87
+ // A previously denied prompt can be granted on a later attempt — drop the
88
+ // message so the controls come back.
89
+ clearCameraPermissionDenied();
90
+
58
91
  if (!state.videoRef) {
59
92
  state.videoRef = document.getElementById("veryfi-video-ref");
60
93
  }
@@ -107,9 +140,23 @@ export const getVideo = async (lensFlavor) => {
107
140
  refs.wasmWrapper.setDocumentCallback(actions.logDocument);
108
141
  }
109
142
  } catch (err) {
110
- state.updateHandler?.({ status: 'camera_failed', error: err.message });
111
- console.error("Error accessing camera:", err);
112
- state.failureHandler?.(err);
143
+ if (PERMISSION_ERROR_NAMES.includes(err?.name)) {
144
+ // A denied prompt is a user decision, not an SDK failure, so it never goes
145
+ // to failureHandler — partners would render a second, duplicate error
146
+ // dialog on top. `message` carries the configured copy so hosts that turn
147
+ // off the in-lens panel can render it on their own error screen.
148
+ state.updateHandler?.({
149
+ status: 'camera_permission_denied',
150
+ message: state.cameraPermissionDeniedMessage,
151
+ error: err.message,
152
+ });
153
+ console.warn("Camera access denied:", err.message);
154
+ handleCameraPermissionDenied();
155
+ } else {
156
+ state.updateHandler?.({ status: 'camera_failed', error: err.message });
157
+ console.error("Error accessing camera:", err);
158
+ state.failureHandler?.(err);
159
+ }
113
160
  }
114
161
  };
115
162
 
@@ -5,6 +5,7 @@ import { hideVideoOverlay } from '../overlay-controls.js';
5
5
  import { stopLens, startStitching, stopStitching } from '../lifecycle.js';
6
6
  import { generateDeviceIDifNeeded } from '../device.js';
7
7
  import { DEFAULTS } from '../constants.js';
8
+ import * as store from '../capture-store.js';
8
9
 
9
10
  export const captureUploaded = async (imageFile) => {
10
11
  state.debug_mode && console.log("Setting document callback");
@@ -54,6 +55,29 @@ export const captureUploaded = async (imageFile) => {
54
55
  });
55
56
  };
56
57
 
58
+ // Commit the capture plus any additional documents the segmenter found in the
59
+ // same frame, tagging each page with its own quality issues so the gallery can
60
+ // flag them individually.
61
+ const commitCaptureAndExtras = async () => {
62
+ const { commitCaptureToReview } = await import('../review-flow.js');
63
+ await commitCaptureToReview({
64
+ kind: 'image',
65
+ data: state.finalImage,
66
+ issues: state.primaryCropIssues,
67
+ });
68
+ for (const extra of state.extraCropImages) {
69
+ if (!store.canAddDocument()) break;
70
+ state.startNewDocumentOnCapture = true;
71
+ await commitCaptureToReview({
72
+ kind: 'image',
73
+ data: extra.data,
74
+ issues: extra.issues,
75
+ });
76
+ }
77
+ state.extraCropImages = [];
78
+ state.primaryCropIssues = [];
79
+ };
80
+
57
81
  export const handleDocumentAutoCaptureDone = async () => {
58
82
  const tStart = performance.now();
59
83
  try {
@@ -108,19 +132,23 @@ export const handleDocumentAutoCaptureDone = async () => {
108
132
  state.lcdStatus = documentLcdResult.is_lcd || (documentLcdResult.lcdProb !== undefined && documentLcdResult.lcdProb > state.lcdThreshold);
109
133
  }
110
134
 
135
+ // A frame holding several documents flags problem pages in the gallery
136
+ // instead of blocking, so one bad crop can't discard the good ones with it.
137
+ const isMultiDocCapture = state.extraCropImages.length > 0;
138
+
111
139
  const isDocumentDetected = checkIfDocumentOnCapture();
112
140
  const documentModalShown = !isDocumentDetected && state.isDocumentModal && !!state.documentModal;
113
- const isBlurry = documentModalShown ? false : checkIfBlurOnCapture();
114
- const isLcd = (documentModalShown || isBlurry) ? false : checkIfLcdOnCapture();
141
+ const skipBlockingModals = documentModalShown || isMultiDocCapture;
142
+ const isBlurry = skipBlockingModals ? false : checkIfBlurOnCapture();
143
+ const isLcd = (skipBlockingModals || isBlurry) ? false : checkIfLcdOnCapture();
115
144
 
116
145
  if (isDocumentDetected && !isBlurry && !isLcd) {
117
146
  state.cropImgRef?.showSuccessBorder?.();
118
147
  }
119
148
 
120
149
  hideVideoOverlay();
121
- if (state.isDocument && !state.blurStatus && !state.lcdStatus) {
122
- const { commitCaptureToReview } = await import('../review-flow.js');
123
- await commitCaptureToReview({ kind: 'image', data: state.finalImage });
150
+ if (state.isDocument && (isMultiDocCapture || (!state.blurStatus && !state.lcdStatus))) {
151
+ await commitCaptureAndExtras();
124
152
  }
125
153
 
126
154
  const tEnd = performance.now();
@@ -199,9 +227,11 @@ export const newCapture = async () => {
199
227
  }
200
228
  }
201
229
 
230
+ const isMultiDocCapture = state.extraCropImages.length > 0;
202
231
  const documentModalShown = !isDocumentDetected && state.isDocumentModal && !!state.documentModal;
203
- const isBlurry = documentModalShown ? false : checkIfBlurOnCapture();
204
- const isLcd = (documentModalShown || isBlurry) ? false : checkIfLcdOnCapture();
232
+ const skipBlockingModals = documentModalShown || isMultiDocCapture;
233
+ const isBlurry = skipBlockingModals ? false : checkIfBlurOnCapture();
234
+ const isLcd = (skipBlockingModals || isBlurry) ? false : checkIfLcdOnCapture();
205
235
 
206
236
  if (isDocumentDetected && !isBlurry && !isLcd) {
207
237
  state.cropImgRef?.showSuccessBorder?.();
@@ -209,10 +239,11 @@ export const newCapture = async () => {
209
239
 
210
240
  if (state.lensFlavor !== 'checks') {
211
241
  hideVideoOverlay();
212
- if (state.isDocument && !state.blurStatus && !state.lcdStatus) {
213
- const { commitCaptureToReview } = await import('../review-flow.js');
214
- await commitCaptureToReview({ kind: 'image', data: state.finalImage });
242
+ if (state.isDocument && (isMultiDocCapture || (!state.blurStatus && !state.lcdStatus))) {
243
+ await commitCaptureAndExtras();
215
244
  }
245
+ state.extraCropImages = [];
246
+ state.primaryCropIssues = [];
216
247
  }
217
248
  } catch (error) {
218
249
  console.error("Capture failed:", error.message);
@@ -11,7 +11,8 @@ import { state } from './state.js';
11
11
  // { id, kind: 'image' | 'video', pages: Page[] }
12
12
  //
13
13
  // Page shape (normalized):
14
- // { id, kind, data, fileName, isBase64, thumb, docReadyResult, jsonData, meta }
14
+ // { id, kind, data, fileName, isBase64, thumb, docReadyResult, jsonData, meta,
15
+ // issues, fieldDetection }
15
16
  // - data: base64 string (images) | Blob (video)
16
17
  // - thumb: base64 image string for gallery rendering (null for video until
17
18
  // a poster frame is provided; the gallery renders a play affordance)
@@ -34,6 +35,14 @@ const normalizePage = (page = {}) => ({
34
35
  docReadyResult: page.docReadyResult || null,
35
36
  jsonData: page.jsonData || null,
36
37
  meta: page.meta || null,
38
+ // Quality problems ('blur' | 'lcd') found on this page at capture time. Only
39
+ // populated for multi-document frames, where the gallery flags instead of
40
+ // blocking; empty means the page passed or wasn't individually checked.
41
+ issues: page.issues || [],
42
+ // Field-detection result (vendor/date/total boxes) computed from this page's
43
+ // own image. Each page owns its result so a multi-document frame doesn't share
44
+ // one global; null until detection completes, or when it isn't applicable.
45
+ fieldDetection: page.fieldDetection ?? null,
37
46
  });
38
47
 
39
48
  export const createDocument = (kind = 'image') => ({
@@ -188,6 +197,27 @@ export const commitCapturedPage = (page) => {
188
197
  return addPage(page);
189
198
  };
190
199
 
200
+ // Attach a field-detection result to a page. Looked up by id because detection
201
+ // is asynchronous and the page may have moved (deletes, new documents) by the
202
+ // time it resolves. Returns true when the page was still present.
203
+ export const setPageFieldDetection = (pageId, result) => {
204
+ if (!pageId) return false;
205
+ for (const doc of state.capturedDocs) {
206
+ const page = doc.pages.find((candidate) => candidate.id === pageId);
207
+ if (page) {
208
+ page.fieldDetection = result || null;
209
+ return true;
210
+ }
211
+ }
212
+ return false;
213
+ };
214
+
215
+ // The field-detection result to submit for a document: the first page that has
216
+ // one. Receipt documents are single-page, so this is that page's result.
217
+ export const documentFieldDetection = (docIndex) =>
218
+ state.capturedDocs[docIndex]?.pages?.find((page) => page.fieldDetection)
219
+ ?.fieldDetection ?? null;
220
+
191
221
  // Produce the per-scan records consumed by api.uploadPackage()/sendCropped for
192
222
  // a single document: { data, fileName, isBase64, jsonData }.
193
223
  export const buildPackageScans = (docIndex) => {
package/core/constants.js CHANGED
@@ -39,7 +39,7 @@ export const setApiBaseUrl = (base) => {
39
39
  };
40
40
 
41
41
  export const DEFAULTS = {
42
- SDK_VERSION: '3.5.2',
42
+ SDK_VERSION: '3.5.4',
43
43
  BLUR_THRESHOLD: 0.7,
44
44
  LCD_THRESHOLD: 0.5,
45
45
  BOX_COLOR: 'rgba(84, 192, 139, 0.6)',
@@ -112,6 +112,8 @@ export const MESSAGES = {
112
112
  REVIEW_DONE: 'Done',
113
113
  REVIEW_RETAKE: 'Retake',
114
114
  REVIEW_DELETE: 'Delete',
115
+ REVIEW_ISSUE_BLUR: 'Blurry',
116
+ REVIEW_ISSUE_LCD: 'Photo of a screen',
115
117
  REVIEW_PREPARING: 'Preparing document...',
116
118
  REVIEW_PROCESSING: 'Processing document...',
117
119
  REVIEW_SUBMITTING: 'Submitting document...',
@@ -119,6 +121,7 @@ export const MESSAGES = {
119
121
  BATCH_FINISH_FAILED: 'Failed to submit the batch. Would you like to try again?',
120
122
  BATCH_FINISH_RETRY: 'Retry',
121
123
  BATCH_FINISH_CANCEL: 'Close without notifying',
124
+ CAMERA_PERMISSION_DENIED: 'Camera access was blocked. Please allow camera access in your browser settings and try again.',
122
125
  AUTH_FAILED: 'Authorization failed, please provide correct client-id or contact customer support at support@veryfi.com',
123
126
  AUTH_FAILED_CONTACT: 'Authorization failed, please contact customer support at support@veryfi.com',
124
127
  };
@@ -1,6 +1,7 @@
1
1
  import { state, refs } from './state.js';
2
2
  import { detectDocumentFieldsFromCropCanvas } from './field-detection.js';
3
3
  import { shouldRunFieldDetection } from './field-detection-policy.js';
4
+ import { setPageFieldDetection } from './capture-store.js';
4
5
  import { hideCropPreview, revealCropPreview } from '../components/croppedImage.js';
5
6
  import {
6
7
  hideFieldDetectionOverlay,
@@ -17,8 +18,15 @@ import {
17
18
  rememberCropPreviewLayout,
18
19
  } from '../components/previewLayout.js';
19
20
 
21
+ // Bumped whenever a pending detection becomes irrelevant (a new crop preview, or
22
+ // an explicit cancel). Page detections started for the same generation run
23
+ // concurrently without cancelling each other — a multi-document frame commits
24
+ // several pages at once and every one of them needs its own result.
20
25
  let cropDetectionGeneration = 0;
21
26
 
27
+ // Detections still in flight. Submit stays held until the last one settles.
28
+ let pendingDetections = 0;
29
+
22
30
  const shouldHoldSubmitForFieldDetection = () =>
23
31
  state.disableSubmitUntilFieldDetectionDone && shouldRunFieldDetection();
24
32
 
@@ -36,29 +44,61 @@ export const holdSubmitForFieldDetectionIfNeeded = () => {
36
44
  };
37
45
 
38
46
  const releaseSubmitAfterFieldDetection = () => {
47
+ if (pendingDetections > 0) {
48
+ return;
49
+ }
39
50
  if (shouldHoldSubmitForFieldDetection()) {
40
51
  setSubmitDisabled(false);
41
52
  }
42
53
  };
43
54
 
44
- // Runs field detection for the review gallery. No crop-preview visuals are
45
- // painted here (the gallery owns the on-image scan animation + boxes); this only
46
- // computes the result, emits events and gates Submit. Returns the field-detection
47
- // result (or null when nothing was detected / it was superseded / it failed).
48
- export const runFieldDetectionForReview = async () => {
49
- if (!shouldRunFieldDetection()) {
55
+ // Decode a stored page image (base64 JPEG, no data-URL prefix) into a canvas.
56
+ // Detection runs on exactly the bytes the gallery renders, so the reported
57
+ // coordinate space matches the image the boxes are drawn over.
58
+ const canvasFromPageData = async (data) => {
59
+ if (typeof data !== 'string' || !data) {
50
60
  return null;
51
61
  }
52
- const canvas = state.cropImgRef;
53
- if (!canvas?.width || !canvas?.height) {
62
+ const img = new Image();
63
+ img.src = `data:image/jpeg;base64,${data}`;
64
+ try {
65
+ await img.decode();
66
+ } catch (error) {
67
+ console.warn('[FieldDetection] Could not decode page image:', error);
68
+ return null;
69
+ }
70
+ if (!img.naturalWidth || !img.naturalHeight) {
71
+ return null;
72
+ }
73
+ const canvas = document.createElement('canvas');
74
+ canvas.width = img.naturalWidth;
75
+ canvas.height = img.naturalHeight;
76
+ canvas.getContext('2d').drawImage(img, 0, 0);
77
+ return canvas;
78
+ };
79
+
80
+ // Runs field detection for one review-gallery page, against that page's own
81
+ // image. No crop-preview visuals are painted here (the gallery owns the on-image
82
+ // scan animation + boxes); this computes the result, stores it on the page,
83
+ // emits events and gates Submit. Several pages committed from a single
84
+ // multi-document frame each get their own detection. Returns the result, or null
85
+ // when nothing was detected / it was cancelled / it failed.
86
+ export const runFieldDetectionForPage = async (page) => {
87
+ if (!shouldRunFieldDetection() || !page || page.kind === 'video') {
88
+ return null;
89
+ }
90
+ const generation = cropDetectionGeneration;
91
+ const canvas = await canvasFromPageData(page.data);
92
+ if (!canvas) {
54
93
  releaseSubmitAfterFieldDetection();
55
94
  return null;
56
95
  }
57
- const generation = ++cropDetectionGeneration;
96
+ pendingDetections += 1;
58
97
  try {
59
98
  state.updateHandler?.({
60
99
  status: 'field_detection_started',
61
100
  field_detection_source: 'tesseract',
101
+ page_id: page.id,
62
102
  });
63
103
  const result = await detectDocumentFieldsFromCropCanvas(canvas, {
64
104
  wasmWrapper: refs.wasmWrapper,
@@ -67,34 +107,39 @@ export const runFieldDetectionForReview = async () => {
67
107
  if (generation !== cropDetectionGeneration) {
68
108
  return null;
69
109
  }
70
- releaseSubmitAfterFieldDetection();
71
110
  if (result) {
111
+ setPageFieldDetection(page.id, result);
72
112
  state.fieldDetectionResult = result;
73
113
  state.updateHandler?.({
74
114
  status: 'field_detection_completed',
75
115
  field_detection: result,
76
116
  field_detection_source: 'tesseract',
117
+ page_id: page.id,
77
118
  });
78
- console.log('[FieldDetection] Review (Tesseract):', result);
119
+ state.debug_mode && console.log('[FieldDetection] Review (Tesseract):', result);
79
120
  return result;
80
121
  }
81
122
  state.updateHandler?.({
82
123
  status: 'field_detection_failed',
83
124
  field_detection_source: 'tesseract',
125
+ page_id: page.id,
84
126
  });
85
127
  return null;
86
128
  } catch (error) {
87
129
  if (generation !== cropDetectionGeneration) {
88
130
  return null;
89
131
  }
90
- releaseSubmitAfterFieldDetection();
91
132
  console.error('[FieldDetection] Review OCR failed:', error);
92
133
  state.updateHandler?.({
93
134
  status: 'field_detection_failed',
94
135
  field_detection_source: 'tesseract',
136
+ page_id: page.id,
95
137
  error,
96
138
  });
97
139
  return null;
140
+ } finally {
141
+ pendingDetections -= 1;
142
+ releaseSubmitAfterFieldDetection();
98
143
  }
99
144
  };
100
145
 
package/core/lifecycle.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { state, refs, resetState, actions } from './state.js';
2
- import { getVideo, displayVideo } from './camera.js';
2
+ import { getVideo, displayVideo, clearCameraPermissionDenied } from './camera.js';
3
3
  import { initializeWasmWrapper, sendWasm, sendCardWasm, getLongImage, logDocument, stopWasm, resetAutoCaptureCounters, resetFieldDetectorBootstrap } from './wasm-processing.js';
4
4
  import { hideVideoOverlay, showVideoOverlay } from './overlay-controls.js';
5
5
  import { docReady, handleSubmit, fireResultSetter } from './api.js';
@@ -285,6 +285,10 @@ export const removeOverlay = () => {
285
285
  state.reviewGalleryRef = null;
286
286
  }
287
287
 
288
+ // Must run explicitly: clearing the container drops the panel but leaves the
289
+ // body class that suppresses the on-camera controls.
290
+ clearCameraPermissionDenied();
291
+
288
292
  cleanupPrescriptionLabelRecording();
289
293
  clearTheme();
290
294
 
@@ -11,7 +11,7 @@ import {
11
11
  finishBatch,
12
12
  getQrSessionId,
13
13
  } from './api.js';
14
- import { holdSubmitForFieldDetectionIfNeeded, runFieldDetectionForReview } from './crop-field-detection.js';
14
+ import { holdSubmitForFieldDetectionIfNeeded, runFieldDetectionForPage } from './crop-field-detection.js';
15
15
  import { shouldRunFieldDetection } from './field-detection-policy.js';
16
16
  import { hideCropPreview } from '../components/croppedImage.js';
17
17
  import { DEFAULTS, MESSAGES } from './constants.js';
@@ -22,7 +22,7 @@ import * as store from './capture-store.js';
22
22
  // gallery callbacks below own add-page / add-document / retake / delete /
23
23
  // select / submit / close. Submission is deferred: at "Submit" every captured
24
24
  // document is uploaded as its own package and posted to /api/document, then
25
- // finishBatch() runs once if batchReady.
25
+ // finishBatch() runs once if batchReady or the session is a reusable batch.
26
26
 
27
27
  // "No SDK submit, no partner handler": the gallery's primary action just
28
28
  // uploads (packageMode) and hands the result to the showCamera setter.
@@ -61,6 +61,11 @@ const galleryOptions = () => ({
61
61
  submitButtonColor: state.reviewSubmitButtonColor || DEFAULTS.SUBMIT_BUTTON_COLOR,
62
62
  submitButtonTextColor: state.reviewSubmitButtonTextColor || DEFAULTS.SUBMIT_BUTTON_TEXT_COLOR,
63
63
  imageQuality: state.imageQuality || DEFAULTS.IMAGE_QUALITY,
64
+ showQualityBadges: state.reviewShowQualityBadges,
65
+ issueLabels: {
66
+ blur: state.reviewIssueBlurText,
67
+ lcd: state.reviewIssueLcdText,
68
+ },
64
69
  allowAddPage: (state.maxPagesPerDocument || Infinity) > 1,
65
70
  allowAddDocument: (state.maxDocuments || Infinity) > 1,
66
71
  showDocumentStrip: (state.maxDocuments || Infinity) > 1,
@@ -195,7 +200,9 @@ const submitOneDocument = async (docIndex) => {
195
200
  }
196
201
 
197
202
  if (isChecks) state.resultSetter?.(checksExtra);
198
- return await handleSubmit(image);
203
+ return await handleSubmit(image, {
204
+ fieldDetection: store.documentFieldDetection(docIndex),
205
+ });
199
206
  };
200
207
 
201
208
  const submitAll = async () => {
@@ -216,6 +223,14 @@ const submitAll = async () => {
216
223
  }
217
224
  if (state.batchReady) {
218
225
  await finishBatch();
226
+ } else if (state.multiSessionActive) {
227
+ // Nothing is delivered by this call, so a failure only means the session
228
+ // lingers until its TTL — not a reason to fail a submitted batch.
229
+ try {
230
+ await finishBatch();
231
+ } catch (error) {
232
+ console.warn('Finishing the batch session failed:', error);
233
+ }
219
234
  }
220
235
  state.updateHandler?.({ status: 'multi_submission_completed' });
221
236
  removeOverlay();
@@ -262,8 +277,13 @@ const galleryCallbacks = () => ({
262
277
  onEditPage: (docIndex, pageIndex, newData) => {
263
278
  const page = state.capturedDocs[docIndex]?.pages?.[pageIndex];
264
279
  if (!page) return;
265
- // Rotate/crop produce a new base64 JPEG; persist as both data and thumb.
266
- store.replacePage({ docIndex, pageIndex }, { ...page, data: newData, thumb: newData });
280
+ // Rotate/crop produce a new base64 JPEG; persist as both data and thumb. Any
281
+ // field-detection boxes were computed against the previous framing, so they
282
+ // no longer line up and are dropped.
283
+ store.replacePage(
284
+ { docIndex, pageIndex },
285
+ { ...page, data: newData, thumb: newData, fieldDetection: null }
286
+ );
267
287
  refreshGallery();
268
288
  },
269
289
  onSubmit: submitAll,
@@ -291,7 +311,7 @@ export const handleCameraExit = () => {
291
311
  // Called by capture handlers after a successful capture. Records the page in the
292
312
  // store (replacing when a retake is in flight), fires the per-capture setter,
293
313
  // and opens/refreshes the review gallery.
294
- export const commitCaptureToReview = async ({ kind = 'image', data, isBase64 = kind !== 'video', thumb, preview, fileName, meta = null } = {}) => {
314
+ export const commitCaptureToReview = async ({ kind = 'image', data, isBase64 = kind !== 'video', thumb, preview, fileName, meta = null, issues = [] } = {}) => {
295
315
  const jsonData = await buildJsonData();
296
316
  ensurePackageIdentity();
297
317
  state.docReadyResult = { upload_id: state.packageUploadId };
@@ -306,6 +326,7 @@ export const commitCaptureToReview = async ({ kind = 'image', data, isBase64 = k
306
326
  jsonData,
307
327
  docReadyResult: state.docReadyResult,
308
328
  meta,
329
+ issues,
309
330
  });
310
331
 
311
332
  // Per-capture setter fire carries the image only; package info is delivered
@@ -327,22 +348,17 @@ export const commitCaptureToReview = async ({ kind = 'image', data, isBase64 = k
327
348
 
328
349
  openGallery();
329
350
 
330
- // Field detection (documentFieldsToDetect) runs on the crop canvas: the gallery
331
- // shows the scanning animation over the focused page while OCR runs, then draws
332
- // the vendor/date/total boxes on completion. The result is also stored for the
333
- // submit payload and gates Submit when disableSubmitUntilFieldDetectionDone is
334
- // set. Only meaningful for images.
335
- if (kind !== 'video') {
351
+ // Field detection (documentFieldsToDetect) runs on this page's own image: the
352
+ // gallery shows the scanning animation over the focused page while OCR runs,
353
+ // then draws the vendor/date/total boxes on completion. The result is stored on
354
+ // the page for its document's submit payload and gates Submit when
355
+ // disableSubmitUntilFieldDetectionDone is set. Only meaningful for images.
356
+ if (kind !== 'video' && page && shouldRunFieldDetection()) {
336
357
  holdSubmitForFieldDetectionIfNeeded();
337
- if (shouldRunFieldDetection()) {
338
- const pageId = page.id;
339
- state.reviewGalleryRef?.startFieldScan?.(pageId);
340
- runFieldDetectionForReview().then((result) => {
341
- state.reviewGalleryRef?.finishFieldScan?.(pageId, result);
342
- });
343
- } else {
344
- void runFieldDetectionForReview();
345
- }
358
+ state.reviewGalleryRef?.startFieldScan?.(page.id);
359
+ runFieldDetectionForPage(page).then((result) => {
360
+ state.reviewGalleryRef?.finishFieldScan?.(page.id, result);
361
+ });
346
362
  }
347
363
 
348
364
  return page;
package/core/state.js CHANGED
@@ -35,6 +35,10 @@ export const state = {
35
35
  variance: null,
36
36
  coordinates: [],
37
37
  allCoordinates: [],
38
+ extraCropImages: [],
39
+ // Quality issues found on the first segmented crop, tracked separately from
40
+ // blurStatus/lcdStatus so multi-document frames can flag per page.
41
+ primaryCropIssues: [],
38
42
  hasCoordinates: false,
39
43
  boxColor: DEFAULTS.BOX_COLOR,
40
44
  doc_type: "",
@@ -87,6 +91,8 @@ export const state = {
87
91
  isLcdModal: true,
88
92
  container: null,
89
93
  exitButton: false,
94
+ showCameraPermissionDeniedMessage: true,
95
+ cameraPermissionDeniedMessage: MESSAGES.CAMERA_PERMISSION_DENIED,
90
96
  onCloseRedirectUrl: null,
91
97
  onClose: null,
92
98
  resultSetter: null,
@@ -148,6 +154,9 @@ export const state = {
148
154
  reviewDoneText: MESSAGES.REVIEW_DONE,
149
155
  reviewRetakeText: MESSAGES.REVIEW_RETAKE,
150
156
  reviewDeleteText: MESSAGES.REVIEW_DELETE,
157
+ reviewShowQualityBadges: true,
158
+ reviewIssueBlurText: MESSAGES.REVIEW_ISSUE_BLUR,
159
+ reviewIssueLcdText: MESSAGES.REVIEW_ISSUE_LCD,
151
160
  reviewSubmitButtonColor: DEFAULTS.SUBMIT_BUTTON_COLOR,
152
161
  reviewSubmitButtonTextColor: DEFAULTS.SUBMIT_BUTTON_TEXT_COLOR,
153
162
  packageScans: [],
@@ -157,6 +166,7 @@ export const state = {
157
166
  anydocSubmitText: MESSAGES.ANYDOC_SUBMIT,
158
167
  multiSubmission: false,
159
168
  batchReady: false,
169
+ multiSessionActive: false,
160
170
  multiCaptureModalMessage: MESSAGES.MULTI_CAPTURE_PROMPT,
161
171
  multiCaptureYesButtonText: MESSAGES.MULTI_CAPTURE_YES,
162
172
  multiCaptureNoButtonText: MESSAGES.MULTI_CAPTURE_NO,
@@ -197,6 +207,7 @@ export const state = {
197
207
  submitButton: null,
198
208
  videoOverlay: null,
199
209
  captureButton: null,
210
+ cameraPermissionMessage: null,
200
211
  exitButtonRef: null,
201
212
  longPreview: null,
202
213
  cardOverlay: null,
@@ -362,6 +373,7 @@ export const resetState = () => {
362
373
  state.isMirrored = false;
363
374
  state.cameraFacingMode = 'environment';
364
375
  state.captureButton = null;
376
+ state.cameraPermissionMessage = null;
365
377
  state.submitButton = null;
366
378
  state.exitButtonRef = null;
367
379
  state.longPreview = null;
package/core/utils.js CHANGED
@@ -91,6 +91,13 @@ export const base64ToImageBitmap = async (base64Data) => {
91
91
  return await createImageBitmap(blob);
92
92
  };
93
93
 
94
+ /**
95
+ * Blur verdict for a single crop's variance, without touching global state.
96
+ * Lets multi-document captures judge each crop independently.
97
+ */
98
+ export const isBlurryVariance = (variance) =>
99
+ variance >= 0 && variance > state.blurThreshold;
100
+
94
101
  export const setBlurStatus = (variance) => {
95
102
  state.debug_mode && console.log(`Blur variance: ${variance}, threshold: ${state.blurThreshold}`);
96
103
 
@@ -1,6 +1,6 @@
1
1
  import { state, refs, actions } from './state.js';
2
2
  import { drawContours, expandCoordinatesWithMargin } from './drawing.js';
3
- import { releaseCanvas, setCoordinates, setHasCoordinates, setBlurStatus } from './utils.js';
3
+ import { releaseCanvas, setCoordinates, setHasCoordinates, setBlurStatus, isBlurryVariance } from './utils.js';
4
4
  import { DEFAULTS, WASM } from './constants.js';
5
5
  import { bootstrapTesseract } from './tesseract-ocr.js';
6
6
  import { shouldRunFieldDetection } from './field-detection-policy.js';
@@ -417,15 +417,79 @@ const applyOrientationCorrection = async (cropOut) => {
417
417
  }
418
418
  };
419
419
 
420
+ // Render a segmentation crop ({data,outputWidth,outputHeight}) to a base64 JPEG
421
+ // string on a throwaway canvas. Used for the extra documents detected in a
422
+ // single frame (multi-doc capture); the primary crop still flows through
423
+ // cropImgRef in cropWasm below.
424
+ const renderCropToBase64 = (cropOut) => {
425
+ const { data, outputWidth, outputHeight } = cropOut;
426
+ const canvas = document.createElement("canvas");
427
+ canvas.width = outputWidth;
428
+ canvas.height = outputHeight;
429
+ canvas.getContext("2d").putImageData(new ImageData(data, outputWidth, outputHeight), 0, 0);
430
+ return canvas.toDataURL("image/jpeg", DEFAULTS.IMAGE_QUALITY).split("data:image/jpeg;base64,")[1];
431
+ };
432
+
433
+ /**
434
+ * Quality verdict for one segmented crop, evaluated in isolation so a bad
435
+ * document in a multi-document frame doesn't speak for the others. Returns the
436
+ * list of issue keys ('blur' | 'lcd'); empty means the crop passed.
437
+ */
438
+ const evaluateCropQuality = async (cropOut) => {
439
+ const issues = [];
440
+
441
+ if (state.detectBlur && isBlurryVariance(cropOut.blurLevel)) {
442
+ issues.push('blur');
443
+ }
444
+
445
+ if (state.lcdDetectionForDocuments && refs.wasmWrapper?.isDocumentLCDDetectorReady?.()) {
446
+ try {
447
+ const canvas = new OffscreenCanvas(cropOut.outputWidth, cropOut.outputHeight);
448
+ canvas.getContext("2d").putImageData(
449
+ new ImageData(cropOut.data, cropOut.outputWidth, cropOut.outputHeight), 0, 0
450
+ );
451
+ const bitmap = await createImageBitmap(canvas);
452
+ const result = refs.wasmWrapper.detectDocumentLCD(bitmap);
453
+ bitmap.close();
454
+ const isLcd = result?.is_lcd
455
+ || (result?.lcdProb !== undefined && result.lcdProb > state.lcdThreshold);
456
+ if (isLcd) issues.push('lcd');
457
+ } catch (error) {
458
+ state.debug_mode && console.log('[SegCrop] LCD check failed for crop:', error);
459
+ }
460
+ }
461
+
462
+ return issues;
463
+ };
464
+
420
465
  export const cropWasm = async (calculateBlur = true) => {
421
466
  let wasmOutput;
422
467
  state.isDocument = false;
468
+ state.extraCropImages = [];
469
+ state.primaryCropIssues = [];
423
470
 
424
471
  if (refs.wasmWrapper?.isSegmentationCropperReady?.() && state.fullSizeImage) {
425
- const segOut = refs.wasmWrapper.cropWithSegmentation(state.fullSizeImage, calculateBlur);
426
- if (segOut) {
427
- wasmOutput = await applyOrientationCorrection(segOut);
472
+ // maxDocuments === 1 means single-doc flavors (document/checks); only gather
473
+ // extra crops when the flavor permits more than one document.
474
+ const allowMultiDoc = state.maxDocuments !== 1;
475
+ const segCrops = refs.wasmWrapper.cropAllWithSegmentation(state.fullSizeImage, calculateBlur);
476
+ if (segCrops.length) {
477
+ wasmOutput = await applyOrientationCorrection(segCrops[0]);
428
478
  state.isDocument = true;
479
+ if (allowMultiDoc) {
480
+ // Judge every crop on its own so one bad document neither blocks the
481
+ // good ones nor rides along unflagged.
482
+ if (segCrops.length > 1) {
483
+ state.primaryCropIssues = await evaluateCropQuality(wasmOutput);
484
+ }
485
+ for (let i = 1; i < segCrops.length; i++) {
486
+ const corrected = await applyOrientationCorrection(segCrops[i]);
487
+ state.extraCropImages.push({
488
+ data: renderCropToBase64(corrected),
489
+ issues: await evaluateCropQuality(corrected),
490
+ });
491
+ }
492
+ }
429
493
  }
430
494
  }
431
495
 
@@ -8,8 +8,11 @@ export const setup = async (state, refs, actions) => {
8
8
  actions.handleDocumentAutoCaptureDone = handleDocumentAutoCaptureDone;
9
9
  actions.logDocument = logDocument;
10
10
  actions.checkIfDocumentOnCapture = checkIfDocumentOnCapture;
11
+ // A receipt is always a single page, but several of them can be captured in
12
+ // one session (and segmentation can pull several out of one frame) when the
13
+ // partner enables multiSubmission/maxDocuments.
11
14
  state.maxPagesPerDocument = 1;
12
- state.maxDocuments = 1;
15
+ if (state.maxDocuments == null) state.maxDocuments = state.multiSubmission ? null : 1;
13
16
  };
14
17
 
15
18
  export const render = async (state, refs, actions) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "veryfi-lens-wasm",
3
- "version": "3.5.2",
3
+ "version": "3.5.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
@@ -67,7 +67,8 @@ const VeryfiLens = {
67
67
  } else {
68
68
  setApiEnvironment(state.useDevApi);
69
69
  }
70
- state.clientData = await validatePartner(state.clientId);
70
+ const isMultiSession = !!config.multiSubmission || (config.maxDocuments ?? 1) !== 1;
71
+ state.clientData = await validatePartner(state.clientId, isMultiSession);
71
72
  config.debug_mode && console.log(state.clientData);
72
73
 
73
74
  Object.keys(config).forEach(key => {
@@ -76,6 +77,8 @@ const VeryfiLens = {
76
77
  }
77
78
  });
78
79
 
80
+ state.multiSessionActive = isMultiSession;
81
+
79
82
  normalizeCaptureConfig(config);
80
83
  normalizeThemeConfig(config);
81
84
 
@@ -196,6 +199,8 @@ const VeryfiLens = {
196
199
  }
197
200
  await flavorModule.setup(state, refs, actions);
198
201
 
202
+ if (!state.hasInit) return;
203
+
199
204
  loadingSpinner.remove();
200
205
 
201
206
  await flavorModule.render(state, refs, actions);
package/wasm/wasm.js CHANGED
@@ -581,6 +581,46 @@ export class WasmWrapper {
581
581
  return result;
582
582
  }
583
583
 
584
+ /**
585
+ * Run segmentation crop and return every detected document crop (multi-doc
586
+ * capture), not just the first. The WASM returns a fixed-size MatResult array
587
+ * (MAX_RESULTS); we read entries until the first empty one.
588
+ * @param {ImageBitmap} bitmap - The full-size frame
589
+ * @param {boolean} [calculateBlur=true]
590
+ * @returns {Array<{data:Uint8ClampedArray,blurLevel:number,outputHeight:number,outputWidth:number}>}
591
+ */
592
+ cropAllWithSegmentation(bitmap, calculateBlur = true) {
593
+ if (!this.loaded || !this.isSegmentationCropperReady()) {
594
+ console.warn('[SegCrop] Cropper not ready');
595
+ return [];
596
+ }
597
+ const t0 = performance.now();
598
+ const buffer = this.setBitmapOnWASMMemory_(bitmap);
599
+ const outputBuffer = this.wasmModule.ccall(
600
+ "cropWithSegmentation",
601
+ "number",
602
+ ["number", "number", "number", "boolean"],
603
+ [buffer, bitmap.width, bitmap.height, calculateBlur]
604
+ );
605
+ this.freeBuffer_(buffer);
606
+ if (outputBuffer === 0) {
607
+ console.warn('[SegCrop] No document detected');
608
+ return [];
609
+ }
610
+ // MatResult = { void* data; int rows; int cols; float blur } = 16 bytes on
611
+ // wasm32. Read up to MAX_RESULTS (2) entries, stopping at the first empty.
612
+ const MAT_RESULT_BYTES = 16;
613
+ const MAX_RESULTS = 2;
614
+ const results = [];
615
+ for (let i = 0; i < MAX_RESULTS; i++) {
616
+ const result = this.getResultFromBuffer(outputBuffer + i * MAT_RESULT_BYTES);
617
+ if (!result.outputWidth || !result.outputHeight) break;
618
+ results.push(result);
619
+ }
620
+ this.debug && console.log(`[PERF][WASM] cropAllWithSegmentation (${bitmap.width}x${bitmap.height}): ${results.length} crop(s) ${(performance.now() - t0).toFixed(1)}ms`);
621
+ return results;
622
+ }
623
+
584
624
  /**
585
625
  * Initialize the orientation classifier. The WASM fetches the encrypted model
586
626
  * from /rest/get_wasm/crop/* (same path as blur/front_back). Poll