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).
This commit is contained in:
blumia committed 2026-11-23 12:26:16 +08:00
1 parent 8f6fd99103
commit 91bf5e5b81
7 files changed
+194 -39

No files matched your search

+126 -31
View File
@@ -34,6 +34,16 @@ class _ScanQrScreenState extends State<ScanQrScreen> {
bool _found = false;
bool _torch = false;
Timer? _hintTimer;
Timer? _watchdog;
Timer? _slowHint;
/// Frame diagnostics: a scanner that silently does nothing is impossible to
/// tell apart from a camera that sends no frames without these.
int _frames = 0;
int _decodeAttempts = 0;
int _decodeErrors = 0;
bool _loggedFrame = false;
String? _frameShape;
@override
void initState() {
@@ -44,6 +54,8 @@ class _ScanQrScreenState extends State<ScanQrScreen> {
@override
void dispose() {
_hintTimer?.cancel();
_watchdog?.cancel();
_slowHint?.cancel();
_closeCamera();
super.dispose();
}
@@ -57,55 +69,118 @@ class _ScanQrScreenState extends State<ScanQrScreen> {
}
Future<void> _start() async {
_closeCamera();
_watchdog?.cancel();
_hintTimer?.cancel();
_slowHint?.cancel();
_frames = 0;
_decodeAttempts = 0;
_decodeErrors = 0;
_loggedFrame = false;
_frameShape = null;
setState(() {
_error = null;
_hint = null;
_found = false;
});
final List<CameraDescription> cameras;
try {
final cameras = await availableCameras();
if (cameras.isEmpty) {
if (mounted) setState(() => _error = '没有可用的相机');
return;
}
CameraDescription pick = cameras.first;
for (final camera in cameras) {
if (camera.lensDirection == CameraLensDirection.back) {
pick = camera;
break;
}
cameras = await availableCameras();
} on CameraException catch (error) {
if (mounted) setState(() => _error = _cameraError(error));
return;
}
if (cameras.isEmpty) {
if (mounted) setState(() => _error = '没有可用的相机');
return;
}
CameraDescription pick = cameras.first;
for (final camera in cameras) {
if (camera.lensDirection == CameraLensDirection.back) {
pick = camera;
break;
}
}
// A dense pairing QR needs pixels: 720p first, then 480p for cameras or
// devices that refuse the higher analysis size.
Object? lastError;
for (final preset in const [ResolutionPreset.high, ResolutionPreset.medium]) {
final controller = CameraController(
pick,
ResolutionPreset.medium,
preset,
enableAudio: false,
imageFormatGroup: ImageFormatGroup.yuv420,
);
await controller.initialize();
if (!mounted) {
await controller.dispose();
try {
await controller.initialize();
if (!mounted) {
await controller.dispose();
return;
}
setState(() => _controller = controller);
await controller.startImageStream(_onFrame);
_watchdog = Timer(const Duration(seconds: 4), () {
if (!mounted || _frames > 0 || _error != null) return;
setState(() => _hint = '没有收到相机画面,请关闭后重试');
});
_slowHint = Timer(const Duration(seconds: 12), () {
if (!mounted || _found || _frames == 0) return;
_showHint('还没识别到二维码,试着靠近一点或让二维码占满取景框', sticky: true);
});
return;
} catch (error) {
lastError = error;
try {
await controller.stopImageStream();
} catch (_) {}
try {
await controller.dispose();
} catch (_) {}
if (mounted && _controller == controller) {
setState(() => _controller = null);
}
}
setState(() => _controller = controller);
await controller.startImageStream(_onFrame);
} on CameraException catch (error) {
if (!mounted) return;
setState(() {
_error = error.code == 'CameraAccessDenied'
? '需要相机权限才能扫码。请在系统设置里允许 OpenChamber 使用相机。'
: '无法打开相机:${error.description ?? error.code}';
});
} catch (error) {
if (mounted) setState(() => _error = '无法打开相机:$error');
}
if (mounted) setState(() => _error = _cameraError(lastError));
}
String _cameraError(Object? error) {
if (error is CameraException) {
if (error.code == 'CameraAccessDenied' ||
error.code == 'CameraAccessDeniedWithoutPrompt' ||
error.code == 'CameraAccessRestricted') {
return '需要相机权限才能扫码。请在系统设置里允许 OpenChamber 使用相机。';
}
return '无法打开相机:${error.description ?? error.code}';
}
return '无法打开相机:$error';
}
void _onFrame(CameraImage image) {
_frames += 1;
if (!_loggedFrame && image.planes.isNotEmpty) {
_loggedFrame = true;
final plane = image.planes.first;
_frameShape =
'${image.width}×${image.height} ${image.format.group.name}';
debugPrint('qr: frame ${image.width}x${image.height} '
'${image.format.group.name} planes=${image.planes.length} '
'row=${plane.bytesPerRow} pixel=${plane.bytesPerPixel} '
'bytes=${plane.bytes.length}');
}
if (mounted && _frames % 10 == 0) {
// Refresh the small diagnostics line without rebuilding 30x a second.
setState(() {});
}
if (_decoding || _found || !mounted) return;
final frame = _frameOf(image);
if (frame == null) return;
_decoding = true;
// Decoding off the UI thread: a 480p frame is cheap, but not free, and
// camera frames arrive at 30/s.
_decodeAttempts += 1;
// Decoding off the UI thread: frames arrive at 30/s and a decode is not
// free, so only one is in flight at a time.
compute(decodeQrFrame, frame).then((text) {
_decoding = false;
if (!mounted || text == null || _found) return;
@@ -114,8 +189,13 @@ class _ScanQrScreenState extends State<ScanQrScreen> {
} else {
_showHint('这不是 OpenChamber 配对二维码');
}
}).catchError((_) {
}).catchError((Object error) {
_decoding = false;
_decodeErrors += 1;
if (_decodeErrors == 1) {
debugPrint('qr: decode failed: $error');
if (mounted) _showHint('二维码解码出错,请重试');
}
});
}
@@ -142,9 +222,10 @@ class _ScanQrScreenState extends State<ScanQrScreen> {
);
}
void _showHint(String text) {
void _showHint(String text, {bool sticky = false}) {
_hintTimer?.cancel();
if (mounted) setState(() => _hint = text);
if (sticky) return;
_hintTimer = Timer(const Duration(seconds: 2), () {
if (mounted) setState(() => _hint = null);
});
@@ -173,7 +254,8 @@ class _ScanQrScreenState extends State<ScanQrScreen> {
@override
Widget build(BuildContext context) {
final controller = _controller;
final ready = controller != null && controller.value.isInitialized;
final ready =
controller != null && controller.value.isInitialized && _error == null;
return Scaffold(
backgroundColor: Colors.black,
body: Stack(
@@ -242,6 +324,19 @@ class _ScanQrScreenState extends State<ScanQrScreen> {
textAlign: TextAlign.center,
style: TextStyle(color: Colors.white60, fontSize: 12),
),
if (ready && _frames > 0)
Padding(
padding: const EdgeInsets.only(top: 8),
child: Text(
'${_frameShape ?? ''} · 已扫描 $_frames 帧 / 解码 $_decodeAttempts 次',
textAlign: TextAlign.center,
style: const TextStyle(
color: Colors.white38,
fontSize: 11,
fontFeatures: [FontFeature.tabularFigures()],
),
),
),
],
),
),