fix(desktop qr): scale large camera frames down before scanning
What changed, and why it matters
This commit is a performance improvement, not a security fix. It makes QR code scanning faster on desktop by shrinking very large camera frames (for example, 3264x2448 images from a Windows webcam) down to 1280 pixels before trying to decode them. Previously, huge frames could take 2-3 seconds per frame to scan. There is no indication this change addresses a security vulnerability.
No security action required. Treat as a normal performance/UX improvement.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change adds an optional maxDimension parameter to decodeQrFromImage(). When set, images whose largest side exceeds maxDimension are resized using average interpolation so the larger side equals maxDimension. The desktop QR scanner now passes _cameraFrameMaxDimension = 1280 when scanning live camera frames, and the Windows preview capture path is adjusted to use the same constant. A test verifies that a 3264x2448 synthetic frame with a QR code is decoded when maxDimension: 1280 is used, and that smaller images are left untouched.
Changed components
lib/utilities/qr_image_decoder.dartlib/widgets/desktop/qr_code_scanner_dialog.darttest/utilities/qr_image_decoder_test.dartInspect captured patch +48 / −7
### lib/utilities/qr_image_decoder.dart
@@ -1,3 +1,4 @@
+import 'dart:math' as math;
import 'dart:typed_data';
import 'package:image/image.dart' as img;
@@ -18,7 +19,26 @@ String? decodeQrFromImageBytes(Uint8List bytes) {
/// Decodes a QR code from [image], whatever its bit depth, channel count or
/// palette. Returns null if no QR code is found.
-String? decodeQrFromImage(img.Image image) {
+///
+/// If [maxDimension] is set, larger images are scaled down to fit it first.
+/// Some webcams hand back full sensor-size photos (3264x2448 from a USB
+/// webcam on Windows), which took 2-3 s per frame to scan on an older laptop.
+String? decodeQrFromImage(img.Image image, {int? maxDimension}) {
+ if (maxDimension != null &&
+ math.max(image.width, image.height) > maxDimension) {
+ image = image.width >= image.height
+ ? img.copyResize(
+ image,
+ width: maxDimension,
+ interpolation: img.Interpolation.average,
+ )
+ : img.copyResize(
+ image,
+ height: maxDimension,
+ interpolation: img.Interpolation.average,
+ );
+ }
+
final LuminanceSource source;
try {
source = RGBLuminanceSource(image.width, image.height, _argbPixels(image));
### lib/widgets/desktop/qr_code_scanner_dialog.dart
@@ -63,9 +63,6 @@ class _QrCodeScannerDialogState extends State<QrCodeScannerDialog> {
bool _loggedBlankWindowsFrame = false;
bool _hasWindowsFrame = false;
- // Bigger captures cost more to read back and scan without helping zxing.
- static const _maxWindowsFrameDimension = 1280;
-
@override
void initState() {
super.initState();
@@ -294,7 +291,7 @@ class _QrCodeScannerDialogState extends State<QrCodeScannerDialog> {
if (preview != null && preview.width > 0 && preview.height > 0) {
final fit = min(
1.0,
- _maxWindowsFrameDimension / max(preview.width, preview.height),
+ _cameraFrameMaxDimension / max(preview.width, preview.height),
);
// toImage() rounds each side up, and the laid-out box is rarely exactly
// the camera's aspect ratio, so take the smaller ratio and stay a hair
@@ -406,7 +403,10 @@ class _QrCodeScannerDialogState extends State<QrCodeScannerDialog> {
});
}
- final String? scanResult = await _scanImage(image);
+ final String? scanResult = await _scanImage(
+ image,
+ maxDimension: _cameraFrameMaxDimension,
+ );
if (scanResult != null && scanResult.isNotEmpty) {
await _stopCamera();
@@ -441,7 +441,13 @@ class _QrCodeScannerDialogState extends State<QrCodeScannerDialog> {
}
}
- Future<String?> _scanImage(img.Image image) async => decodeQrFromImage(image);
+ // Camera frames bigger than this are scaled down before scanning, and
+ // Windows preview captures are taken at no more than this. Files picked
+ // with "Select file" are scanned at full size.
+ static const _cameraFrameMaxDimension = 1280;
+
+ Future<String?> _scanImage(img.Image image, {int? maxDimension}) async =>
+ decodeQrFromImage(image, maxDimension: maxDimension);
Widget _buildCameraView(BuildContext context) {
if (Platform.isWindows && _isCameraOpen) {
### test/utilities/qr_image_decoder_test.dart
@@ -68,6 +68,21 @@ void main() {
expect(decodeQrFromImage(decoded), _address);
});
+ test("decodes a large camera-sized frame scaled down to maxDimension", () {
+ // A QR code held up to a 3264x2448 webcam, filling about a third of the
+ // frame's height.
+ final frame = img.Image(width: 3264, height: 2448);
+ img.fill(frame, color: img.ColorRgb8(200, 200, 200));
+ final code = _qrImage(_address, scale: 18);
+ img.compositeImage(frame, code, dstX: 1200, dstY: 700);
+ final jpeg = img.decodeJpg(img.encodeJpg(frame, quality: 85))!;
+ expect(decodeQrFromImage(jpeg, maxDimension: 1280), _address);
+ });
+
+ test("leaves images within maxDimension alone", () {
+ expect(decodeQrFromImage(rgb, maxDimension: 1280), _address);
+ });
+
test("decodes a JPEG", () {
final bytes = Uint8List.fromList(img.encodeJpg(rgb, quality: 85));
expect(decodeQrFromImageBytes(bytes), _address);Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.