fix: make the pairing QR scanner actually see dense codes

The scanner read frames at 480p, which is thin for the web UI's dense
pairing QR (1024px, low error correction, a two-module quiet zone), and it
rejected a Y plane whose last row has no padding even though some cameras
return exactly rowStride * (height - 1) + width bytes — on those devices
every frame was dropped, so nothing ever reacted.

Frames are now requested at 720p and fall back to 480p only when the device
refuses the higher analysis size; a stream that fails to start reports an
error instead of leaving a silent preview. Decoding runs with zxing's
tryHarder hint and retries an inverted frame for light-on-dark codes.

The screen also reports what it sees: resolution/format and a live
"frames scanned / decode attempts" line, a one-time log of the first frame's
planes, row stride and byte count, a no-frames hint after four seconds, and a
"move closer" hint when frames decode but no QR is found.

0.4.6 (406).
This commit is contained in:
blumia committed 2026-11-23 12:26:16 +08:00
1 parent 8f6fd99103
commit 91bf5e5b81
7 files changed
+194 -39

No files matched your search

+20 -5
View File
@@ -33,11 +33,22 @@ String? decodeQrFrame(QrFrame frame) => decodeQrLuminance(
String? decodeQrLuminance(
Uint8List luminance, int width, int height, int rowStride) {
if (width <= 0 || height <= 0 || rowStride < width) return null;
if (luminance.length < rowStride * height) return null;
// The camera's Y plane may omit the padding of its last row, so the buffer
// can be one row shorter than rowStride * height.
if (luminance.length < rowStride * (height - 1) + width) return null;
final source = _PlaneLuminanceSource(luminance, width, height, rowStride);
final direct = _read(source);
if (direct != null) return direct;
// A pairing QR drawn light-on-dark (a dark card in the web UI) is still a
// QR: try the inverted frame before giving up on it.
return _read(InvertedLuminanceSource(source));
}
String? _read(LuminanceSource source) {
try {
final source = _PlaneLuminanceSource(luminance, width, height, rowStride);
final bitmap = BinaryBitmap(HybridBinarizer(source));
return QRCodeReader().decode(bitmap).text;
final hints = DecodeHints()..put(DecodeHintType.tryHarder);
return QRCodeReader().decode(bitmap, hints: hints).text;
} on ReaderException {
return null;
} catch (_) {
@@ -77,7 +88,9 @@ class _PlaneLuminanceSource extends LuminanceSource {
Int8List getRow(int y, Int8List? row) {
final out = (row != null && row.length >= width) ? row : Int8List(width);
final base = y * rowStride;
for (var x = 0; x < width; x++) {
final available = data.length - base;
final count = available < width ? available : width;
for (var x = 0; x < count; x++) {
out[x] = data[base + x].toSigned(8);
}
return out;
@@ -89,7 +102,9 @@ class _PlaneLuminanceSource extends LuminanceSource {
for (var y = 0; y < height; y++) {
final base = y * rowStride;
final target = y * width;
for (var x = 0; x < width; x++) {
final available = data.length - base;
final count = available < width ? available : width;
for (var x = 0; x < count; x++) {
out[target + x] = data[base + x].toSigned(8);
}
}