34 lines
1.6 KiB
TypeScript
34 lines
1.6 KiB
TypeScript
/**
|
|
* 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 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 };
|
|
}
|
|
|
|
/** 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 };
|
|
}
|