fix(desktop qr): decode QR images of any PNG bit depth from Select file
What changed, and why it matters
This commit fixes a bug in Stack Wallet's desktop QR code scanner that prevented users from importing QR codes saved as certain types of PNG files (especially small 1-bit black-and-white ones, like those produced by Python's qrcode library). The old code misread the raw image bytes, causing the scanner to silently report 'No QR code found.' The fix converts all incoming images to a standard 8-bit RGBA format before decoding, and adds automated tests for many image formats.
No urgent security action required. This is a reliability/usability fix. Users who previously could not import QR codes from 1-bit or 16-bit PNGs should now be able to. Reviewers may want to verify that the new utility is used consistently across all QR import paths and that the test suite passes.
Security signals we found
Fixes a functional bug that caused valid QR codes to be rejected
Adds input validation: returns null for non-image bytes and images without QR codes
No memory-unsafe operations, network calls, or cryptographic changes observed
Refactoring only: no new dependencies introduced
Evidence from the diff
The patch moves QR decoding from the desktop dialog into a new utility, lib/utilities/qr_image_decoder.dart. The root cause was that img.Image.convert(numChannels: 4) preserves the source pixel format, so 1-bit and 16-bit PNGs remained bit-packed or 2-byte-per-channel. The old code then passed those bytes to zxing2’s RGBLuminanceSource, which expects 32-bit 0xAARRGGBB pixels, producing a RangeError that was swallowed and surfaced as a failed scan. The new code explicitly converts to uint8 RGBA, builds correct ARGB pixels, composites transparency onto white, and falls back through HybridBinarizer and GlobalHistogramBinarizer. Tests cover 1-bit, 8-bit gray, palette, 16-bit, RGB, RGBA, transparent, and JPEG inputs.
Changed components
lib/utilities/qr_image_decoder.dart (new)lib/widgets/desktop/qr_code_scanner_dialog.darttest/utilities/qr_image_decoder_test.dart (new)Inspect captured patch +174 / −34
### lib/utilities/qr_image_decoder.dart
@@ -0,0 +1,73 @@
+import 'dart:typed_data';
+
+import 'package:image/image.dart' as img;
+import 'package:zxing2/qrcode.dart';
+
+/// Decodes a QR code from encoded image bytes (PNG, JPEG, or anything else
+/// the image package can read). Returns null if the bytes can't be decoded as
+/// an image or no QR code is found.
+String? decodeQrFromImageBytes(Uint8List bytes) {
+ final img.Image? image;
+ try {
+ image = img.decodeImage(bytes);
+ } catch (_) {
+ return null;
+ }
+ return image == null ? null : decodeQrFromImage(image);
+}
+
+/// 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) {
+ final LuminanceSource source;
+ try {
+ source = RGBLuminanceSource(
+ image.width,
+ image.height,
+ _argbPixels(image),
+ );
+ } catch (_) {
+ return null;
+ }
+
+ // HybridBinarizer thresholds locally and copes with uneven lighting in a
+ // webcam frame far better. Keep the global one as a fallback.
+ for (final Binarizer binarizer in [
+ HybridBinarizer(source),
+ GlobalHistogramBinarizer(source),
+ ]) {
+ try {
+ final result = QRCodeReader().decode(BinaryBitmap(binarizer));
+ if (result.text.isNotEmpty) {
+ return result.text;
+ }
+ } catch (_) {
+ // No QR code found with this binarizer.
+ }
+ }
+ return null;
+}
+
+/// Packs [image] into the 0xAARRGGBB ints RGBLuminanceSource expects.
+///
+/// convert() keeps the source format unless told otherwise, so a 1-bit PNG
+/// (Python qrcode's default) stays bit-packed and a 16-bit one keeps 2 bytes
+/// per channel. Force 8-bit RGBA, which also resolves palettes. Transparent
+/// pixels are composited onto white so a dark-on-transparent code still has
+/// a light background.
+Int32List _argbPixels(img.Image image) {
+ final rgba = image.convert(format: img.Format.uint8, numChannels: 4);
+ final bytes = rgba.getBytes(order: img.ChannelOrder.rgba);
+ final pixels = Int32List(image.width * image.height);
+ for (var i = 0, j = 0; i < pixels.length; i++, j += 4) {
+ final a = bytes[j + 3];
+ int r = bytes[j], g = bytes[j + 1], b = bytes[j + 2];
+ if (a != 255) {
+ r = (r * a + 255 * (255 - a)) ~/ 255;
+ g = (g * a + 255 * (255 - a)) ~/ 255;
+ b = (b * a + 255 * (255 - a)) ~/ 255;
+ }
+ pixels[i] = 0xFF000000 | (r << 16) | (g << 8) | b;
+ }
+ return pixels;
+}
### lib/widgets/desktop/qr_code_scanner_dialog.dart
@@ -15,13 +15,13 @@ import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:image/image.dart' as img;
-import 'package:zxing2/qrcode.dart';
import '../../notifications/show_flush_bar.dart';
import '../../themes/stack_colors.dart';
import '../../utilities/assets.dart';
import '../../utilities/camera_preview_frame.dart';
import '../../utilities/logger.dart';
+import '../../utilities/qr_image_decoder.dart';
import '../../utilities/text_styles.dart';
import 'desktop_dialog.dart';
import 'desktop_dialog_close_button.dart';
@@ -386,39 +386,8 @@ class _QrCodeScannerDialogState extends State<QrCodeScannerDialog> {
}
}
- Future<String?> _scanImage(img.Image image) async {
- final LuminanceSource source;
- try {
- source = RGBLuminanceSource(
- image.width,
- image.height,
- image
- .convert(numChannels: 4)
- .getBytes(order: img.ChannelOrder.abgr)
- .buffer
- .asInt32List(),
- );
- } catch (_) {
- return null;
- }
-
- // HybridBinarizer thresholds locally and copes with uneven lighting in a
- // webcam frame far better. Keep the global one as a fallback.
- for (final Binarizer binarizer in [
- HybridBinarizer(source),
- GlobalHistogramBinarizer(source),
- ]) {
- try {
- final qrDecode = QRCodeReader().decode(BinaryBitmap(binarizer));
- if (qrDecode.text.isNotEmpty) {
- return qrDecode.text;
- }
- } catch (_) {
- // No QR code found with this binarizer. Spammy to log.
- }
- }
- return null;
- }
+ Future<String?> _scanImage(img.Image image) async =>
+ decodeQrFromImage(image);
Widget _buildCameraView(BuildContext context) {
if (Platform.isWindows && _isCameraOpen) {
### test/utilities/qr_image_decoder_test.dart
@@ -0,0 +1,98 @@
+import 'dart:typed_data';
+
+import 'package:flutter_test/flutter_test.dart';
+import 'package:image/image.dart' as img;
+import 'package:stackwallet/utilities/qr_image_decoder.dart';
+import 'package:zxing2/qrcode.dart';
+
+const _address = "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq";
+
+/// Renders [text] as a black-on-white QR code in 8-bit RGB.
+img.Image _qrImage(String text, {int scale = 8, int border = 4}) {
+ final matrix = Encoder.encode(text, ErrorCorrectionLevel.m).matrix!;
+ final size = (matrix.width + 2 * border) * scale;
+ final image = img.Image(width: size, height: size);
+ img.fill(image, color: img.ColorRgb8(255, 255, 255));
+ for (var y = 0; y < matrix.height; y++) {
+ for (var x = 0; x < matrix.width; x++) {
+ if (matrix.get(x, y) == 1) {
+ img.fillRect(
+ image,
+ x1: (x + border) * scale,
+ y1: (y + border) * scale,
+ x2: (x + border + 1) * scale - 1,
+ y2: (y + border + 1) * scale - 1,
+ color: img.ColorRgb8(0, 0, 0),
+ );
+ }
+ }
+ }
+ return image;
+}
+
+Uint8List _png(img.Image image) => Uint8List.fromList(img.encodePng(image));
+
+void main() {
+ final rgb = _qrImage(_address);
+
+ final variants = <String, img.Image>{
+ "1-bit grayscale": rgb.convert(format: img.Format.uint1, numChannels: 1),
+ "1-bit palette": rgb.convert(
+ format: img.Format.uint1,
+ numChannels: 1,
+ withPalette: true,
+ ),
+ "8-bit grayscale": rgb.convert(numChannels: 1),
+ "8-bit palette": img.quantize(rgb, numberOfColors: 2),
+ "16-bit grayscale": rgb.convert(format: img.Format.uint16, numChannels: 1),
+ "8-bit RGB": rgb,
+ "8-bit RGBA": rgb.convert(numChannels: 4),
+ };
+
+ for (final entry in variants.entries) {
+ test("decodes a ${entry.key} PNG", () {
+ final bytes = _png(entry.value);
+ final decoded = img.decodePng(bytes)!;
+ expect(decoded.width, rgb.width);
+ expect(decodeQrFromImageBytes(bytes), _address);
+ });
+ }
+
+ test("decodes a 1-bit PNG with a 1-bit format after decoding", () {
+ final decoded = img.decodePng(
+ _png(rgb.convert(format: img.Format.uint1, numChannels: 1)),
+ )!;
+ // This is the case the old code got wrong: convert(numChannels: 4) kept
+ // the 1-bit format and handed bit-packed bytes to RGBLuminanceSource.
+ expect(decoded.format, img.Format.uint1);
+ expect(decodeQrFromImage(decoded), _address);
+ });
+
+ test("decodes a JPEG", () {
+ final bytes = Uint8List.fromList(img.encodeJpg(rgb, quality: 85));
+ expect(decodeQrFromImageBytes(bytes), _address);
+ });
+
+ test("decodes a black-on-transparent RGBA PNG", () {
+ final image = img.Image(
+ width: rgb.width,
+ height: rgb.height,
+ numChannels: 4,
+ );
+ for (final p in rgb) {
+ final dark = p.r < 128;
+ image.setPixelRgba(p.x, p.y, 0, 0, 0, dark ? 255 : 0);
+ }
+ expect(decodeQrFromImageBytes(_png(image)), _address);
+ });
+
+ test("returns null for an image without a QR code", () {
+ final blank = img.Image(width: 64, height: 64);
+ img.fill(blank, color: img.ColorRgb8(255, 255, 255));
+ expect(decodeQrFromImageBytes(_png(blank)), isNull);
+ });
+
+ test("returns null for bytes that are not an image", () {
+ expect(decodeQrFromImageBytes(Uint8List.fromList([1, 2, 3, 4])), isNull);
+ });
+}Why this scored 32/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.