Files
openchamber-android-native/lib/api/qr_scan.dart
T
blumia 91bf5e5b81 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).
2026-11-23 12:26:16 +08:00

114 lines
3.5 KiB
Dart

// QR decoding for the pairing scanner. The camera hands us a Y plane (YUV420
// on Android) that is already luminance; zxing2's QR reader does the rest. Kept
// free of Flutter so a frame can be decoded in a background isolate and a unit
// test can feed it a generated QR image.
import 'dart:typed_data';
import 'package:zxing2/qrcode.dart';
import 'package:zxing2/zxing2.dart';
/// One grayscale frame: [rowStride] may exceed [width] (camera padding).
class QrFrame {
final Uint8List luminance;
final int width;
final int height;
final int rowStride;
const QrFrame({
required this.luminance,
required this.width,
required this.height,
required this.rowStride,
});
}
/// The payload of the QR code in [frame], or null when there is none.
String? decodeQrFrame(QrFrame frame) => decodeQrLuminance(
frame.luminance,
frame.width,
frame.height,
frame.rowStride,
);
String? decodeQrLuminance(
Uint8List luminance, int width, int height, int rowStride) {
if (width <= 0 || height <= 0 || rowStride < width) 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 bitmap = BinaryBitmap(HybridBinarizer(source));
final hints = DecodeHints()..put(DecodeHintType.tryHarder);
return QRCodeReader().decode(bitmap, hints: hints).text;
} on ReaderException {
return null;
} catch (_) {
// A torn or malformed frame must not stop the scanner.
return null;
}
}
/// BGRA8888 (iOS and some cameras): green-weighted luminance, as zxing2's
/// RGBLuminanceSource computes it.
Uint8List bgraToLuminance(
Uint8List bytes, int width, int height, int rowStride) {
final out = Uint8List(width * height);
for (var y = 0; y < height; y++) {
final row = y * rowStride;
final target = y * width;
for (var x = 0; x < width; x++) {
final i = row + x * 4;
if (i + 2 >= bytes.length) break;
final b = bytes[i];
final g = bytes[i + 1];
final r = bytes[i + 2];
out[target + x] = (r + 2 * g + b) >> 2;
}
}
return out;
}
class _PlaneLuminanceSource extends LuminanceSource {
final Uint8List data;
final int rowStride;
_PlaneLuminanceSource(this.data, int width, int height, this.rowStride)
: super(width, height);
@override
Int8List getRow(int y, Int8List? row) {
final out = (row != null && row.length >= width) ? row : Int8List(width);
final base = y * rowStride;
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;
}
@override
Int8List getMatrix() {
final out = Int8List(width * height);
for (var y = 0; y < height; y++) {
final base = y * rowStride;
final target = y * width;
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);
}
}
return out;
}
}