Files
openchamber-android-native/lib/api/qr_scan.dart
T
blumia 8f6fd99103 feat: scan the pairing QR code with the camera
The pairing dialog gained a "扫码" button: a full-screen scanner points the
back camera at the QR that OpenChamber's connect-a-device dialog shows, and a
decoded pairing link connects straight away.

Frames are decoded with zxing2 (pure Dart, no ML Kit or Play services): the
YUV420 Y plane is already luminance, BGRA frames are converted green-weighted,
and each frame is decoded in a background isolate so camera frames never block
the UI. Only an openchamber://connect payload ends the scan; any other QR code
shows a short hint and scanning continues. A torch button, a denied-permission
message with retry, and camera-less devices are handled.

The recorder audio permission the camera plugin declares for video is removed
from the merged manifest: the scanner reads frames only.

0.4.5 (405).
2026-11-22 17:52:47 +08:00

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