Add QR codes to cards, stop inventing call number parts
Cards now carry a QR code holding the book's own record (call number, title, authors, ISBN, year), and the scanner reads it back to find or add that book. Call number selection no longer pads catalogue numbers with generated cutters. A number is "exact" only when it comes verbatim from a record for this edition; a bare class is a hint that steers the other-edition and similar-books lookups; a record fetched by LCCN must list the book's ISBN to count as this edition. "Look up again" bypasses every cached result, and shelf order breaks ties by author then title. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
6cec404e21
commit
ad2956e868
56 files changed
+4020
-156
No files matched your search
+23
-11
@@ -1,10 +1,12 @@
|
||||
import { readBarcodes } from 'zxing-wasm/reader';
|
||||
import type { ReaderOptions } from 'zxing-wasm/reader';
|
||||
import { normalizeIsbn } from '../shared/isbn.ts';
|
||||
import { parseCardCode } from '../shared/cardcode.ts';
|
||||
|
||||
/** RGBA pixels, as from ImageData. Plain data so that this module runs in Node tests. */
|
||||
export interface Pixels { data: Uint8ClampedArray | Uint8Array; width: number; height: number }
|
||||
export interface DecodeOutcome { isbn: string | null; other: string | null }
|
||||
/** `card` is the text of a Booksorter card's QR code; QR codes that are not cards are dropped without a trace. */
|
||||
export interface DecodeOutcome { isbn: string | null; other: string | null; card: string | null }
|
||||
|
||||
// ---- image operations (grey, one byte per pixel) -------------------------------------------------------
|
||||
|
||||
@@ -86,6 +88,12 @@ export const BASE: ReaderOptions = {
|
||||
formats: ['EAN13', 'UPCA'], tryHarder: true, tryDownscale: true, tryRotate: false, tryInvert: false, maxNumberOfSymbols: 2,
|
||||
};
|
||||
|
||||
/**
|
||||
* QR codes are looked for only on the variants that keep 2D detail (plain and enlarged); the row-averaged ones
|
||||
* smear a QR code to nothing and stay EAN-only, so their speed and reliability are as before.
|
||||
*/
|
||||
const WITH_QR: ReaderOptions = { ...BASE, formats: ['EAN13', 'UPCA', 'QRCode'] };
|
||||
|
||||
export interface Variant { name: string; opts: ReaderOptions; prep: (g: Grey) => Grey }
|
||||
const rowSmooth = (g: Grey) => upscale(rowAverage(g, 6), 2);
|
||||
|
||||
@@ -97,18 +105,19 @@ const rowSmooth = (g: Grey) => upscale(rowAverage(g, 6), 2);
|
||||
export const VARIANTS: Variant[] = [
|
||||
{ name: 'rows+sharp2x', opts: { ...BASE, binarizer: 'LocalAverage' }, prep: g => sharpenAcross(rowSmooth(g), 3, 1.2) },
|
||||
{ name: 'rows2x', opts: { ...BASE, binarizer: 'GlobalHistogram' }, prep: rowSmooth },
|
||||
{ name: 'crop', opts: { ...BASE, binarizer: 'LocalAverage' }, prep: g => g },
|
||||
{ name: 'up2x', opts: { ...BASE, binarizer: 'GlobalHistogram' }, prep: g => upscale(g, 2) },
|
||||
{ name: 'up3x', opts: { ...BASE, binarizer: 'LocalAverage' }, prep: g => upscale(g, 3) },
|
||||
{ name: 'crop', opts: { ...WITH_QR, binarizer: 'LocalAverage' }, prep: g => g },
|
||||
{ name: 'up2x', opts: { ...WITH_QR, binarizer: 'GlobalHistogram' }, prep: g => upscale(g, 2) },
|
||||
{ name: 'up3x', opts: { ...WITH_QR, binarizer: 'LocalAverage' }, prep: g => upscale(g, 3) },
|
||||
];
|
||||
/** For the whole frame, which is too big to enlarge: no upscaling. */
|
||||
export const WHOLE_VARIANTS: Variant[] = [
|
||||
{ name: 'whole', opts: { ...BASE, binarizer: 'LocalAverage' }, prep: g => g },
|
||||
{ name: 'whole', opts: { ...WITH_QR, binarizer: 'LocalAverage' }, prep: g => g },
|
||||
{ name: 'whole-rows', opts: { ...BASE, binarizer: 'GlobalHistogram' }, prep: g => sharpenAcross(rowAverage(g, 6), 1, 1) },
|
||||
];
|
||||
|
||||
function judge(codes: { text: string }[], into: DecodeOutcome): void {
|
||||
function judge(codes: { text: string; format?: string }[], into: DecodeOutcome): void {
|
||||
for (const c of codes) {
|
||||
if (c.format === 'QRCode') { if (parseCardCode(c.text)) into.card ??= c.text; continue; }
|
||||
const isbn = /^\d{13}$/.test(c.text) ? normalizeIsbn(c.text) : null;
|
||||
if (isbn) into.isbn ??= isbn; else into.other ??= c.text;
|
||||
}
|
||||
@@ -118,6 +127,9 @@ async function tryVariant(g: Grey, v: Variant, extra: ReaderOptions, into: Decod
|
||||
judge(await readBarcodes(toImage(v.prep(g)) as ImageData, { ...v.opts, ...extra }), into);
|
||||
}
|
||||
|
||||
/** A book ISBN or a card was read: nothing more to try on this frame. */
|
||||
const found = (o: DecodeOutcome): boolean => o.isbn !== null || o.card !== null;
|
||||
|
||||
export interface LiveState { round: number; cursor: number }
|
||||
export const newLiveState = (): LiveState => ({ round: 0, cursor: 0 });
|
||||
/** Every FULL_EVERY-th live round looks at the whole frame instead of the crop. */
|
||||
@@ -134,14 +146,14 @@ export const wantsFull = (st: LiveState): boolean => st.round % FULL_EVERY === F
|
||||
* stopped, so that slow machines still get through every variant across frames.
|
||||
*/
|
||||
export async function decodeLive(p: Pixels, st: LiveState, now: () => number = () => performance.now()): Promise<{ out: DecodeOutcome; state: LiveState }> {
|
||||
const out: DecodeOutcome = { isbn: null, other: null };
|
||||
const out: DecodeOutcome = { isbn: null, other: null, card: null };
|
||||
const began = now(), g = toGrey(p);
|
||||
let cursor = st.cursor;
|
||||
if (wantsFull(st)) {
|
||||
for (const v of WHOLE_VARIANTS) if (!out.isbn) await tryVariant(g, v, {}, out);
|
||||
for (const v of WHOLE_VARIANTS) if (!found(out)) await tryVariant(g, v, {}, out);
|
||||
} else {
|
||||
await tryVariant(g, VARIANTS[0]!, {}, out);
|
||||
for (let i = 0; i < REST.length && !out.isbn && now() - began <= LIVE_BUDGET_MS; i++) {
|
||||
for (let i = 0; i < REST.length && !found(out) && now() - began <= LIVE_BUDGET_MS; i++) {
|
||||
await tryVariant(g, REST[cursor]!, {}, out);
|
||||
cursor = (cursor + 1) % REST.length;
|
||||
}
|
||||
@@ -151,12 +163,12 @@ export async function decodeLive(p: Pixels, st: LiveState, now: () => number = (
|
||||
|
||||
/** Everything we have on one frame, for the Capture burst: every variant, rotated too, crop then whole frame. */
|
||||
export async function decodeThorough(crop: Pixels, whole: Pixels | null): Promise<DecodeOutcome> {
|
||||
const out: DecodeOutcome = { isbn: null, other: null };
|
||||
const out: DecodeOutcome = { isbn: null, other: null, card: null };
|
||||
for (const p of whole ? [crop, whole] : [crop]) {
|
||||
const g = toGrey(p);
|
||||
for (const v of p === crop ? VARIANTS : WHOLE_VARIANTS) {
|
||||
await tryVariant(g, v, { tryRotate: true, tryInvert: true }, out);
|
||||
if (out.isbn) return out;
|
||||
if (found(out)) return out;
|
||||
}
|
||||
}
|
||||
return out;
|
||||
|
||||
Reference in new issue
Block a user