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).
This commit is contained in:
blumia committed 2026-11-22 17:52:47 +08:00
1 parent 902f1e18bb
commit 8f6fd99103
10 files changed
+634 -13

No files matched your search

+93
View File
@@ -0,0 +1,93 @@
import 'dart:typed_data';
import 'package:flutter/foundation.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:openchamber/api/qr_scan.dart';
import 'package:qr/qr.dart';
/// Renders [data] as a black-on-white luminance image, like a camera frame's
/// Y plane, with a quiet zone and the requested scale.
Uint8List _qrImage(String data, {int scale = 6, int quiet = 4, int pad = 0}) {
final code = QrCode(
payload: QrPayload.fromString(data),
errorCorrectLevel: QrErrorCorrectLevel.medium,
);
final image = QrImage(code);
final size = (image.moduleCount + quiet * 2) * scale;
final stride = size + pad;
final out = Uint8List(stride * size)..fillRange(0, stride * size, 255);
for (var r = 0; r < image.moduleCount; r++) {
for (var c = 0; c < image.moduleCount; c++) {
if (!image.isDark(r, c)) continue;
final y0 = (r + quiet) * scale;
final x0 = (c + quiet) * scale;
for (var dy = 0; dy < scale; dy++) {
final row = (y0 + dy) * stride;
out.fillRange(row + x0, row + x0 + scale, 0);
}
}
}
return out;
}
void main() {
const link =
'openchamber://connect?v=2&p=eyJ2IjoyLCJwYWlyaW5nSWQiOiJwYWlyX2FiYyJ9';
test('decodes a QR rendered as a camera Y plane', () {
const scale = 6;
const quiet = 4;
final code = QrCode(
payload: QrPayload.fromString(link),
errorCorrectLevel: QrErrorCorrectLevel.medium,
);
final size = (code.moduleCount + quiet * 2) * scale;
final image = _qrImage(link, scale: scale, quiet: quiet);
expect(decodeQrLuminance(image, size, size, size), link);
});
test('a frame can be decoded in a background isolate', () async {
const scale = 6;
const quiet = 4;
final code = QrCode(
payload: QrPayload.fromString(link),
errorCorrectLevel: QrErrorCorrectLevel.medium,
);
final size = (code.moduleCount + quiet * 2) * scale;
final image = _qrImage(link, scale: scale, quiet: quiet);
final text = await compute(
decodeQrFrame,
QrFrame(
luminance: image,
width: size,
height: size,
rowStride: size,
),
);
expect(text, link);
});
test('respects a padded row stride', () {
const scale = 6;
const quiet = 4;
const pad = 12;
final code = QrCode(
payload: QrPayload.fromString(link),
errorCorrectLevel: QrErrorCorrectLevel.medium,
);
final size = (code.moduleCount + quiet * 2) * scale;
final image = _qrImage(link, scale: scale, quiet: quiet, pad: pad);
expect(decodeQrLuminance(image, size, size, size + pad), link);
});
test('a frame without a QR code yields null', () {
final blank = Uint8List(320 * 240)..fillRange(0, 320 * 240, 255);
expect(decodeQrLuminance(blank, 320, 240, 320), isNull);
});
test('short buffers and impossible sizes are refused', () {
expect(decodeQrLuminance(Uint8List(10), 100, 100, 100), isNull);
expect(decodeQrLuminance(Uint8List(100), 0, 10, 10), isNull);
expect(decodeQrLuminance(Uint8List(100), 20, 10, 10), isNull);
});
}