From 0ff7279cf68bdf0968367d07b7965b99f7e7bc89 Mon Sep 17 00:00:00 2001 From: Elijah Date: Sun, 25 Oct 2026 11:02:36 -0700 Subject: [PATCH] Fix touch input highlights and barcode camera focus Remove field focus outlines. Preserve camera constraints across light and focus changes, fall back to one-shot autofocus, and offer supported manual focus, lens selection, and camera restart. Add scanner regression coverage. --- README.md | 2 + src/Scanner.tsx | 159 +++++++++++++++++++++++++++++++++++++----- src/cameraFocus.ts | 85 ++++++++++++++++++++++ src/styles.css | 16 ++++- tests/scanner.test.js | 153 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 394 insertions(+), 21 deletions(-) create mode 100644 src/cameraFocus.ts create mode 100644 tests/scanner.test.js diff --git a/README.md b/README.md index 2026d3c..dfb5af3 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,8 @@ The food sheet's **Quick entry** tab starts with **Scan barcode** and **Scan lab 3. The server's copy of **Open Food Facts** (~3.4 million products worldwide, community-entered). 4. The live Open Food Facts API, for products newer than the copies. Answers, including "not found", are cached on the server. +The scanner requests continuous autofocus when supported, with one-shot autofocus as a fallback. **Focus distance** appears only on cameras that expose manual lens control; adjusting it holds a fixed focus until you choose **Use autofocus** or restart. If the image stays blurry, move the package farther away, try **Restart camera**, or choose a different lens under **Camera** when multiple cameras are available. Browser and camera support determine which focus controls are available; the native **Take a photo instead** option remains available. + A find fills the form with the product's name, serving and every nutrient the source has: the seven tracked ones plus extras such as saturated fat, cholesterol, added sugars, vitamins and minerals, under **More nutrients**. **Save this food to my library** is pre-checked so the next scan is instant. When nothing is found, scan the label instead; the barcode stays attached to the food. **Scan label** opens the iPhone camera app (full resolution, focus and flash, no permission prompt), or a file picker on desktop. The photo is downscaled on the device and read on the server with PaddleOCR PP-OCRv5 (server detection, English recognition) on ONNX Runtime, CPU only, typically in 1–3 seconds. A parser then pulls out the serving size and each nutrient, undoes common OCR confusions (O/0, l/1, a unit read as a digit), checks amounts against their printed %DV, and handles US/Canadian panels, linear labels, tilted photos, Spanish labels and EU "per 100 g" tables (salt → sodium). Values it found replace the form's values; check them against the package before saving. Fill the frame with the panel and keep it flat and sharp: curved containers and glare are the usual causes of misses. diff --git a/src/Scanner.tsx b/src/Scanner.tsx index a1bcb4f..61435a8 100644 --- a/src/Scanner.tsx +++ b/src/Scanner.tsx @@ -1,8 +1,14 @@ import { useEffect, useRef, useState } from "react"; -import { Camera, Flashlight, Search } from "lucide-react"; +import { Camera, Flashlight, RotateCcw, Search } from "lucide-react"; import { normalizeBarcode } from "../shared/barcode.js"; import { Modal } from "./components"; import { barcodeDetector, detectBarcode } from "./scan"; +import { + autofocus, + cameraCapabilities, + cameraConstraint, + manualFocusRange, +} from "./cameraFocus"; type Status = "starting" | "scanning" | "denied" | "unavailable"; @@ -20,6 +26,14 @@ export function BarcodeScanner({ const [torch, setTorch] = useState(null); const [digits, setDigits] = useState(""); const [error, setError] = useState(""); + const [restart, setRestart] = useState(0); + const [focus, setFocus] = useState>(null); + const [adjusting, setAdjusting] = useState(false); + const [manual, setManual] = useState(false); + const [canAutofocus, setCanAutofocus] = useState(false); + const [cameras, setCameras] = useState([]); + const [cameraId, setCameraId] = useState(""); + const [activeCameraId, setActiveCameraId] = useState(""); const finish = (gtin: string) => { if (done.current) return; done.current = true; @@ -33,12 +47,19 @@ export function BarcodeScanner({ let stream: MediaStream | undefined; let stopped = false; let timer = 0; + setStatus("starting"); + setTorch(null); + setFocus(null); + setManual(false); + setCanAutofocus(false); + setAdjusting(false); const reader = barcodeDetector(); (async () => { try { stream = await navigator.mediaDevices.getUserMedia({ audio: false, video: { + ...(cameraId ? { deviceId: { exact: cameraId } } : {}), facingMode: { ideal: "environment" }, width: { ideal: 1920 }, height: { ideal: 1080 }, @@ -57,20 +78,27 @@ export function BarcodeScanner({ const element = video.current!; element.srcObject = stream; await element.play().catch(() => {}); + if (stopped) return; const camera = stream.getVideoTracks()[0]; track.current = camera; - const capabilities = camera.getCapabilities?.() as - | (MediaTrackCapabilities & { torch?: boolean; focusMode?: string[] }) - | undefined; + const capabilities = cameraCapabilities(camera); if (capabilities?.torch) setTorch(false); - if (capabilities?.focusMode?.includes("continuous")) - camera - .applyConstraints({ - advanced: [{ focusMode: "continuous" } as MediaTrackConstraintSet], - }) - .catch(() => {}); + setActiveCameraId(camera.getSettings().deviceId ?? ""); + setCanAutofocus(Boolean(capabilities.focusMode?.some( + mode => mode === "continuous" || mode === "single-shot", + ))); + // Permission makes lens names available. Let the user switch away from + // a fixed-focus rear lens when a phone chooses it by default. + void navigator.mediaDevices.enumerateDevices?.().then(devices => { + if (!stopped) + setCameras(devices.filter(device => device.kind === "videoinput" && device.deviceId)); + }).catch(() => {}); + await autofocus(camera); + if (stopped) return; + setFocus(manualFocusRange(camera)); setStatus("scanning"); const detector = await reader.catch(() => null); + if (stopped) return; if (!detector) return setStatus("unavailable"); const canvas = document.createElement("canvas"); const context = canvas.getContext("2d", { willReadFrequently: true })!; @@ -109,16 +137,40 @@ export function BarcodeScanner({ // A dropped frame; try the next one. } } - timer = window.setTimeout(tick, 100); + if (!stopped && !done.current) timer = window.setTimeout(tick, 100); }; void tick(); })(); return () => { stopped = true; clearTimeout(timer); + track.current = null; stream?.getTracks().forEach((t) => t.stop()); }; - }, []); + }, [restart, cameraId]); + + async function adjustFocus(value?: number) { + const camera = track.current; + if (!camera) return; + setAdjusting(true); + setError(""); + try { + if (value === undefined) { + const applied = await autofocus(camera); + if (track.current === camera) { + if (applied) setManual(false); + else setError("Autofocus isn’t available. Adjust focus or restart the camera."); + } + } else { + await cameraConstraint(camera, { focusMode: "manual", focusDistance: value }); + if (track.current === camera) setManual(true); + } + } catch { + if (track.current === camera) setError("This camera couldn’t adjust focus. Try restarting it or taking a photo."); + } finally { + if (track.current === camera) setAdjusting(false); + } + } async function photo(file: File | undefined) { if (!file) return; @@ -161,20 +213,89 @@ export function BarcodeScanner({ className="scanner-torch" aria-label={torch ? "Turn light off" : "Turn light on"} aria-pressed={torch} - onClick={() => { + disabled={adjusting} + onClick={async () => { + const camera = track.current; + if (!camera) return; const next = !torch; - track.current - ?.applyConstraints({ - advanced: [{ torch: next } as MediaTrackConstraintSet], - }) - .then(() => setTorch(next)) - .catch(() => setTorch(null)); + setAdjusting(true); + try { + await cameraConstraint(camera, { torch: next }); + if (track.current === camera) setTorch(next); + } catch { + if (track.current === camera) setTorch(null); + } finally { + if (track.current === camera) setAdjusting(false); + } }} > )} + {status === "scanning" && ( +
+

+ If it’s blurry, move the barcode farther away and hold still. +

+ {focus && ( + + )} + {canAutofocus && ( + + )} + +
+ )} + {cameras.length > 1 && ( + + )}