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).
126 lines
4.2 KiB
Dart
126 lines
4.2 KiB
Dart
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 last row without its padding still decodes', () {
|
|
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 stride = size + pad;
|
|
final full = _qrImage(link, scale: scale, quiet: quiet, pad: pad);
|
|
// Some cameras return rowStride * (height - 1) + width bytes: the padding
|
|
// after the final row is simply not there.
|
|
final short = Uint8List(stride * (size - 1) + size)
|
|
..setRange(0, stride * (size - 1) + size, full);
|
|
expect(decodeQrLuminance(short, size, size, stride), link);
|
|
});
|
|
|
|
test('a light-on-dark QR decodes through the inverted fallback', () {
|
|
const scale = 6;
|
|
const quiet = 4;
|
|
final code = QrCode(
|
|
payload: QrPayload.fromString(link),
|
|
errorCorrectLevel: QrErrorCorrectLevel.medium,
|
|
);
|
|
final size = (code.moduleCount + quiet * 2) * scale;
|
|
final normal = _qrImage(link, scale: scale, quiet: quiet);
|
|
final inverted = Uint8List.fromList(
|
|
[for (final value in normal) 255 - value]);
|
|
expect(decodeQrLuminance(inverted, size, size, size), 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);
|
|
});
|
|
}
|