Accept a camera read only when three frames agree

One frame was enough to accept an ISBN, on the theory that the check
digit makes a false read unlikely. About one multi-digit misread in ten
still passes the check, and a single book put two phantom ISBNs in the
queue. A read now has to repeat on three consecutive frames (two in a
capture burst), and a frame that yields two different ISBNs is ignored.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Fable 5.2 committed 2026-11-22 11:58:46 -07:00
1 parent a210576661
commit ddf6020731
5 files changed
+172 -22

No files matched your search

+31 -6
View File
@@ -1,21 +1,44 @@
import { normalizeIsbn } from '../shared/isbn.ts';
export const REPEAT_MS = 5000;
/**
* A barcode read is only believed once the same full code has been decoded on this many separate frames, each
* within AGREE_GAP_MS of the one before. The EAN check digit alone is not enough: a blurred frame sometimes
* misreads two digits so that the check still passes (about one misread in ten), and the same wrong code can
* come up again on the next frame, so two agreeing reads are not safe. Three still land in about 0.3 s on a
* clean barcode.
*/
export const AGREE_READS = 3;
export const AGREE_GAP_MS = 1500;
/** Remembers when each code was last reported, so a barcode held in view is announced once per 5 seconds. */
export interface ReadState {
emittedAt: Record<string, number>;
rejectedAt: Record<string, number>;
/** The ISBN being confirmed: how many frames in a row have read it, and when the latest one did. */
seen: { isbn: string; count: number; at: number } | null;
}
export function newReadState(): ReadState {
return { emittedAt: {}, rejectedAt: {} };
return { emittedAt: {}, rejectedAt: {}, seen: null };
}
/**
* Judge one decoded barcode. A valid ISBN-13 (check digit, 978 or 979 prefix) is accepted at once, with no
* second confirming read: the EAN checksum already makes a false read very unlikely. Anything else
* (a UPC, a price code, a bad check digit) is rejected. Neither is repeated within REPEAT_MS.
* Count one frame's read of a valid ISBN. The count restarts when a different valid ISBN was read in between
* or the last agreeing read is older than AGREE_GAP_MS. `agreed` is true once `need` frames in a row agree.
*/
export function agree(state: ReadState, isbn: string, now: number, need: number = AGREE_READS): { state: ReadState; agreed: boolean } {
const s = state.seen;
const count = s && s.isbn === isbn && now - s.at <= AGREE_GAP_MS ? s.count + 1 : 1;
if (count >= need) return { state: { ...state, seen: null }, agreed: true };
return { state: { ...state, seen: { isbn, count, at: now } }, agreed: false };
}
/**
* Judge one decoded barcode, one call per frame. A 13-digit code with a correct check digit and a 978 or 979
* prefix counts toward AGREE_READS agreeing frames and is emitted on the last of them; the check digit is only
* ever verified, never computed. Anything else (a UPC, a price code, a bad check digit) is rejected. Neither is
* repeated within REPEAT_MS.
*/
export function acceptRead(
state: ReadState, code: string, now: number,
@@ -27,8 +50,10 @@ export function acceptRead(
return { state: { ...state, rejectedAt: { ...state.rejectedAt, [code]: now } }, emit: null, reject: true };
}
const last = state.emittedAt[isbn];
if (last !== undefined && now - last < REPEAT_MS) return { state, emit: null, reject: false };
return { state: { ...state, emittedAt: { ...state.emittedAt, [isbn]: now } }, emit: isbn, reject: false };
if (last !== undefined && now - last < REPEAT_MS) return { state: { ...state, seen: null }, emit: null, reject: false };
const a = agree(state, isbn, now);
if (!a.agreed) return { state: a.state, emit: null, reject: false };
return { state: { ...a.state, emittedAt: { ...a.state.emittedAt, [isbn]: now } }, emit: isbn, reject: false };
}
/** Record that an ISBN was just reported by some other path (the Capture button), so the loop stays quiet. */