98 lines
2.8 KiB
Dart
98 lines
2.8 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;
|
||
|
|
if (luminance.length < rowStride * height) return null;
|
||
|
|
try {
|
||
|
|
final source = _PlaneLuminanceSource(luminance, width, height, rowStride);
|
||
|
|
final bitmap = BinaryBitmap(HybridBinarizer(source));
|
||
|
|
return QRCodeReader().decode(bitmap).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;
|
||
|
|
for (var x = 0; x < width; 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;
|
||
|
|
for (var x = 0; x < width; x++) {
|
||
|
|
out[target + x] = data[base + x].toSigned(8);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return out;
|
||
|
|
}
|
||
|
|
}
|