veryfi-lens-wasm 3.5.3 → 3.5.5
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 +8 -0
- package/components/bottleCapOverlay.js +2 -1
- package/components/cameraPermissionMessage.js +76 -0
- package/components/checkOverlay.js +2 -1
- package/components/codeStripsOverlay.js +2 -1
- package/components/reviewGallery.js +3 -2
- package/core/api.js +22 -0
- package/core/camera.js +50 -3
- package/core/capture/document.js +3 -3
- package/core/capture/prescription_label.js +2 -2
- package/core/constants.js +4 -1
- package/core/image-encode.js +58 -0
- package/core/lifecycle.js +8 -2
- package/core/state.js +14 -0
- package/core/wasm-processing.js +8 -7
- package/package.json +1 -1
- package/veryfi-lens.js +26 -0
package/README.md
CHANGED
|
@@ -115,6 +115,11 @@ VeryfiLens.onFailure((error) => { /* error occurred */ });
|
|
|
115
115
|
| `enableFullScreen` | `boolean` | `false` | Enable fullscreen mode |
|
|
116
116
|
| `debug_mode` | `boolean` | `false` | Enable console logging |
|
|
117
117
|
| `useDevApi` | `boolean` | `false` | Route API requests to `lens-dev.veryfi.com` instead of production |
|
|
118
|
+
| `originalImageMaxSizeInMB` | `number` | — | Maximum size in MB for produced images (valid range `0.2`–`2.5`). Quality, then dimensions, are reduced until the JPEG fits. Unset or `0` means no size limit. Does not apply to recorded video |
|
|
119
|
+
| `autoTagSource` | `boolean` | `false` | Tag the submitted document with its source (`LFB`) |
|
|
120
|
+
| `autoTagDeviceId` | `boolean` | `false` | Tag the submitted document with the device ID (UUID) |
|
|
121
|
+
| `autoTagLensVersion` | `boolean` | `false` | Tag the submitted document with the Lens version |
|
|
122
|
+
| `autoTagPlatform` | `boolean` | `false` | Tag the submitted document with the platform (`Web`, or `Web-<browser>` when the browser is known) |
|
|
118
123
|
|
|
119
124
|
### UI Controls
|
|
120
125
|
|
|
@@ -126,6 +131,7 @@ VeryfiLens.onFailure((error) => { /* error occurred */ });
|
|
|
126
131
|
| `mirrorButton` | `boolean` | `true` | Show mirror/flip video button |
|
|
127
132
|
| `mirrorOnDesktop` | `boolean` | `true` | Auto-mirror video on desktop (front-facing webcam) |
|
|
128
133
|
| `exitButton` | `boolean` | `false` | Show exit/close button |
|
|
134
|
+
| `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
135
|
| `onClose` | `function` | — | Callback when lens is closed |
|
|
130
136
|
| `onCloseRedirectUrl` | `string` | — | URL to redirect to on close |
|
|
131
137
|
| `boxColor` | `string` | `"rgba(84, 192, 139, 0.6)"` | Document detection bounding box color |
|
|
@@ -138,6 +144,7 @@ VeryfiLens.onFailure((error) => { /* error occurred */ });
|
|
|
138
144
|
| `isBlurModal` | `boolean` | `true` | Show modal when blur detected |
|
|
139
145
|
| `isDocumentModal` | `boolean` | `true` | Show modal when no document detected |
|
|
140
146
|
| `isLcdModal` | `boolean` | `true` | Show modal when LCD/screen capture detected |
|
|
147
|
+
| `isDesktopModeModal` | `boolean` | `true` | Show the modal asking mobile users to leave "Desktop site" mode |
|
|
141
148
|
| `enforceDocumentDetection` | `boolean` | `false` | Block submission if no document detected |
|
|
142
149
|
| `enforceBlurDetection` | `boolean` | `false` | Block submission if image is blurry |
|
|
143
150
|
| `enforceLcdDetection` | `boolean` | `false` | Block submission if screen capture detected |
|
|
@@ -291,6 +298,7 @@ When `multiSubmission` is set or `maxDocuments` is not `1`, `init()` asks the ba
|
|
|
291
298
|
| `lcdModalMessage` | `string` | `"LCD screen detected. Please capture a printed document instead"` |
|
|
292
299
|
| `submitButtonText` | `string` | `"Submit Anyway"` |
|
|
293
300
|
| `retakeButtonText` | `string` | `"Retake a picture"` |
|
|
301
|
+
| `cameraPermissionDeniedMessage` | `string` | `"Camera access was blocked. Please allow camera access in your browser settings and try again."` |
|
|
294
302
|
| `checkEnforcedModalMessage` | `string` | `"Please flip the check and capture the back side"` |
|
|
295
303
|
| `checkOptionalModalMessage` | `string` | `"Do you want to capture the back side of the check?"` |
|
|
296
304
|
| `wrongSideFrontMessage` | `string` | `"This looks like the back of the check..."` |
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { createOverlayBase, createCenteredElement, createLoadingSpinner } from './overlayBase.js';
|
|
2
|
+
import { canvasToJpegBase64 } from '../core/image-encode.js';
|
|
2
3
|
|
|
3
4
|
export const createBottleCapOverlay = (container, options = {}) => {
|
|
4
5
|
const circleRadius = options.circleRadius !== undefined ? options.circleRadius : 0.7;
|
|
@@ -62,7 +63,7 @@ export const createBottleCapOverlay = (container, options = {}) => {
|
|
|
62
63
|
if (typeof cropImgRef.setSquareDisplay === 'function') {
|
|
63
64
|
cropImgRef.setSquareDisplay();
|
|
64
65
|
}
|
|
65
|
-
return cropImgRef
|
|
66
|
+
return canvasToJpegBase64(cropImgRef);
|
|
66
67
|
},
|
|
67
68
|
});
|
|
68
69
|
|
|
@@ -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
|
+
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { createOverlayBase } from './overlayBase.js';
|
|
2
|
+
import { canvasToJpegBase64 } from '../core/image-encode.js';
|
|
2
3
|
|
|
3
4
|
export const createCheckOverlay = (container, options = {}) => {
|
|
4
5
|
const cropLayoutAspectRatio = options.cropLayoutAspectRatio !== undefined ? options.cropLayoutAspectRatio : 0.0;
|
|
@@ -236,7 +237,7 @@ export const createCheckOverlay = (container, options = {}) => {
|
|
|
236
237
|
0, 0, finalCropWidth, finalCropHeight
|
|
237
238
|
);
|
|
238
239
|
|
|
239
|
-
return cropImgRef
|
|
240
|
+
return canvasToJpegBase64(cropImgRef);
|
|
240
241
|
},
|
|
241
242
|
});
|
|
242
243
|
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { createOverlayBase, createCenteredElement, createLoadingSpinner } from './overlayBase.js';
|
|
2
|
+
import { canvasToJpegBase64 } from '../core/image-encode.js';
|
|
2
3
|
|
|
3
4
|
export const createCodeStripsOverlay = (container, options = {}) => {
|
|
4
5
|
const rectWidth = options.rectWidth !== undefined ? options.rectWidth : 0.8;
|
|
@@ -72,7 +73,7 @@ export const createCodeStripsOverlay = (container, options = {}) => {
|
|
|
72
73
|
if (typeof cropImgRef.setRectangleDisplay === 'function') {
|
|
73
74
|
cropImgRef.setRectangleDisplay();
|
|
74
75
|
}
|
|
75
|
-
return cropImgRef
|
|
76
|
+
return canvasToJpegBase64(cropImgRef);
|
|
76
77
|
},
|
|
77
78
|
});
|
|
78
79
|
|
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
hideFieldDetectionOverlay,
|
|
10
10
|
} from './fieldDetectionOverlay.js';
|
|
11
11
|
import { getPreviewLayoutRect } from './previewLayout.js';
|
|
12
|
+
import { canvasToJpegBase64 } from '../core/image-encode.js';
|
|
12
13
|
|
|
13
14
|
const MOBILE_BREAKPOINT = 768;
|
|
14
15
|
const DESKTOP_GALLERY_MAX_WIDTH = 500;
|
|
@@ -481,7 +482,7 @@ export const createReviewGallery = (container, callbacks = {}, options = {}) =>
|
|
|
481
482
|
ctx.translate(canvas.width / 2, canvas.height / 2);
|
|
482
483
|
ctx.rotate(Math.PI / 2);
|
|
483
484
|
ctx.drawImage(image, -image.naturalWidth / 2, -image.naturalHeight / 2);
|
|
484
|
-
resolve(canvas
|
|
485
|
+
resolve(canvasToJpegBase64(canvas, o.imageQuality));
|
|
485
486
|
};
|
|
486
487
|
image.onerror = reject;
|
|
487
488
|
image.src = `data:image/jpeg;base64,${base64}`;
|
|
@@ -703,7 +704,7 @@ export const createReviewGallery = (container, callbacks = {}, options = {}) =>
|
|
|
703
704
|
canvas.height = sh;
|
|
704
705
|
const ctx = canvas.getContext('2d');
|
|
705
706
|
ctx.drawImage(img, sx, sy, sw, sh, 0, 0, sw, sh);
|
|
706
|
-
const data = canvas
|
|
707
|
+
const data = canvasToJpegBase64(canvas, o.imageQuality);
|
|
707
708
|
cleanup();
|
|
708
709
|
onDone(data);
|
|
709
710
|
});
|
package/core/api.js
CHANGED
|
@@ -70,6 +70,21 @@ export const getMetaData = () => {
|
|
|
70
70
|
};
|
|
71
71
|
};
|
|
72
72
|
|
|
73
|
+
// Auto tags appended to the /api/document payload when the partner opts in.
|
|
74
|
+
// Each flag contributes one tag; the values mirror what the metadata already
|
|
75
|
+
// reports (source, device UUID, SDK version, platform + browser).
|
|
76
|
+
export const buildAutoTags = () => {
|
|
77
|
+
const tags = [];
|
|
78
|
+
if (state.autoTagSource) tags.push('LFB');
|
|
79
|
+
if (state.autoTagDeviceId && state.uuidv4) tags.push(state.uuidv4);
|
|
80
|
+
if (state.autoTagLensVersion) tags.push(state.sdkVersion);
|
|
81
|
+
if (state.autoTagPlatform) {
|
|
82
|
+
const browserName = parseUA()?.browser?.name;
|
|
83
|
+
tags.push(browserName ? `Web-${browserName}` : 'Web');
|
|
84
|
+
}
|
|
85
|
+
return tags;
|
|
86
|
+
};
|
|
87
|
+
|
|
73
88
|
export const buildJsonData = async () => {
|
|
74
89
|
const browserData = await getBrowserData();
|
|
75
90
|
const UAData = flattenObject(parseUA());
|
|
@@ -302,6 +317,12 @@ export const handleSubmit = async (image, { fieldDetection } = {}) => {
|
|
|
302
317
|
const lastJsonData = state.packageScans?.[state.packageScans.length - 1]?.jsonData;
|
|
303
318
|
const documentFieldsToDetect = state.documentFieldsToDetect?.slice() ?? [];
|
|
304
319
|
const successHandler = state.successHandler;
|
|
320
|
+
// Auto tags are merged with any tags the partner set through customData so
|
|
321
|
+
// neither source is lost; duplicates are collapsed.
|
|
322
|
+
const tags = [...new Set([
|
|
323
|
+
...buildAutoTags(),
|
|
324
|
+
...(Array.isArray(customData?.tags) ? customData.tags : []),
|
|
325
|
+
])];
|
|
305
326
|
const resolvedFieldDetection = fieldDetection ?? state.fieldDetectionResult;
|
|
306
327
|
const localFieldDetection = resolvedFieldDetection
|
|
307
328
|
? structuredClone(resolvedFieldDetection)
|
|
@@ -342,6 +363,7 @@ export const handleSubmit = async (image, { fieldDetection } = {}) => {
|
|
|
342
363
|
: { image: image }),
|
|
343
364
|
...((lensFlavor === 'anydocs' || lensFlavor === 'prescription_label') && selectedBlueprint ? { blueprint_name: selectedBlueprint } : {}),
|
|
344
365
|
...customData,
|
|
366
|
+
...(tags.length ? { tags } : {}),
|
|
345
367
|
};
|
|
346
368
|
|
|
347
369
|
const qrSessionId = getQrSessionId();
|
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
|
-
|
|
111
|
-
|
|
112
|
-
|
|
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
|
|
package/core/capture/document.js
CHANGED
|
@@ -4,7 +4,7 @@ import { checkIfDocumentOnCapture, checkIfBlurOnCapture, checkIfLcdOnCapture } f
|
|
|
4
4
|
import { hideVideoOverlay } from '../overlay-controls.js';
|
|
5
5
|
import { stopLens, startStitching, stopStitching } from '../lifecycle.js';
|
|
6
6
|
import { generateDeviceIDifNeeded } from '../device.js';
|
|
7
|
-
import {
|
|
7
|
+
import { canvasToJpegBase64 } from '../image-encode.js';
|
|
8
8
|
import * as store from '../capture-store.js';
|
|
9
9
|
|
|
10
10
|
export const captureUploaded = async (imageFile) => {
|
|
@@ -28,7 +28,7 @@ export const captureUploaded = async (imageFile) => {
|
|
|
28
28
|
const croppedImageData = new ImageData(data, outputWidth, outputHeight);
|
|
29
29
|
croppedCtx.putImageData(croppedImageData, 0, 0);
|
|
30
30
|
|
|
31
|
-
const croppedBase64 = croppedCanvas
|
|
31
|
+
const croppedBase64 = canvasToJpegBase64(croppedCanvas);
|
|
32
32
|
resolve(croppedBase64);
|
|
33
33
|
state.resultSetter?.(croppedBase64);
|
|
34
34
|
} else {
|
|
@@ -38,7 +38,7 @@ export const captureUploaded = async (imageFile) => {
|
|
|
38
38
|
canvas.height = img.height;
|
|
39
39
|
const ctx = canvas.getContext('2d');
|
|
40
40
|
ctx.drawImage(img, 0, 0);
|
|
41
|
-
const originalBase64 = canvas
|
|
41
|
+
const originalBase64 = canvasToJpegBase64(canvas);
|
|
42
42
|
resolve(originalBase64);
|
|
43
43
|
state.resultSetter?.(originalBase64);
|
|
44
44
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { state } from '../state.js';
|
|
2
2
|
import { hideVideoOverlay, showVideoOverlay } from '../overlay-controls.js';
|
|
3
3
|
import { newStartLens } from '../lifecycle.js';
|
|
4
|
-
import {
|
|
4
|
+
import { canvasToJpegBase64 } from '../image-encode.js';
|
|
5
5
|
import { CAPTURE_BUTTON_BACKGROUND } from '../../components/captureButton.js';
|
|
6
6
|
|
|
7
7
|
let previewRecorder = null;
|
|
@@ -436,7 +436,7 @@ const capturePrescriptionImage = async () => {
|
|
|
436
436
|
0, 0, cropImgRef.width, cropImgRef.height
|
|
437
437
|
);
|
|
438
438
|
|
|
439
|
-
state.finalImage = cropImgRef
|
|
439
|
+
state.finalImage = canvasToJpegBase64(cropImgRef);
|
|
440
440
|
if (!state.finalImage) {
|
|
441
441
|
throw new Error('Failed to capture prescription label image');
|
|
442
442
|
}
|
package/core/constants.js
CHANGED
|
@@ -39,13 +39,15 @@ export const setApiBaseUrl = (base) => {
|
|
|
39
39
|
};
|
|
40
40
|
|
|
41
41
|
export const DEFAULTS = {
|
|
42
|
-
SDK_VERSION: '3.5.
|
|
42
|
+
SDK_VERSION: '3.5.5',
|
|
43
43
|
BLUR_THRESHOLD: 0.7,
|
|
44
44
|
LCD_THRESHOLD: 0.5,
|
|
45
45
|
BOX_COLOR: 'rgba(84, 192, 139, 0.6)',
|
|
46
46
|
CHECK_ASPECT_RATIO: 2.61,
|
|
47
47
|
CROP_LAYOUT_BORDER_COLOR: '#54C08B',
|
|
48
48
|
IMAGE_QUALITY: 0.97,
|
|
49
|
+
ORIGINAL_IMAGE_MAX_SIZE_MB: 2.5,
|
|
50
|
+
ORIGINAL_IMAGE_MIN_SIZE_MB: 0.2,
|
|
49
51
|
SUBMIT_BUTTON_COLOR: '#00AA00',
|
|
50
52
|
SUBMIT_BUTTON_TEXT_COLOR: '#FFFFFF',
|
|
51
53
|
FINGERPRINT_TIMEOUT: 20000,
|
|
@@ -121,6 +123,7 @@ export const MESSAGES = {
|
|
|
121
123
|
BATCH_FINISH_FAILED: 'Failed to submit the batch. Would you like to try again?',
|
|
122
124
|
BATCH_FINISH_RETRY: 'Retry',
|
|
123
125
|
BATCH_FINISH_CANCEL: 'Close without notifying',
|
|
126
|
+
CAMERA_PERMISSION_DENIED: 'Camera access was blocked. Please allow camera access in your browser settings and try again.',
|
|
124
127
|
AUTH_FAILED: 'Authorization failed, please provide correct client-id or contact customer support at support@veryfi.com',
|
|
125
128
|
AUTH_FAILED_CONTACT: 'Authorization failed, please contact customer support at support@veryfi.com',
|
|
126
129
|
};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { state } from './state.js';
|
|
2
|
+
import { DEFAULTS } from './constants.js';
|
|
3
|
+
|
|
4
|
+
// Quality ladder walked down when the encoded image exceeds the configured cap.
|
|
5
|
+
const QUALITY_STEPS = [0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3];
|
|
6
|
+
// Once the lowest quality still overshoots, shrink the pixel dimensions.
|
|
7
|
+
const SCALE_STEP = 0.8;
|
|
8
|
+
const MAX_SCALE_ATTEMPTS = 6;
|
|
9
|
+
|
|
10
|
+
const base64Bytes = (base64) => Math.floor((base64.length * 3) / 4);
|
|
11
|
+
|
|
12
|
+
const encode = (canvas, quality) =>
|
|
13
|
+
canvas.toDataURL('image/jpeg', quality).split(',')[1];
|
|
14
|
+
|
|
15
|
+
const scaleCanvas = (source, scale) => {
|
|
16
|
+
const width = Math.max(1, Math.round(source.width * scale));
|
|
17
|
+
const height = Math.max(1, Math.round(source.height * scale));
|
|
18
|
+
const scaled = document.createElement('canvas');
|
|
19
|
+
scaled.width = width;
|
|
20
|
+
scaled.height = height;
|
|
21
|
+
scaled.getContext('2d').drawImage(source, 0, 0, width, height);
|
|
22
|
+
return scaled;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Encode a canvas as a base64 JPEG (no data URL prefix). When
|
|
27
|
+
* originalImageMaxSizeInMB is configured the result is kept within that budget:
|
|
28
|
+
* quality is reduced first, then the image is scaled down if the lowest quality
|
|
29
|
+
* still overshoots. Unset (the default) means the image is left uncompressed.
|
|
30
|
+
* Applies to still images only — recorded video is never re-encoded.
|
|
31
|
+
*/
|
|
32
|
+
export const canvasToJpegBase64 = (canvas, quality = DEFAULTS.IMAGE_QUALITY) => {
|
|
33
|
+
let base64 = encode(canvas, quality);
|
|
34
|
+
|
|
35
|
+
const maxSizeInMB = state.originalImageMaxSizeInMB;
|
|
36
|
+
if (!maxSizeInMB) return base64;
|
|
37
|
+
|
|
38
|
+
const maxBytes = maxSizeInMB * 1024 * 1024;
|
|
39
|
+
if (base64Bytes(base64) <= maxBytes) return base64;
|
|
40
|
+
|
|
41
|
+
for (const step of QUALITY_STEPS) {
|
|
42
|
+
if (step >= quality) continue;
|
|
43
|
+
base64 = encode(canvas, step);
|
|
44
|
+
if (base64Bytes(base64) <= maxBytes) return base64;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
let working = canvas;
|
|
48
|
+
for (let attempt = 0; attempt < MAX_SCALE_ATTEMPTS; attempt += 1) {
|
|
49
|
+
working = scaleCanvas(working, SCALE_STEP);
|
|
50
|
+
base64 = encode(working, QUALITY_STEPS[QUALITY_STEPS.length - 1]);
|
|
51
|
+
if (base64Bytes(base64) <= maxBytes) return base64;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
state.debug_mode && console.log(
|
|
55
|
+
`[ImageEncode] Could not fit image under ${maxSizeInMB}MB; returning smallest produced (${(base64Bytes(base64) / (1024 * 1024)).toFixed(2)}MB)`
|
|
56
|
+
);
|
|
57
|
+
return base64;
|
|
58
|
+
};
|
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';
|
|
@@ -22,7 +22,9 @@ import { clearTheme } from './theme.js';
|
|
|
22
22
|
|
|
23
23
|
export const newStartLens = async () => {
|
|
24
24
|
state.started = true;
|
|
25
|
-
|
|
25
|
+
if (state.isDesktopModeModal) {
|
|
26
|
+
installDesktopModeWatchdog();
|
|
27
|
+
}
|
|
26
28
|
if (!state.lensFlavor) {
|
|
27
29
|
throw new Error("Lens flavor is required");
|
|
28
30
|
}
|
|
@@ -285,6 +287,10 @@ export const removeOverlay = () => {
|
|
|
285
287
|
state.reviewGalleryRef = null;
|
|
286
288
|
}
|
|
287
289
|
|
|
290
|
+
// Must run explicitly: clearing the container drops the panel but leaves the
|
|
291
|
+
// body class that suppresses the on-camera controls.
|
|
292
|
+
clearCameraPermissionDenied();
|
|
293
|
+
|
|
288
294
|
cleanupPrescriptionLabelRecording();
|
|
289
295
|
clearTheme();
|
|
290
296
|
|
package/core/state.js
CHANGED
|
@@ -89,8 +89,11 @@ export const state = {
|
|
|
89
89
|
isBlurModal: true,
|
|
90
90
|
isDocumentModal: true,
|
|
91
91
|
isLcdModal: true,
|
|
92
|
+
isDesktopModeModal: true,
|
|
92
93
|
container: null,
|
|
93
94
|
exitButton: false,
|
|
95
|
+
showCameraPermissionDeniedMessage: true,
|
|
96
|
+
cameraPermissionDeniedMessage: MESSAGES.CAMERA_PERMISSION_DENIED,
|
|
94
97
|
onCloseRedirectUrl: null,
|
|
95
98
|
onClose: null,
|
|
96
99
|
resultSetter: null,
|
|
@@ -107,6 +110,15 @@ export const state = {
|
|
|
107
110
|
enforceLcdDetection: false,
|
|
108
111
|
usingOverlayContainer: false,
|
|
109
112
|
|
|
113
|
+
// Produced image size cap (MB), valid range 0.2 - 2.5. null = no cap.
|
|
114
|
+
originalImageMaxSizeInMB: null,
|
|
115
|
+
|
|
116
|
+
// Auto tags added to the submitted document
|
|
117
|
+
autoTagSource: false,
|
|
118
|
+
autoTagDeviceId: false,
|
|
119
|
+
autoTagLensVersion: false,
|
|
120
|
+
autoTagPlatform: false,
|
|
121
|
+
|
|
110
122
|
// Detection thresholds
|
|
111
123
|
blurThreshold: DEFAULTS.BLUR_THRESHOLD,
|
|
112
124
|
lcdThreshold: DEFAULTS.LCD_THRESHOLD,
|
|
@@ -205,6 +217,7 @@ export const state = {
|
|
|
205
217
|
submitButton: null,
|
|
206
218
|
videoOverlay: null,
|
|
207
219
|
captureButton: null,
|
|
220
|
+
cameraPermissionMessage: null,
|
|
208
221
|
exitButtonRef: null,
|
|
209
222
|
longPreview: null,
|
|
210
223
|
cardOverlay: null,
|
|
@@ -370,6 +383,7 @@ export const resetState = () => {
|
|
|
370
383
|
state.isMirrored = false;
|
|
371
384
|
state.cameraFacingMode = 'environment';
|
|
372
385
|
state.captureButton = null;
|
|
386
|
+
state.cameraPermissionMessage = null;
|
|
373
387
|
state.submitButton = null;
|
|
374
388
|
state.exitButtonRef = null;
|
|
375
389
|
state.longPreview = null;
|
package/core/wasm-processing.js
CHANGED
|
@@ -2,6 +2,7 @@ import { state, refs, actions } from './state.js';
|
|
|
2
2
|
import { drawContours, expandCoordinatesWithMargin } from './drawing.js';
|
|
3
3
|
import { releaseCanvas, setCoordinates, setHasCoordinates, setBlurStatus, isBlurryVariance } from './utils.js';
|
|
4
4
|
import { DEFAULTS, WASM } from './constants.js';
|
|
5
|
+
import { canvasToJpegBase64 } from './image-encode.js';
|
|
5
6
|
import { bootstrapTesseract } from './tesseract-ocr.js';
|
|
6
7
|
import { shouldRunFieldDetection } from './field-detection-policy.js';
|
|
7
8
|
|
|
@@ -367,14 +368,14 @@ export const getLongImage = async () => {
|
|
|
367
368
|
const ctx = cropImgCanvas.getContext("2d");
|
|
368
369
|
const imageData = new ImageData(data, width, height);
|
|
369
370
|
ctx.putImageData(imageData, 0, 0);
|
|
370
|
-
const imgString = cropImgCanvas
|
|
371
|
+
const imgString = canvasToJpegBase64(cropImgCanvas);
|
|
371
372
|
state.image = cropImgCanvas;
|
|
372
373
|
state.isDocument = true;
|
|
373
374
|
|
|
374
375
|
stopWasm();
|
|
375
376
|
releaseCanvas(state.boxRef);
|
|
376
377
|
state.coordinates = [];
|
|
377
|
-
return imgString
|
|
378
|
+
return imgString;
|
|
378
379
|
};
|
|
379
380
|
|
|
380
381
|
// Rotate an RGBA crop by the orientation classifier's prediction so the crop
|
|
@@ -427,7 +428,7 @@ const renderCropToBase64 = (cropOut) => {
|
|
|
427
428
|
canvas.width = outputWidth;
|
|
428
429
|
canvas.height = outputHeight;
|
|
429
430
|
canvas.getContext("2d").putImageData(new ImageData(data, outputWidth, outputHeight), 0, 0);
|
|
430
|
-
return canvas
|
|
431
|
+
return canvasToJpegBase64(canvas);
|
|
431
432
|
};
|
|
432
433
|
|
|
433
434
|
/**
|
|
@@ -566,11 +567,11 @@ export const cropWasm = async (calculateBlur = true) => {
|
|
|
566
567
|
}
|
|
567
568
|
stopWasm();
|
|
568
569
|
|
|
569
|
-
const imgString = cropImgCanvas
|
|
570
|
+
const imgString = canvasToJpegBase64(cropImgCanvas);
|
|
570
571
|
state.image = cropImgCanvas;
|
|
571
572
|
releaseCanvas(state.boxRef);
|
|
572
573
|
state.coordinates = [];
|
|
573
|
-
return imgString
|
|
574
|
+
return imgString;
|
|
574
575
|
};
|
|
575
576
|
|
|
576
577
|
export const cropFromAutoCapture = (calculateBlur = true) => {
|
|
@@ -591,9 +592,9 @@ export const cropFromAutoCapture = (calculateBlur = true) => {
|
|
|
591
592
|
state.blurStatus = false;
|
|
592
593
|
}
|
|
593
594
|
|
|
594
|
-
const imgString = cropImgCanvas
|
|
595
|
+
const imgString = canvasToJpegBase64(cropImgCanvas);
|
|
595
596
|
state.image = cropImgCanvas;
|
|
596
597
|
releaseCanvas(state.boxRef);
|
|
597
598
|
state.coordinates = [];
|
|
598
|
-
return imgString
|
|
599
|
+
return imgString;
|
|
599
600
|
};
|
package/package.json
CHANGED
package/veryfi-lens.js
CHANGED
|
@@ -37,6 +37,28 @@ const normalizeCaptureConfig = (config) => {
|
|
|
37
37
|
}
|
|
38
38
|
};
|
|
39
39
|
|
|
40
|
+
// Cap on the size of produced images (still images only, never recorded video).
|
|
41
|
+
// Unset means no cap; anything outside the supported range is rejected.
|
|
42
|
+
const normalizeOriginalImageMaxSizeConfig = (config) => {
|
|
43
|
+
if (config.originalImageMaxSizeInMB === undefined) return;
|
|
44
|
+
const value = config.originalImageMaxSizeInMB;
|
|
45
|
+
// null / 0 both mean "no cap" so a UI slider can express the disabled state.
|
|
46
|
+
if (value === null || value === 0) {
|
|
47
|
+
state.originalImageMaxSizeInMB = null;
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
if (typeof value === 'number' &&
|
|
51
|
+
value >= DEFAULTS.ORIGINAL_IMAGE_MIN_SIZE_MB &&
|
|
52
|
+
value <= DEFAULTS.ORIGINAL_IMAGE_MAX_SIZE_MB) {
|
|
53
|
+
state.originalImageMaxSizeInMB = value;
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
console.warn(
|
|
57
|
+
`originalImageMaxSizeInMB must be a number between ${DEFAULTS.ORIGINAL_IMAGE_MIN_SIZE_MB} and ${DEFAULTS.ORIGINAL_IMAGE_MAX_SIZE_MB}. Ignoring invalid value.`
|
|
58
|
+
);
|
|
59
|
+
state.originalImageMaxSizeInMB = null;
|
|
60
|
+
};
|
|
61
|
+
|
|
40
62
|
const normalizeThemeConfig = (config) => {
|
|
41
63
|
if (config.theme === undefined) return;
|
|
42
64
|
if (isValidTheme(config.theme)) {
|
|
@@ -80,6 +102,7 @@ const VeryfiLens = {
|
|
|
80
102
|
state.multiSessionActive = isMultiSession;
|
|
81
103
|
|
|
82
104
|
normalizeCaptureConfig(config);
|
|
105
|
+
normalizeOriginalImageMaxSizeConfig(config);
|
|
83
106
|
normalizeThemeConfig(config);
|
|
84
107
|
|
|
85
108
|
if (config.lensFlavor !== "caps" && config.lensFlavor !== "code_strips" && config.lensFlavor !== "prescription_label") {
|
|
@@ -199,6 +222,8 @@ const VeryfiLens = {
|
|
|
199
222
|
}
|
|
200
223
|
await flavorModule.setup(state, refs, actions);
|
|
201
224
|
|
|
225
|
+
if (!state.hasInit) return;
|
|
226
|
+
|
|
202
227
|
loadingSpinner.remove();
|
|
203
228
|
|
|
204
229
|
await flavorModule.render(state, refs, actions);
|
|
@@ -265,6 +290,7 @@ const VeryfiLens = {
|
|
|
265
290
|
});
|
|
266
291
|
|
|
267
292
|
normalizeCaptureConfig(config);
|
|
293
|
+
normalizeOriginalImageMaxSizeConfig(config);
|
|
268
294
|
normalizeThemeConfig(config);
|
|
269
295
|
if (state.container) {
|
|
270
296
|
applyTheme(state.container, state.theme);
|