fix code detection

This commit is contained in:
brontomark committed 2026-11-15 09:27:26 +01:00
1 parent 466f2f759e
commit 579ddadeff
4 files changed
+116 -16

No files matched your search

@@ -27,6 +27,16 @@ import '../widgets/scanner_controls_bar.dart';
import '../widgets/scanner_overlay.dart';
import 'history_screen.dart';
class _TrackedBarcodeEntry {
DetectedBarcodeItem item;
DateTime lastSeen;
_TrackedBarcodeEntry({
required this.item,
required this.lastSeen,
});
}
class ScannerScreen extends StatefulWidget {
const ScannerScreen({super.key});
@@ -45,6 +55,9 @@ class _ScannerScreenState extends State<ScannerScreen>
bool _autoZoomedForPrecision = false;
// Live AR & Precision targeting state
final Map<String, _TrackedBarcodeEntry> _trackedArItems = {};
final Set<String> _sentLiveKeys = <String>{};
Timer? _arPruneTimer;
List<DetectedBarcodeItem> _liveArItems = const [];
int? _precisionTargetIndex;
Timer? _arDecayTimer;
@@ -66,14 +79,16 @@ class _ScannerScreenState extends State<ScannerScreen>
super.initState();
WidgetsBinding.instance.addObserver(this);
_scannerController = MobileScannerController(
detectionSpeed: DetectionSpeed.normal,
detectionTimeoutMs: 180,
cameraResolution: const Size(1920, 1080),
detectionSpeed: DetectionSpeed.unrestricted,
facing: CameraFacing.back,
torchEnabled: false,
returnImage: true,
);
_arPruneTimer = Timer.periodic(const Duration(milliseconds: 200), (_) {
_pruneExpiredArItems();
});
WidgetsBinding.instance.addPostFrameCallback((_) {
_checkStartupConnection();
_applyInitialModeZoom();
@@ -117,6 +132,7 @@ class _ScannerScreenState extends State<ScannerScreen>
void dispose() {
WidgetsBinding.instance.removeObserver(this);
_arDecayTimer?.cancel();
_arPruneTimer?.cancel();
_focusRingTimer?.cancel();
_scannerController.dispose();
super.dispose();
@@ -192,6 +208,8 @@ class _ScannerScreenState extends State<ScannerScreen>
if (!mounted) return;
_arDecayTimer?.cancel();
_trackedArItems.clear();
_sentLiveKeys.clear();
setState(() {
_liveArItems = const [];
_precisionTargetIndex = null;
@@ -266,9 +284,45 @@ class _ScannerScreenState extends State<ScannerScreen>
widgetSize: _viewportSize,
deviceOrientation: deviceOrientation,
);
if (items.isEmpty) return;
_updateLiveArItems(items);
final now = DateTime.now();
// 1. Update/Add detected items into sticky cache
for (final item in items) {
final key = item.selectionKey;
if (_trackedArItems.containsKey(key)) {
final entry = _trackedArItems[key]!;
entry.item = item; // Update coordinates for live tracking
entry.lastSeen = now;
} else {
_trackedArItems[key] = _TrackedBarcodeEntry(
item: item,
lastSeen: now,
);
}
}
// 2. Prune items that haven't been seen for > 2200ms
_trackedArItems.removeWhere((key, entry) =>
now.difference(entry.lastSeen) > const Duration(milliseconds: 2200));
final activeItems = _trackedArItems.values.map((e) => e.item).toList();
// Sort predictably and assign 1-based indices
final reindexed = <DetectedBarcodeItem>[];
for (var i = 0; i < activeItems.length; i++) {
final it = activeItems[i];
reindexed.add(it.copyWith(
index: i + 1,
color: kArBarcodePalette[i % kArBarcodePalette.length],
));
}
if (mounted && _frozenSnapshot == null) {
setState(() {
_liveArItems = reindexed;
});
}
final imageBytes = capture.image;
if (imageBytes != null && imageBytes.isNotEmpty) {
@@ -277,12 +331,12 @@ class _ScannerScreenState extends State<ScannerScreen>
imageSize: capture.size,
viewportSize: _viewportSize,
deviceOrientation: deviceOrientation,
items: items,
timestamp: DateTime.now(),
items: reindexed.isNotEmpty ? reindexed : items,
timestamp: now,
);
_latestArSnapshot = snapshot;
if (_pendingFreeze) {
if (_pendingFreeze && reindexed.isNotEmpty) {
_pendingFreeze = false;
_freezeWithSnapshot(snapshot);
}
@@ -290,6 +344,33 @@ class _ScannerScreenState extends State<ScannerScreen>
}
}
void _pruneExpiredArItems() {
if (!mounted || _frozenSnapshot != null) return;
final aimMode = context.read<SettingsCubit>().state.settings.aimMode;
if (aimMode != ScanAimMode.arMulti) return;
if (_trackedArItems.isEmpty && _liveArItems.isEmpty) return;
final now = DateTime.now();
final beforeCount = _trackedArItems.length;
_trackedArItems.removeWhere((key, entry) =>
now.difference(entry.lastSeen) > const Duration(milliseconds: 2200));
if (_trackedArItems.length != beforeCount) {
final activeItems = _trackedArItems.values.map((e) => e.item).toList();
final reindexed = <DetectedBarcodeItem>[];
for (var i = 0; i < activeItems.length; i++) {
final it = activeItems[i];
reindexed.add(it.copyWith(
index: i + 1,
color: kArBarcodePalette[i % kArBarcodePalette.length],
));
}
setState(() {
_liveArItems = reindexed;
});
}
}
void _updateLiveArItems(
List<DetectedBarcodeItem> items, {
int? precisionTargetIndex,
@@ -311,12 +392,21 @@ class _ScannerScreenState extends State<ScannerScreen>
void _requestFreeze() {
final snapshot = _latestArSnapshot;
final now = DateTime.now();
final isFresh = snapshot != null &&
DateTime.now().difference(snapshot.timestamp) <=
const Duration(milliseconds: 1500);
now.difference(snapshot.timestamp) <= const Duration(milliseconds: 3000);
if (isFresh) {
_freezeWithSnapshot(snapshot);
if (isFresh && (_liveArItems.isNotEmpty || snapshot.items.isNotEmpty)) {
final finalItems = _liveArItems.isNotEmpty ? _liveArItems : snapshot.items;
final readySnapshot = ArCaptureSnapshot(
imageBytes: snapshot.imageBytes,
imageSize: snapshot.imageSize,
viewportSize: snapshot.viewportSize,
deviceOrientation: snapshot.deviceOrientation,
items: finalItems,
timestamp: now,
);
_freezeWithSnapshot(readySnapshot);
return;
}
@@ -358,11 +448,17 @@ class _ScannerScreenState extends State<ScannerScreen>
);
if (!mounted) return;
if (_frozenSnapshot != null && success) {
if (success) {
setState(() {
if (_frozenSnapshot != null) {
_sentFrozenKeys.add(item.selectionKey);
} else {
_sentLiveKeys.add(item.selectionKey);
}
});
} else if (_frozenSnapshot == null) {
}
if (_frozenSnapshot == null) {
ScaffoldMessenger.of(context)
..hideCurrentSnackBar()
..showSnackBar(
@@ -500,6 +596,8 @@ class _ScannerScreenState extends State<ScannerScreen>
items: _liveArItems,
safeBounds: arSafeBounds,
showBadges: aimMode == ScanAimMode.arMulti,
allowBoxTap: true,
sentKeys: _sentLiveKeys,
highlightedIndex: aimMode == ScanAimMode.precision
? _precisionTargetIndex
: null,
@@ -142,6 +142,7 @@ class _ZoomPresetsBar extends StatelessWidget {
static const _presets = [
(label: '1x', scale: 0.0),
(label: '1.5x', scale: 0.125),
(label: '2x', scale: 0.25),
(label: '3x', scale: 0.50),
];
@@ -174,7 +175,7 @@ class _ZoomPresetsBar extends StatelessWidget {
for (final preset in _presets)
_ZoomChip(
label: preset.label,
selected: !isExtraZoom && (zoomScale - preset.scale).abs() < 0.09,
selected: !isExtraZoom && (zoomScale - preset.scale).abs() < 0.06,
onTap: () => onSelect(preset.scale),
),
if (isExtraZoom)
+1 -1
View File
@@ -16,7 +16,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
# In Windows, build-name is used as the major, minor, and patch parts
# of the product and file versions while build-number is used as the build suffix.
version: 1.0.1+2
version: 1.0.2+3
environment:
sdk: ^3.13.4
+1
View File
@@ -307,6 +307,7 @@ void main() {
expect(find.text('Blocca frame (2 codici)'), findsOneWidget);
expect(find.text('1x'), findsOneWidget);
expect(find.text('1.5x'), findsOneWidget);
expect(find.text('2x'), findsOneWidget);
expect(find.text('3x'), findsOneWidget);
expect(find.text('Standard'), findsOneWidget);