// 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; } }