veryfi-lens-wasm 3.3.2 → 3.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -17,7 +17,7 @@ Veryfi Lens for Web is a browser SDK that gives your web app document capture su
17
17
  | `credit_card` | Credit card scanning (name, number, date, CVV) |
18
18
  | `caps` | Bottle cap capture with circular guide overlay |
19
19
  | `code_strips` | Code strip capture with rectangular guide overlay |
20
- | `prescription_label` | Prescription label video capture with pill bottle overlay, zips video and submits to anydocs |
20
+ | `prescription_label` | Prescription label capture, submits to anydocs. Switch overlays during capture: bottle overlay records video, box overlay captures a still image |
21
21
  | `upload` | File upload with crop and submit (no camera) |
22
22
 
23
23
  ## Installation
@@ -236,7 +236,10 @@ VeryfiLens.onFailure((error) => { /* error occurred */ });
236
236
  | `prescriptionLabelSquareHeightRatio` | `number` | `0.5` | Square overlay height (0-1 relative to viewport) |
237
237
  | `prescriptionLabelShowGuide` | `boolean` | `false` | Show a semi-transparent guide explaining how to record the label. Auto-hides when recording starts and reappears on retake |
238
238
  | `prescriptionLabelGuideText` | `string` | `"Position the bottle within the outline.\n\nPress the capture button to start recording, slowly rotate the bottle to capture the entire label and press the capture button again to stop recording."` | Guide text shown when the bottle outline overlay is active. Use `\n` for line breaks |
239
- | `prescriptionLabelSquareGuideText` | `string` | `"Position the prescription label within the square.\n\nPress the capture button to start recording, press again to stop."` | Guide text shown when the square overlay is active. Use `\n` for line breaks |
239
+ | `prescriptionLabelSquareGuideText` | `string` | `"Position the prescription label within the square.\n\nPress the capture button to take a photo of the label."` | Guide text shown when the box overlay is active. Use `\n` for line breaks |
240
+ | `prescriptionLabelShowOverlayToggle` | `boolean` | `true` | Show a toggle that lets the user switch between the bottle and box overlays during capture. The **bottle** overlay records a video; the **box** overlay captures a still image |
241
+ | `prescriptionLabelBottleLabel` | `string` | `"Bottle"` | Label for the bottle (video) segment of the overlay toggle |
242
+ | `prescriptionLabelBoxLabel` | `string` | `"Box"` | Label for the box (image) segment of the overlay toggle |
240
243
 
241
244
  ### Upload Settings (`upload` flavor)
242
245
 
