Camera view: wait 2.5 s of a still, readable band before the photo
CI / secret scan (push) Successful in 7s
CI / fmt, clippy, tests (push) Successful in 36s
CI / release (push) Skipped

On a phone the camera view took the photo too soon: a second of a
readable band, while the hand still moved. Now it takes the photo by
itself only when the band has looked readable and the picture has stood
still (mean change of a band pixel between checks under 6 of 255) for ten
checks in a row, 2.5 s, and never in the first 1.5 s. The hint counts
down ("the photo is taken in 2 s, or press Take the photo"), and says
"Hold still: the picture still moves" while it moves. Take the photo
still takes it at once.

Checked with fake cameras in Edge at 390x844: glare and dark keep their
hints and never take a photo, a feed that moves is never taken in 8 s, a
still good one is taken after 4.0 s and read with every check digit.
This commit is contained in:
grabowski committed 2026-11-20 19:34:47 +07:00
1 parent 0d3b2d8b84
commit a6ebdfc89c
2 files changed
+50 -12

No files matched your search

+45 -9
View File
@@ -481,6 +481,16 @@
// the variance of the band's Laplacian below which it is not sharp yet,
// measured on the band scaled to 320 pixels across
var SHARP = 150;
// The photo takes itself only once the band has looked readable and the
// picture has stood still for HOLD checks in a row (one every 250 ms, so
// 2.5 s), and never in the first START ms, while the passport is still
// being put into the frame. A second was too quick on a phone: it took
// the photo while the hand still moved.
var HOLD = 10;
var START = 1500;
// the mean change of a band pixel from one check to the next (0 to 255)
// above which the picture still moves
var STILL = 6;
// What is wrong with the band in the picture, or "" when it looks
// readable: too dark, glare, no lines in it, or not sharp.
@@ -512,18 +522,28 @@
}
var sharp = n ? s2 / n - (s1 / n) * (s1 / n) : 0;
var m = { mean: Math.round(mean), glare: bright / g.length, ink: ink / g.length, sharp: Math.round(sharp) };
if (mean < 50) return { why: "Too dark: find more light.", m: m };
if (mean < 50) return { why: "Too dark: find more light.", m: m, g: g };
if (m.glare > 0.02) {
return {
why: "Glare on the two lines: tilt the passport a little, or turn away from the lamp.",
m: m,
g: g,
};
}
if (m.ink < 0.04) {
return { why: "Put the two lines at the bottom of the photo page into the yellow band.", m: m };
return { why: "Put the two lines at the bottom of the photo page into the yellow band.", m: m, g: g };
}
if (sharp < SHARP) return { why: "Hold still: the lines are not sharp yet.", m: m };
return { why: "", m: m };
if (sharp < SHARP) return { why: "Hold still: the lines are not sharp yet.", m: m, g: g };
return { why: "", m: m, g: g };
}
// The mean change of a pixel between two looks at the band; Infinity
// when there is no earlier look of the same size.
function moved(a, b) {
if (!a || !b || a.length !== b.length) return Infinity;
var sum = 0;
for (var i = 0; i < a.length; i++) sum += Math.abs(a[i] - b[i]);
return sum / a.length;
}
function el(tag, cls, text) {
@@ -621,6 +641,8 @@
var timer = 0;
var good = 0;
var last = null;
var opened = Date.now();
var done = false;
var small = canvas(320, 80);
var sctx = small.getContext("2d", { willReadFrequently: true });
@@ -658,12 +680,26 @@
small.height = Math.max(8, Math.round((bh * 320) / bw));
sctx.drawImage(video, bx, by, bw, bh, 0, 0, 320, small.height);
var j = judge(sctx, 320, small.height);
hint.textContent = j.why || "Good. Hold still...";
root.classList.toggle("good", !j.why);
var change = moved(last, j.g);
last = j.g;
j.m.moved = Math.round(change * 10) / 10;
// the first look has nothing to compare with: not still yet, but
// nothing to say about it either
var why = j.why || (change > STILL && change !== Infinity ? "Hold still: the picture still moves." : "");
var early = Date.now() - opened < START;
good = why || early || change > STILL ? 0 : good + 1;
if (why) {
hint.textContent = why;
} else if (early) {
hint.textContent = "Good. Hold still...";
} else {
var left = Math.max(1, Math.ceil(((HOLD - good) * 250) / 1000));
hint.textContent = "Good. Hold still: the photo is taken in " + left + " s, or press Take the photo.";
}
root.classList.toggle("good", !why);
root.setAttribute("data-measure", JSON.stringify(j.m));
good = j.why ? 0 : good + 1;
// a steady, readable band for about a second takes the photo
if (good >= 4) {
// a readable band that stands still for HOLD checks takes the photo
if (good >= HOLD) {
take();
return;
}