Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 | 14x 14x 582221x 582221x 2763293x 2763293x 2726340x 545268x 538458x 538458x 538458x 35002x 62659x 62659x 62659x 62659x 580395x 580395x 580395x 62659x 61844x 493757x 493757x 493757x 61844x 60370x 464148x 464148x 464148x 60370x 60165x 60165x 60165x 434437x 434437x 434437x 60165x 59729x 486474x 486474x 486474x 59729x 57831x 434516x 434516x 434516x 57831x 57522x 57522x 8956x 48566x 36462x 36462x 207x 207x 207x 207x 53592x 53592x 53592x 53592x 18268237x 18268237x 17071769x 17071769x 1196468x 531691x 34960x 34960x 34960x 34960x 34960x 1x 531690x 531690x 531690x 531690x 531690x 531690x 664777x 53591x 42x 42x 206x 35002x 35002x 35002x 35002x 9685x 9685x 9685x 9137x 8835x 8805x 17355x 17355x 8249x 8249x 8249x 8249x 8249x 8249x 556x 556x 556x 207x 207x 426x 556x 927x | import type { BitMatrix } from "../image/bitmatrix";
import type { Point } from "../types";
/**
* Finder-pattern detection: the 1 : 1 : 3 : 1 : 1 dark/light ratio in the three
* corners of every QR symbol (ISO/IEC 18004 §6.3.3).
*
* The scan is a run-length state machine over each row. A row hit alone means
* little — text, table borders and barcodes produce the ratio by accident — so
* every candidate is re-checked vertically and along both diagonals before it
* is believed, and centres are refined to sub-pixel accuracy from the run
* boundaries. That last part is not a nicety: at 3 pixels per module a
* half-pixel error in the centre is a sixth of a module, and by the far corner
* of a version 20 symbol that has grown into a whole module of drift.
*/
export interface FinderCandidate {
/** Sub-pixel centre, in binarized-image pixels. */
readonly x: number;
readonly y: number;
/** Estimated module size (one seventh of the pattern's width). */
readonly moduleSize: number;
/** How many independent scan lines agreed on this centre. */
readonly count: number;
}
/** Every candidate must be re-found within this fraction of its module size. */
const CENTRE_TOLERANCE = 1.0;
/**
* How far two candidates' module sizes may differ and still be merged: one
* pixel, **or** the same fraction of the estimate, whichever is larger.
*
* The relative half matters for close-ups. At 20 px per module, two scan lines
* through the same finder pattern routinely disagree by more than a pixel, and
* an absolute-only budget would split one pattern into several candidates that
* never merge — handing the grouping stage more corners than the frame
* contains.
*/
const MODULE_SIZE_TOLERANCE = 1.0;
/**
* Does a five-run sequence match 1 : 1 : 3 : 1 : 1?
*
* The tolerance is half a module per unit, which is the standard reading of
* "the pattern shall be recognisable"; tighter rejects real photographs, looser
* starts accepting body text.
*/
function matchesRatio(runs: ArrayLike<number>): boolean {
let total = 0;
for (let i = 0; i < 5; i++) {
const run = runs[i]!;
if (run === 0) return false;
total += run;
}
if (total < 7) return false;
const moduleSize = total / 7;
const tolerance = moduleSize / 2;
return (
Math.abs(moduleSize - runs[0]!) < tolerance &&
Math.abs(moduleSize - runs[1]!) < tolerance &&
Math.abs(3 * moduleSize - runs[2]!) < 3 * tolerance &&
Math.abs(moduleSize - runs[3]!) < tolerance &&
Math.abs(moduleSize - runs[4]!) < tolerance
);
}
/** The centre of the middle run, given the run lengths and the scan position. */
function centreOfRuns(runs: ArrayLike<number>, end: number): number {
return end - runs[4]! - runs[3]! - runs[2]! / 2;
}
/**
* Walk one axis (or diagonal) through a candidate centre and measure the five
* runs around it. Returns the refined centre along that axis, or `null` when
* the pattern does not hold up.
*/
function crossCheck(
bits: BitMatrix,
startX: number,
startY: number,
stepX: number,
stepY: number,
maxCount: number,
/**
* The row scan's total run length, for the axis checks. Pass `null` for a
* diagonal, where no fixed expectation exists (see {@link confirm}).
*/
originalTotal: number | null,
): number | null {
const runs = [0, 0, 0, 0, 0];
// Centre run, walking backwards then forwards.
let x = startX;
let y = startY;
while (bits.getSafe(x, y) && runs[2]! < maxCount) {
runs[2]!++;
x -= stepX;
y -= stepY;
}
if (runs[2]! >= maxCount) return null;
while (!bits.getSafe(x, y) && runs[1]! < maxCount) {
runs[1]!++;
x -= stepX;
y -= stepY;
}
if (runs[1]! >= maxCount) return null;
while (bits.getSafe(x, y) && runs[0]! < maxCount) {
runs[0]!++;
x -= stepX;
y -= stepY;
}
if (runs[0]! >= maxCount) return null;
x = startX + stepX;
y = startY + stepY;
while (bits.getSafe(x, y) && runs[2]! < maxCount) {
runs[2]!++;
x += stepX;
y += stepY;
}
if (runs[2]! >= maxCount) return null;
while (!bits.getSafe(x, y) && runs[3]! < maxCount) {
runs[3]!++;
x += stepX;
y += stepY;
}
if (runs[3]! >= maxCount) return null;
while (bits.getSafe(x, y) && runs[4]! < maxCount) {
runs[4]!++;
x += stepX;
y += stepY;
}
if (runs[4]! >= maxCount) return null;
const total = runs[0]! + runs[1]! + runs[2]! + runs[3]! + runs[4]!;
// A cross-check that disagrees with the original scan by more than ~40% is
// measuring something else — a letter, or an adjacent pattern.
if (originalTotal !== null && 5 * Math.abs(total - originalTotal) >= 2 * originalTotal) {
return null;
}
if (!matchesRatio(runs)) return null;
// Distance travelled along the axis, converted back to a coordinate.
const back = runs[4]! + runs[3]! + runs[2]! / 2;
return stepX !== 0 ? x - stepX * back : y - stepY * back;
}
/**
* Find every plausible finder-pattern centre in a binarized image.
*
* Candidates are merged when they agree on both position and module size, and
* the `count` of agreeing scan lines becomes a confidence score the grouping
* stage sorts by.
*
* @param bits - The binarized image.
* @param limit - Stop after this many distinct candidates (0 = unlimited).
*/
export function findFinderPatterns(bits: BitMatrix, limit = 0): FinderCandidate[] {
const candidates: Array<{ x: number; y: number; moduleSize: number; count: number }> = [];
const { width, height } = bits;
// Rows are sampled rather than exhaustively walked: a finder pattern is seven
// modules tall, so a symbol big enough to decode cannot slip between samples
// three rows apart, and the scan cost drops by the same factor.
const rowStep = Math.max(1, Math.floor(height / 256));
for (let y = 0; y < height; y += rowStep) {
const runs = [0, 0, 0, 0, 0];
let state = 0;
let rowOffset = y * width;
for (let x = 0; x < width; x++) {
const dark = bits.data[rowOffset + x] === 1;
if (dark === (state % 2 === 0)) {
// Same colour as the run we are in.
runs[state]!++;
continue;
}
if (state === 4) {
if (matchesRatio(runs)) {
const centreX = centreOfRuns(runs, x);
const total = runs[0]! + runs[1]! + runs[2]! + runs[3]! + runs[4]!;
const moduleSize = total / 7;
const found = confirm(bits, centreX, y, moduleSize, total, candidates);
if (found && limit > 0 && candidates.length >= limit) {
return finalize(candidates);
}
}
// Shift the window: the last two runs become the first two.
runs[0] = runs[2]!;
runs[1] = runs[3]!;
runs[2] = runs[4]!;
runs[3] = 1;
runs[4] = 0;
state = 3;
} else {
runs[++state] = 1;
}
}
// A pattern that ends exactly at the right edge of the image.
if (state === 4 && matchesRatio(runs)) {
const total = runs[0]! + runs[1]! + runs[2]! + runs[3]! + runs[4]!;
confirm(bits, centreOfRuns(runs, width), y, total / 7, total, candidates);
}
}
return finalize(candidates);
}
/**
* Cross-check a row hit vertically and diagonally, then merge it into the
* candidate list. Returns whether a *new* candidate was created.
*/
function confirm(
bits: BitMatrix,
centreX: number,
rowY: number,
moduleSize: number,
rowTotal: number,
candidates: Array<{ x: number; y: number; moduleSize: number; count: number }>,
): boolean {
const maxCount = Math.ceil(moduleSize * 7);
const x = Math.round(centreX);
const verticalCentre = crossCheck(bits, x, rowY, 0, 1, maxCount, rowTotal);
if (verticalCentre === null) return false;
const y = Math.round(verticalCentre);
const horizontalCentre = crossCheck(bits, x, y, 1, 0, maxCount, rowTotal);
if (horizontalCentre === null) return false;
// ── Diagonals are checked by ratio only, never by length ──
// How long a diagonal chord through the pattern is depends entirely on how
// the pattern is rotated: √2 × its side when axis-aligned, and 1 × its side
// when the symbol sits at 45°, with everything in between. There is no
// expected length to compare against, and comparing to the row scan's total
// rejects real patterns — measured in a browser, where an antialiased symbol
// rotated 37° lost one of its three finders and became undecodable while
// jsQR read it fine.
//
// The diagonals still earn their place: a run of text or a 1D barcode
// produces the 1 : 1 : 3 : 1 : 1 ratio on one axis and nothing at all on a
// diagonal, which the ratio test alone rejects.
if (crossCheck(bits, x, y, 1, 1, maxCount, null) === null) return false;
if (crossCheck(bits, x, y, 1, -1, maxCount, null) === null) return false;
for (const candidate of candidates) {
const sizeDelta = Math.abs(candidate.moduleSize - moduleSize);
if (
Math.abs(candidate.x - horizontalCentre) <= CENTRE_TOLERANCE * candidate.moduleSize &&
Math.abs(candidate.y - verticalCentre) <= CENTRE_TOLERANCE * candidate.moduleSize &&
(sizeDelta <= MODULE_SIZE_TOLERANCE ||
sizeDelta / candidate.moduleSize <= MODULE_SIZE_TOLERANCE)
) {
// Running average, weighted by how many lines have agreed so far — a
// centre confirmed ten times should not be dragged by an eleventh.
const total = candidate.count + 1;
candidate.x = (candidate.x * candidate.count + horizontalCentre) / total;
candidate.y = (candidate.y * candidate.count + verticalCentre) / total;
candidate.moduleSize = (candidate.moduleSize * candidate.count + moduleSize) / total;
candidate.count = total;
return false;
}
}
candidates.push({ x: horizontalCentre, y: verticalCentre, moduleSize, count: 1 });
return true;
}
function finalize(
candidates: Array<{ x: number; y: number; moduleSize: number; count: number }>,
): FinderCandidate[] {
// A real finder pattern is confirmed by several scan lines; a coincidence
// usually is not. But a symbol photographed small enough can legitimately
// produce a single confirmation, so the filter only applies when discarding
// the singletons still leaves a full triple to work with.
const confirmed = candidates.filter((candidate) => candidate.count >= 2);
const pool = confirmed.length >= 3 ? confirmed : candidates;
// Ordering by confidence lets the grouping stage try the likeliest triples
// first, which matters when a busy photo yields a dozen candidates.
return pool
.slice()
.sort((a, b) => b.count - a.count)
.map(({ x, y, moduleSize, count }) => ({ x, y, moduleSize, count }));
}
/** Euclidean distance between two points — used all over the detector. */
export function distance(a: Point, b: Point): number {
return Math.hypot(a.x - b.x, a.y - b.y);
}
|