Mirror and zoom the scan preview, strengthen barcode decoding, save failed captures

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Fable 5.2 committed 2026-11-18 10:28:45 -07:00
1 parent b5defb4e4f
commit 308c0e1ed3
10 files changed
+510 -152

No files matched your search

+27 -9
View File
@@ -1,15 +1,33 @@
/** Where the barcode is expected, as fractions of the video frame. The overlay box is drawn from the same numbers. */
export const TARGET = { x: 0.1, y: 0.27, w: 0.8, h: 0.46 };
/**
* Geometry of the scan view. The preview shows the centre of the camera frame magnified ZOOM times, mirrored,
* so the target box is drawn in display fractions, while decoding works on the real (unmirrored) frame.
*/
export const ZOOM = 2.5;
/** The target box as fractions of the displayed width; its shape is a barcode's, 3:2 (wide:tall). */
export const BOX_W = 0.38;
export const BOX_RATIO = 3 / 2;
export interface Rect { x: number; y: number; w: number; h: number }
/** The target box in pixels of a frame of the given size. */
export function cropRect(width: number, height: number): Rect {
const x = Math.round(width * TARGET.x), y = Math.round(height * TARGET.y);
return { x, y, w: Math.min(width - x, Math.round(width * TARGET.w)), h: Math.min(height - y, Math.round(height * TARGET.h)) };
/** The target box as percentages of the displayed video, for a stream of the given width/height ratio. */
export function targetBox(aspect: number): { left: number; top: number; width: number; height: number } {
const height = Math.min(0.9, (BOX_W / BOX_RATIO) * aspect);
return { left: ((1 - BOX_W) / 2) * 100, top: ((1 - height) / 2) * 100, width: BOX_W * 100, height: height * 100 };
}
/** A small crop is enlarged so that thin bars cover enough pixels for the decoder: 2x below 1000 px wide. */
export function upscaleFor(cropWidth: number): number {
return cropWidth < 1000 ? 2 : 1;
/** The target box in pixels of the real frame (about 15 percent of its width). */
export function boxRect(width: number, height: number): Rect {
const w = Math.round((width * BOX_W) / ZOOM), h = Math.min(height, Math.round(w / BOX_RATIO));
return centred(width, height, w, h);
}
/** The decode region: the target box doubled in each direction, because nobody aims perfectly. */
export function cropRect(width: number, height: number): Rect {
const b = boxRect(width, height);
return centred(width, height, Math.min(width, b.w * 2), Math.min(height, b.h * 2));
}
function centred(width: number, height: number, w: number, h: number): Rect {
return { x: Math.round((width - w) / 2), y: Math.round((height - h) / 2), w, h };
}