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:
1 parent
b5defb4e4f
commit
308c0e1ed3
10 files changed
+510
-152
No files matched your search
+18
-11
@@ -5,7 +5,7 @@ import { api } from '../api.ts';
|
||||
import { getBook, upsertBook, useBooks } from '../live.ts';
|
||||
import { startScanner } from '../scanner.ts';
|
||||
import type { Scanner } from '../scanner.ts';
|
||||
import { TARGET } from '../frame.ts';
|
||||
import { targetBox, ZOOM } from '../frame.ts';
|
||||
import { beepAccept, beepLow, unlockAudio } from '../sound.ts';
|
||||
import { emptySession, touch } from '../session.ts';
|
||||
import type { SessionState } from '../session.ts';
|
||||
@@ -38,7 +38,8 @@ type Cam = 'off' | 'starting' | 'on';
|
||||
type Tone = 'info' | 'ok' | 'warn';
|
||||
interface Status { text: string; tone: Tone; hold: boolean }
|
||||
|
||||
const NOT_FOUND = 'No barcode found in that picture. Move closer, add light, or type the ISBN below.';
|
||||
const NOT_FOUND = 'No barcode found. Try again, or type the ISBN below.';
|
||||
const NOT_FOUND_SAVED = 'No barcode found. Saved the picture to data/captures so the reader can be tuned.';
|
||||
|
||||
export function Scan() {
|
||||
useBooks(); // re-render on live updates
|
||||
@@ -54,6 +55,7 @@ export function Scan() {
|
||||
const [devices, setDevices] = useState<MediaDeviceInfo[]>([]);
|
||||
const [deviceId, setDeviceId] = useState(() => readFlag('camera') ?? '');
|
||||
const [aspect, setAspect] = useState('16 / 9');
|
||||
const [res, setRes] = useState('');
|
||||
const [flash, setFlash] = useState(false);
|
||||
const [status, setStatus] = useState<Status | null>(null);
|
||||
const [capturing, setCapturing] = useState(false);
|
||||
@@ -108,7 +110,7 @@ export function Scan() {
|
||||
function stopCamera(user: boolean) {
|
||||
opening.current?.abort(); opening.current = null;
|
||||
scanner.current?.stop(); scanner.current = null;
|
||||
setCam('off'); setStatus(null); setCapturing(false); setAspect('16 / 9');
|
||||
setCam('off'); setStatus(null); setCapturing(false); setAspect('16 / 9'); setRes('');
|
||||
if (user) writeFlag(CAMERA_FLAG, null);
|
||||
}
|
||||
|
||||
@@ -128,7 +130,7 @@ export function Scan() {
|
||||
setCam('on');
|
||||
writeFlag(CAMERA_FLAG, '1');
|
||||
const v = video.current;
|
||||
if (v?.videoWidth) setAspect(`${v.videoWidth} / ${v.videoHeight}`);
|
||||
if (v?.videoWidth) { setAspect(`${v.videoWidth} / ${v.videoHeight}`); setRes(`${v.videoWidth}×${v.videoHeight}`); }
|
||||
setDevices((await navigator.mediaDevices.enumerateDevices()).filter(d => d.kind === 'videoinput'));
|
||||
} catch (e) {
|
||||
if (ac.signal.aborted) return;
|
||||
@@ -144,13 +146,13 @@ export function Scan() {
|
||||
const sc = scanner.current;
|
||||
if (!sc || capturing) return;
|
||||
setCapturing(true);
|
||||
say('Taking a picture and reading it…', 'info', true);
|
||||
say('Trying… 0 frames', 'info', true);
|
||||
try {
|
||||
const r = await sc.capture();
|
||||
const r = await sc.capture(n => say(`Trying… ${n} frames`, 'info', true));
|
||||
if (scanner.current !== sc) return;
|
||||
if (r.isbn) gotIsbn(r.isbn);
|
||||
else if (r.other) { beepLow(); say('That barcode is not an ISBN', 'warn', true); }
|
||||
else { beepLow(); say(NOT_FOUND, 'warn', true); }
|
||||
else { beepLow(); say(r.saved ? NOT_FOUND_SAVED : NOT_FOUND, 'warn', true); }
|
||||
} catch {
|
||||
say(NOT_FOUND, 'warn', true);
|
||||
}
|
||||
@@ -215,17 +217,21 @@ export function Scan() {
|
||||
? { text: 'Starting the camera. Allow access if the browser asks.', tone: 'info', hold: true }
|
||||
: { text: 'The camera is off. Start it to scan, or type the ISBN below.', tone: 'info', hold: true });
|
||||
|
||||
const [aw, ah] = aspect.split('/').map(Number);
|
||||
const t = targetBox((aw || 16) / (ah || 9));
|
||||
const boxStyle = { left: `${t.left}%`, top: `${t.top}%`, width: `${t.width}%`, height: `${t.height}%` };
|
||||
|
||||
return (
|
||||
<div class="scan">
|
||||
<section class="left">
|
||||
<div class={'video-wrap' + (flash ? ' flash' : '')} style={{ aspectRatio: aspect }}>
|
||||
<video ref={video} playsInline muted onLoadedMetadata={e => {
|
||||
const v = e.currentTarget; if (v.videoWidth) setAspect(`${v.videoWidth} / ${v.videoHeight}`);
|
||||
<video ref={video} playsInline muted style={{ transform: `scale(${-ZOOM}, ${ZOOM})` }} onLoadedMetadata={e => {
|
||||
const v = e.currentTarget; if (v.videoWidth) { setAspect(`${v.videoWidth} / ${v.videoHeight}`); setRes(`${v.videoWidth}×${v.videoHeight}`); }
|
||||
}} />
|
||||
{cam === 'on' ? (
|
||||
<>
|
||||
<div class="target" aria-hidden="true" style={{ left: `${TARGET.x * 100}%`, top: `${TARGET.y * 100}%`, width: `${TARGET.w * 100}%`, height: `${TARGET.h * 100}%` }} />
|
||||
<div class="hint" aria-hidden="true">Hold the barcode inside the box</div>
|
||||
<div class="target" aria-hidden="true" style={boxStyle} />
|
||||
<div class="hint" aria-hidden="true">Hold the barcode in the box, about a forearm's length from the camera</div>
|
||||
<div class="scanning" aria-hidden="true"><span class="pulse" />Scanning</div>
|
||||
</>
|
||||
) : (
|
||||
@@ -235,6 +241,7 @@ export function Scan() {
|
||||
)}
|
||||
</div>
|
||||
<p class={'status ' + shown.tone} role="status" aria-live="polite" data-tone={shown.tone}>{shown.text}</p>
|
||||
{cam === 'on' && res ? <p class="muted small res" aria-label="Camera resolution">{res}</p> : null}
|
||||
<div class="row wrap">
|
||||
<button class="primary" disabled={cam !== 'on' || capturing} onClick={() => void capture()}
|
||||
title="Take one picture now and read it as carefully as possible (Space)">{capturing ? 'Reading…' : 'Capture'}</button>
|
||||
|
||||
Reference in new issue
Block a user