Scanner: gpu canvases (cpu ones copied whole frames in software), back to 1080p with a 720p fallback on slow phones; lab frame save works on android

This commit is contained in:
2026-11-17 14:06:03 +01:00
parent 7765f22f61
commit 70d09b1008
5 changed files with 35 additions and 17 deletions
+14 -5
View File
@@ -77,7 +77,7 @@ async function startScanner() {
// calls onToken(token) for every code of this poll it reads
function makeScanner(video, onToken, onOther) {
let stream = null, running = false;
const canvas = document.createElement("canvas"), ctx = canvas.getContext("2d", { willReadFrequently: true });
const canvas = document.createElement("canvas"), ctx = canvas.getContext("2d");
const rings = makeRingScanner();
async function loop() {
if (!running) return;
@@ -86,9 +86,10 @@ function makeScanner(video, onToken, onOther) {
try {
if (codeStyle === "ring") {
// reads a crop at sensor resolution: a far code keeps all its pixels
const d = rings.scan(video, video.videoWidth, video.videoHeight);
const ts = performance.now(), d = rings.scan(video, video.videoWidth, video.videoHeight);
if (d.token) onToken(d.token);
steer(d);
pace(performance.now() - ts);
} else {
// 960 px is plenty for a qr and keeps low-end phones near 7 fps
const k = Math.min(1, 960 / video.videoWidth);
@@ -117,6 +118,14 @@ function makeScanner(video, onToken, onOther) {
zoom.textContent = `${(Math.round(z * 10) / 10).toLocaleString("fr-FR")}×`;
track.applyConstraints({ advanced: [{ zoom: z }] }).catch(() => { zs = null; zoom.hidden = true; });
}
// a slow phone reads few frames and misses codes: past ~300 ms a frame, drop the stream to 720p once
let slow = 0, frames = 0, dropped = false;
function pace(ms) {
slow = frames++ ? slow * 0.8 + ms * 0.2 : ms;
if (dropped || frames < 6 || slow < 300 || video.videoWidth * video.videoHeight <= 1280 * 720) return;
dropped = true;
stream?.getVideoTracks()[0]?.applyConstraints({ width: { ideal: 1280 }, height: { ideal: 720 } }).catch(() => {});
}
function steer(d) {
// people keep their seat between votes: a zoom that read the ring is where the next scan starts
if (zs && d.token) lsSet(ZOOM_KEY, String(zs.now));
@@ -183,9 +192,9 @@ const ZOOM_KEY = `rv_zoom_${pid}`;
const FRONT = /front|user|avant|facetime|selfie/i, ODD = /ultra|wide|grand.angle|macro|tele|télé|depth|infra|dual|triple|double/i, BACK = /back|rear|arri[eè]re|environment/i;
function openCamera(deviceId) {
// rings from the back of a room are a few dozen pixels wide at 720p: ask for up to 4k, the browser hands over
// the closest it has. only a crop around the ring is read at that size, so the cost per frame stays flat
const [w, h] = codeStyle === "ring" ? [3840, 2160] : [1280, 720];
// rings from the back of a room are a few dozen pixels wide at 720p. 4k was tried: a mid-range android took
// it and fell to one frame every 2 s, so 1080p, and 720p on phones too slow for that (pace)
const [w, h] = codeStyle === "ring" ? [1920, 1080] : [1280, 720];
const base = { width: { ideal: w }, height: { ideal: h } };
return navigator.mediaDevices.getUserMedia({
audio: false, video: deviceId ? { ...base, deviceId: { exact: deviceId } } : { ...base, facingMode: { ideal: "environment" } },