@@ -67,6 +67,7 @@ export const createPrescriptionLabelGuide = (container, options = {}) => {
67
67
 
68
68
  return {
69
69
  element: guide,
70
+ setText: (newText) => { textEl.textContent = newText || ''; },
70
71
  show: () => { guide.style.display = 'block'; },
71
72
  hide: () => { guide.style.display = 'none'; },
72
73
  remove: () => {
@@ -0,0 +1,75 @@
1
+ export const createPrescriptionOverlayToggleButton = (container, options = {}) => {
2
+ const bottleLabel = options.bottleLabel || 'Bottle';
3
+ const boxLabel = options.boxLabel || 'Box';
4
+ let active = options.initial === 'box' ? 'box' : 'bottle';
5
+ const onToggle = typeof options.onToggle === 'function' ? options.onToggle : () => {};
6
+
7
+ const wrapper = document.createElement('div');
8
+ wrapper.id = 'veryfi-prescription-overlay-toggle';
9
+ Object.assign(wrapper.style, {
10
+ position: 'absolute',
11
+ top: '20px',
12
+ left: '50%',
13
+ transform: 'translateX(-50%)',
14
+ display: 'flex',
15
+ gap: '4px',
16
+ padding: '4px',
17
+ backgroundColor: 'rgba(255, 255, 255, 0.15)',
18
+ border: '1px solid rgba(255, 255, 255, 0.4)',
19
+ borderRadius: '25px',
20
+ backdropFilter: 'blur(8px)',
21
+ WebkitBackdropFilter: 'blur(8px)',
22
+ zIndex: '1004',
23
+ pointerEvents: 'auto',
24
+ });
25
+
26
+ const makeSegment = (label, type) => {
27
+ const btn = document.createElement('button');
28
+ btn.type = 'button';
29
+ btn.textContent = label;
30
+ btn.dataset.type = type;
31
+ Object.assign(btn.style, {
32
+ border: 'none',
33
+ outline: 'none',
34
+ cursor: 'pointer',
35
+ borderRadius: '20px',
36
+ padding: '8px 18px',
37
+ fontSize: '14px',
38
+ fontWeight: '600',
39
+ fontFamily: "system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif",
40
+ color: 'white',
41
+ backgroundColor: 'transparent',
42
+ transition: 'background-color 0.2s ease',
43
+ });
44
+ btn.addEventListener('click', () => {
45
+ if (active === type) return;
46
+ active = type;
47
+ updateSegments();
48
+ onToggle(active);
49
+ });
50
+ return btn;
51
+ };
52
+
53
+ const bottleBtn = makeSegment(bottleLabel, 'bottle');
54
+ const boxBtn = makeSegment(boxLabel, 'box');
55
+
56
+ const updateSegments = () => {
57
+ [[bottleBtn, 'bottle'], [boxBtn, 'box']].forEach(([btn, type]) => {
58
+ btn.style.backgroundColor = active === type ? 'rgba(255, 255, 255, 0.35)' : 'transparent';
59
+ });
60
+ };
61
+
62
+ wrapper.appendChild(bottleBtn);
63
+ wrapper.appendChild(boxBtn);
64
+ updateSegments();
65
+ container.appendChild(wrapper);
66
+
67
+ return {
68
+ element: wrapper,
69
+ getActive: () => active,
70
+ setActive: (type) => { active = type === 'box' ? 'box' : 'bottle'; updateSegments(); },
71
+ show: () => { wrapper.style.display = 'flex'; },
72
+ hide: () => { wrapper.style.display = 'none'; },
73
+ remove: () => { if (wrapper.parentNode) wrapper.parentNode.removeChild(wrapper); },
74
+ };
75
+ };
@@ -1,9 +1,10 @@
1
1
  import { state } from '../state.js';
2
- import { handleSubmit, addPackageScan, ensurePackageUploaded } from '../api.js';
2
+ import { handleSubmit, addPackageScan, ensurePackageUploaded, docReady } from '../api.js';
3
3
  import { hideVideoOverlay, showVideoOverlay } from '../overlay-controls.js';
4
4
  import { removeOverlay, newStartLens } from '../lifecycle.js';
5
5
  import { promptForAnotherCapture } from '../multi-submission.js';
6
6
  import { createSubmitButton } from '../../components/submitButton.js';
7
+ import { DEFAULTS } from '../constants.js';
7
8
 
8
9
  let previewRecorder = null;
9
10
  let previewChunks = [];
@@ -134,7 +135,7 @@ const getRecorderMimeType = () => {
134
135
  };
135
136
 
136
137
  const computeOverlayCropRect = (vw, vh) => {
137
- if (state.prescriptionLabelShowSquareOverlay) {
138
+ if (state.prescriptionLabelActiveOverlay === 'box') {
138
139
  const w = vw * state.prescriptionLabelSquareWidthRatio;
139
140
  const h = vh * state.prescriptionLabelSquareHeightRatio;
140
141
  const cx = vw / 2;
@@ -142,15 +143,11 @@ const computeOverlayCropRect = (vw, vh) => {
142
143
  return { x: cx - w / 2, y: cy - h / 2, width: w, height: h };
143
144
  }
144
145
 
145
- if (state.prescriptionLabelShowOverlay) {
146
- const bodyW = vw * state.prescriptionLabelWidthRatio;
147
- const totalH = vh * state.prescriptionLabelHeightRatio;
148
- const cx = vw / 2;
149
- const cy = vh * 0.46;
150
- return { x: cx - bodyW / 2, y: cy - totalH / 2, width: bodyW, height: totalH };
151
- }
152
-
153
- return null;
146
+ const bodyW = vw * state.prescriptionLabelWidthRatio;
147
+ const totalH = vh * state.prescriptionLabelHeightRatio;
148
+ const cx = vw / 2;
149
+ const cy = vh * 0.46;
150
+ return { x: cx - bodyW / 2, y: cy - totalH / 2, width: bodyW, height: totalH };
154
151
  };
155
152
 
156
153
  const computeSourceCropRegion = (videoElement) => {
@@ -273,6 +270,7 @@ const startRecording = () => {
273
270
  isRecording = true;
274
271
  updateRecordingUI(true);
275
272
  state.prescriptionLabelGuide?.hide?.();
273
+ state.prescriptionOverlayToggleButton?.hide?.();
276
274
  state.updateHandler?.({ status: 'recording_started' });
277
275
  state.debug_mode && console.log('[EVENT] prescription label recording started');
278
276
  };
@@ -457,6 +455,7 @@ const showVideoPreview = (videoBlob) => {
457
455
  }
458
456
  showVideoOverlay();
459
457
  state.prescriptionLabelGuide?.show?.();
458
+ state.prescriptionOverlayToggleButton?.show?.();
460
459
  await newStartLens();
461
460
  });
462
461
  containerEl.appendChild(retakeButtonEl);
@@ -497,7 +496,133 @@ const submitVideoBlob = async (videoBlob) => {
497
496
  return submitResult;
498
497
  };
499
498
 
499
+ const submitPrescriptionImage = async (imageBase64) => {
500
+ state.updateHandler?.({ status: 'processing_started' });
501
+ state.updateHandler?.({ status: 'processing_completed' });
502
+
503
+ let submitResult;
504
+ if (typeof state.customSubmitHandler === 'function') {
505
+ const packageInfo = await ensurePackageUploaded();
506
+ state.customSubmitHandler(imageBase64, packageInfo);
507
+ submitResult = imageBase64;
508
+ } else {
509
+ submitResult = await handleSubmit(imageBase64);
510
+ }
511
+
512
+ state.updateHandler?.({ status: 'capture_completed' });
513
+ return submitResult;
514
+ };
515
+
516
+ const capturePrescriptionImage = async () => {
517
+ try {
518
+ state.debug_mode && console.log('[EVENT] capture prescription label image');
519
+ state.updateHandler?.({ status: 'capture_started' });
520
+
521
+ const videoElement = state.videoRef;
522
+ if (!videoElement) {
523
+ throw new Error('Video reference not initialized');
524
+ }
525
+
526
+ const region = computeSourceCropRegion(videoElement);
527
+ if (!region) {
528
+ throw new Error('Failed to compute prescription label crop region');
529
+ }
530
+
531
+ const cropImgRef = state.cropImgRef;
532
+ if (!cropImgRef) {
533
+ throw new Error('Crop image reference not initialized');
534
+ }
535
+
536
+ cropImgRef.width = Math.max(2, Math.round(region.srcW));
537
+ cropImgRef.height = Math.max(2, Math.round(region.srcH));
538
+ const ctx = cropImgRef.getContext('2d');
539
+ ctx.clearRect(0, 0, cropImgRef.width, cropImgRef.height);
540
+ ctx.drawImage(
541
+ videoElement,
542
+ region.srcX, region.srcY, region.srcW, region.srcH,
543
+ 0, 0, cropImgRef.width, cropImgRef.height
544
+ );
545
+
546
+ state.finalImage = cropImgRef.toDataURL('image/jpeg', DEFAULTS.IMAGE_QUALITY).split(',')[1];
547
+ if (!state.finalImage) {
548
+ throw new Error('Failed to capture prescription label image');
549
+ }
550
+
551
+ state.isDocument = true;
552
+ hideVideoOverlay();
553
+ state.prescriptionSquareOverlay?.hide?.();
554
+ state.prescriptionLabelOverlay?.hide?.();
555
+ state.prescriptionOverlayToggleButton?.hide?.();
556
+ state.prescriptionLabelGuide?.hide?.();
557
+ state.exitButtonRef?.hide?.();
558
+ state.videoRef?.srcObject?.getTracks?.().forEach((track) => track.stop());
559
+
560
+ state.cropImgRef?.setRectangleDisplay?.();
561
+ state.cropImgRef?.showSuccessBorder?.();
562
+
563
+ // Per-capture upload, parity with image flavors' docReady() on crop accept:
564
+ // - packageMode: true => accumulate scan into state.packageScans
565
+ // - packageMode: false => POST base64 to /rest/send-cropped now
566
+ state.updateHandler?.({ status: 'processing_started' });
567
+ state.docReadyResult = await docReady();
568
+ state.updateHandler?.({ status: 'processing_completed' });
569
+
570
+ const restartPrescription = async () => {
571
+ if (state.cropImgRef) {
572
+ const previewImg = state.cropImgRef.previewImg;
573
+ if (previewImg) {
574
+ previewImg.style.display = 'none';
575
+ previewImg.src = '';
576
+ }
577
+ state.cropImgRef.hideBorder?.();
578
+ const cctx = state.cropImgRef.getContext('2d');
579
+ cctx?.clearRect(0, 0, state.cropImgRef.width, state.cropImgRef.height);
580
+ }
581
+ showVideoOverlay();
582
+ state.prescriptionSquareOverlay?.show?.();
583
+ state.prescriptionLabelGuide?.show?.();
584
+ state.prescriptionOverlayToggleButton?.show?.();
585
+ await newStartLens();
586
+ };
587
+
588
+ state.submitButton = createSubmitButton(
589
+ state.container,
590
+ async () => {
591
+ const result = await submitPrescriptionImage(state.finalImage);
592
+ if (state.multiSubmission && result) {
593
+ state.submitButton?.hide?.();
594
+ promptForAnotherCapture(restartPrescription);
595
+ } else {
596
+ removeOverlay();
597
+ }
598
+ },
599
+ () => {},
600
+ { autoClose: false }
601
+ );
602
+ state.submitButton.show();
603
+
604
+ if (state.packageMode && !state.enableSubmit) {
605
+ const packageInfo = await ensurePackageUploaded();
606
+ state.resultSetter?.({ ...(packageInfo || {}), image: state.finalImage });
607
+ } else {
608
+ state.resultSetter?.(state.finalImage);
609
+ }
610
+
611
+ state.updateHandler?.({ status: 'capture_completed' });
612
+ return state.finalImage;
613
+ } catch (error) {
614
+ console.error('Prescription label image capture failed:', error.message);
615
+ state.failureHandler?.(error);
616
+ state.updateHandler?.({ status: 'capture_failed' });
617
+ return null;
618
+ }
619
+ };
620
+
500
621
  export const capturePrescriptionLabel = async () => {
622
+ if (state.prescriptionLabelActiveOverlay === 'box') {
623
+ return capturePrescriptionImage();
624
+ }
625
+
501
626
  try {
502
627
  if (!isRecording) {
503
628
  state.debug_mode && console.log('[EVENT] starting prescription label recording');
@@ -539,6 +664,7 @@ export const capturePrescriptionLabel = async () => {
539
664
  cleanupPreview();
540
665
  showVideoOverlay();
541
666
  state.prescriptionLabelGuide?.show?.();
667
+ state.prescriptionOverlayToggleButton?.show?.();
542
668
  await newStartLens();
543
669
  };
544
670
 
package/core/constants.js CHANGED
@@ -86,7 +86,9 @@ export const MESSAGES = {
86
86
  BOTTLE_CAP: 'Position bottle cap in circle',
87
87
  PRESCRIPTION_LABEL: 'Position prescription label in outline',
88
88
  PRESCRIPTION_LABEL_GUIDE: 'Position the bottle within the outline.\n\nPress the capture button to start recording, slowly rotate the bottle to capture the entire label and press the capture button again to stop recording.',
89
- PRESCRIPTION_SQUARE_GUIDE: 'Position the prescription label within the square.\n\nPress the capture button to start recording, press again to stop.',
89
+ PRESCRIPTION_SQUARE_GUIDE: 'Position the prescription label within the square.\n\nPress the capture button to take a photo of the label.',
90
+ PRESCRIPTION_TOGGLE_BOTTLE: 'Bottle',
91
+ PRESCRIPTION_TOGGLE_BOX: 'Box',
90
92
  CODE_STRIPS: 'Position code strip in rectangle',
91
93
  CHECK_POSITION: 'We could not detect a check, hope this will make it easier',
92
94
  ANYDOC_ADD_PAGE: 'Add Page',
package/core/state.js CHANGED
@@ -214,6 +214,11 @@ export const state = {
214
214
  prescriptionLabelGuideText: MESSAGES.PRESCRIPTION_LABEL_GUIDE,
215
215
  prescriptionLabelSquareGuideText: MESSAGES.PRESCRIPTION_SQUARE_GUIDE,
216
216
  prescriptionLabelGuide: null,
217
+ prescriptionLabelActiveOverlay: null,
218
+ prescriptionLabelShowOverlayToggle: true,
219
+ prescriptionLabelBottleLabel: MESSAGES.PRESCRIPTION_TOGGLE_BOTTLE,
220
+ prescriptionLabelBoxLabel: MESSAGES.PRESCRIPTION_TOGGLE_BOX,
221
+ prescriptionOverlayToggleButton: null,
217
222
  preferredVideoFormat: null,
218
223
 
219
224
  codeStripsOverlay: null,
@@ -333,6 +338,8 @@ export const resetState = () => {
333
338
  state.prescriptionLabelOverlay = null;
334
339
  state.prescriptionSquareOverlay = null;
335
340
  state.prescriptionLabelGuide = null;
341
+ state.prescriptionLabelActiveOverlay = null;
342
+ state.prescriptionOverlayToggleButton = null;
336
343
  state.codeStripsOverlay = null;
337
344
  state.checkOverlay = null;
338
345
  state.isManualCheckCapture = false;
@@ -20,6 +20,7 @@ import { createCodeStripsOverlay } from '../components/codeStripsOverlay.js';
20
20
  import { createPrescriptionLabelOverlay } from '../components/prescriptionLabelOverlay.js';
21
21
  import { createPrescriptionSquareOverlay } from '../components/prescriptionSquareOverlay.js';
22
22
  import { createPrescriptionLabelGuide } from '../components/prescriptionLabelGuide.js';
23
+ import { createPrescriptionOverlayToggleButton } from '../components/prescriptionOverlayToggleButton.js';
23
24
  import { createMirrorButton } from '../components/mirrorButton.js';
24
25
  import { createSwitchCameraButton } from '../components/switchCameraButton.js';
25
26
 
@@ -69,19 +70,22 @@ const setupVideoOverlayControls = (captureAction) => {
69
70
  renderWatermarkIfNeeded();
70
71
  };
71
72
 
72
- const createSpecialtyOverlayOnReady = (overlayKey, createFn, options) => {
73
+ const createSpecialtyOverlayOnReady = (overlayKey, createFn, options, onReady) => {
73
74
  const videoElement = document.getElementById("veryfi-video-ref");
74
75
  if (!videoElement) return;
75
76
 
76
- if (videoElement.readyState >= 2) {
77
+ const create = () => {
77
78
  if (!state[overlayKey]) {
78
79
  state[overlayKey] = createFn(state.container, options);
79
80
  }
81
+ onReady?.();
82
+ };
83
+
84
+ if (videoElement.readyState >= 2) {
85
+ create();
80
86
  } else {
81
87
  const handleVideoLoad = () => {
82
- if (!state[overlayKey]) {
83
- state[overlayKey] = createFn(state.container, options);
84
- }
88
+ create();
85
89
  videoElement.removeEventListener('loadeddata', handleVideoLoad);
86
90
  };
87
91
  videoElement.addEventListener('loadeddata', handleVideoLoad);
@@ -293,23 +297,47 @@ export const renderPrescriptionLabelOverlayIfNeeded = () => {
293
297
  state.debug_mode && console.log("Rendering prescription label overlay");
294
298
  setupVideoOverlayControls(() => actions.capturePrescriptionLabel());
295
299
 
296
- if (state.prescriptionLabelShowOverlay) {
300
+ const toggleEnabled = state.prescriptionLabelShowOverlayToggle;
301
+ // When the toggle is enabled both overlays must be switchable regardless of
302
+ // the individual show flags; otherwise fall back to the legacy per-flag show.
303
+ const bottleEnabled = toggleEnabled || state.prescriptionLabelShowOverlay;
304
+ const boxEnabled = toggleEnabled || state.prescriptionLabelShowSquareOverlay;
305
+
306
+ if (!state.prescriptionLabelActiveOverlay) {
307
+ state.prescriptionLabelActiveOverlay =
308
+ (state.prescriptionLabelShowSquareOverlay && !state.prescriptionLabelShowOverlay) ? 'box' : 'bottle';
309
+ }
310
+
311
+ const applyOverlayVisibility = () => {
312
+ if (state.prescriptionLabelOverlay) {
313
+ state.prescriptionLabelActiveOverlay === 'bottle'
314
+ ? state.prescriptionLabelOverlay.show()
315
+ : state.prescriptionLabelOverlay.hide();
316
+ }
317
+ if (state.prescriptionSquareOverlay) {
318
+ state.prescriptionLabelActiveOverlay === 'box'
319
+ ? state.prescriptionSquareOverlay.show()
320
+ : state.prescriptionSquareOverlay.hide();
321
+ }
322
+ };
323
+
324
+ if (bottleEnabled) {
297
325
  createSpecialtyOverlayOnReady('prescriptionLabelOverlay', createPrescriptionLabelOverlay, {
298
326
  bottleWidthRatio: state.prescriptionLabelWidthRatio,
299
327
  bottleHeightRatio: state.prescriptionLabelHeightRatio,
300
328
  instructionText: state.prescriptionLabelText
301
- });
329
+ }, applyOverlayVisibility);
302
330
  }
303
331
 
304
- if (state.prescriptionLabelShowSquareOverlay) {
332
+ if (boxEnabled) {
305
333
  createSpecialtyOverlayOnReady('prescriptionSquareOverlay', createPrescriptionSquareOverlay, {
306
334
  squareWidthRatio: state.prescriptionLabelSquareWidthRatio,
307
335
  squareHeightRatio: state.prescriptionLabelSquareHeightRatio,
308
- });
336
+ }, applyOverlayVisibility);
309
337
  }
310
338
 
311
339
  if (state.prescriptionLabelShowGuide && !state.prescriptionLabelGuide) {
312
- const guideText = state.prescriptionLabelShowSquareOverlay
340
+ const guideText = state.prescriptionLabelActiveOverlay === 'box'
313
341
  ? state.prescriptionLabelSquareGuideText
314
342
  : state.prescriptionLabelGuideText;
315
343
  state.prescriptionLabelGuide = createPrescriptionLabelGuide(
@@ -317,6 +345,25 @@ export const renderPrescriptionLabelOverlayIfNeeded = () => {
317
345
  { text: guideText }
318
346
  );
319
347
  }
348
+
349
+ if (toggleEnabled && bottleEnabled && boxEnabled && !state.prescriptionOverlayToggleButton) {
350
+ state.prescriptionOverlayToggleButton = createPrescriptionOverlayToggleButton(
351
+ state.videoOverlay?.overlay || state.container,
352
+ {
353
+ initial: state.prescriptionLabelActiveOverlay,
354
+ bottleLabel: state.prescriptionLabelBottleLabel,
355
+ boxLabel: state.prescriptionLabelBoxLabel,
356
+ onToggle: (type) => {
357
+ state.prescriptionLabelActiveOverlay = type;
358
+ applyOverlayVisibility();
359
+ const text = type === 'box'
360
+ ? state.prescriptionLabelSquareGuideText
361
+ : state.prescriptionLabelGuideText;
362
+ state.prescriptionLabelGuide?.setText?.(text);
363
+ },
364
+ }
365
+ );
366
+ }
320
367
  };
321
368
 
322
369
  export const renderCodeStripsOverlayIfNeeded = () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "veryfi-lens-wasm",
3
- "version": "3.3.2",
3
+ "version": "3.3.3",
4
4
  "description": "Lens For Web SDK with WASM",
5
5
  "main": "veryfi-lens.js",
6
6
  "type": "module",