fix code detection
This commit is contained in:
1 parent
466f2f759e
commit
579ddadeff
4 files changed
+117
-17
No files matched your search
@@ -27,6 +27,16 @@ import '../widgets/scanner_controls_bar.dart';
|
|||||||
import '../widgets/scanner_overlay.dart';
|
import '../widgets/scanner_overlay.dart';
|
||||||
import 'history_screen.dart';
|
import 'history_screen.dart';
|
||||||
|
|
||||||
|
class _TrackedBarcodeEntry {
|
||||||
|
DetectedBarcodeItem item;
|
||||||
|
DateTime lastSeen;
|
||||||
|
|
||||||
|
_TrackedBarcodeEntry({
|
||||||
|
required this.item,
|
||||||
|
required this.lastSeen,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
class ScannerScreen extends StatefulWidget {
|
class ScannerScreen extends StatefulWidget {
|
||||||
const ScannerScreen({super.key});
|
const ScannerScreen({super.key});
|
||||||
|
|
||||||
@@ -45,6 +55,9 @@ class _ScannerScreenState extends State<ScannerScreen>
|
|||||||
bool _autoZoomedForPrecision = false;
|
bool _autoZoomedForPrecision = false;
|
||||||
|
|
||||||
// Live AR & Precision targeting state
|
// Live AR & Precision targeting state
|
||||||
|
final Map<String, _TrackedBarcodeEntry> _trackedArItems = {};
|
||||||
|
final Set<String> _sentLiveKeys = <String>{};
|
||||||
|
Timer? _arPruneTimer;
|
||||||
List<DetectedBarcodeItem> _liveArItems = const [];
|
List<DetectedBarcodeItem> _liveArItems = const [];
|
||||||
int? _precisionTargetIndex;
|
int? _precisionTargetIndex;
|
||||||
Timer? _arDecayTimer;
|
Timer? _arDecayTimer;
|
||||||
@@ -66,14 +79,16 @@ class _ScannerScreenState extends State<ScannerScreen>
|
|||||||
super.initState();
|
super.initState();
|
||||||
WidgetsBinding.instance.addObserver(this);
|
WidgetsBinding.instance.addObserver(this);
|
||||||
_scannerController = MobileScannerController(
|
_scannerController = MobileScannerController(
|
||||||
detectionSpeed: DetectionSpeed.normal,
|
detectionSpeed: DetectionSpeed.unrestricted,
|
||||||
detectionTimeoutMs: 180,
|
|
||||||
cameraResolution: const Size(1920, 1080),
|
|
||||||
facing: CameraFacing.back,
|
facing: CameraFacing.back,
|
||||||
torchEnabled: false,
|
torchEnabled: false,
|
||||||
returnImage: true,
|
returnImage: true,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
_arPruneTimer = Timer.periodic(const Duration(milliseconds: 200), (_) {
|
||||||
|
_pruneExpiredArItems();
|
||||||
|
});
|
||||||
|
|
||||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
_checkStartupConnection();
|
_checkStartupConnection();
|
||||||
_applyInitialModeZoom();
|
_applyInitialModeZoom();
|
||||||
@@ -117,6 +132,7 @@ class _ScannerScreenState extends State<ScannerScreen>
|
|||||||
void dispose() {
|
void dispose() {
|
||||||
WidgetsBinding.instance.removeObserver(this);
|
WidgetsBinding.instance.removeObserver(this);
|
||||||
_arDecayTimer?.cancel();
|
_arDecayTimer?.cancel();
|
||||||
|
_arPruneTimer?.cancel();
|
||||||
_focusRingTimer?.cancel();
|
_focusRingTimer?.cancel();
|
||||||
_scannerController.dispose();
|
_scannerController.dispose();
|
||||||
super.dispose();
|
super.dispose();
|
||||||
@@ -192,6 +208,8 @@ class _ScannerScreenState extends State<ScannerScreen>
|
|||||||
if (!mounted) return;
|
if (!mounted) return;
|
||||||
|
|
||||||
_arDecayTimer?.cancel();
|
_arDecayTimer?.cancel();
|
||||||
|
_trackedArItems.clear();
|
||||||
|
_sentLiveKeys.clear();
|
||||||
setState(() {
|
setState(() {
|
||||||
_liveArItems = const [];
|
_liveArItems = const [];
|
||||||
_precisionTargetIndex = null;
|
_precisionTargetIndex = null;
|
||||||
@@ -266,9 +284,45 @@ class _ScannerScreenState extends State<ScannerScreen>
|
|||||||
widgetSize: _viewportSize,
|
widgetSize: _viewportSize,
|
||||||
deviceOrientation: deviceOrientation,
|
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;
|
final imageBytes = capture.image;
|
||||||
if (imageBytes != null && imageBytes.isNotEmpty) {
|
if (imageBytes != null && imageBytes.isNotEmpty) {
|
||||||
@@ -277,12 +331,12 @@ class _ScannerScreenState extends State<ScannerScreen>
|
|||||||
imageSize: capture.size,
|
imageSize: capture.size,
|
||||||
viewportSize: _viewportSize,
|
viewportSize: _viewportSize,
|
||||||
deviceOrientation: deviceOrientation,
|
deviceOrientation: deviceOrientation,
|
||||||
items: items,
|
items: reindexed.isNotEmpty ? reindexed : items,
|
||||||
timestamp: DateTime.now(),
|
timestamp: now,
|
||||||
);
|
);
|
||||||
_latestArSnapshot = snapshot;
|
_latestArSnapshot = snapshot;
|
||||||
|
|
||||||
if (_pendingFreeze) {
|
if (_pendingFreeze && reindexed.isNotEmpty) {
|
||||||
_pendingFreeze = false;
|
_pendingFreeze = false;
|
||||||
_freezeWithSnapshot(snapshot);
|
_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(
|
void _updateLiveArItems(
|
||||||
List<DetectedBarcodeItem> items, {
|
List<DetectedBarcodeItem> items, {
|
||||||
int? precisionTargetIndex,
|
int? precisionTargetIndex,
|
||||||
@@ -311,12 +392,21 @@ class _ScannerScreenState extends State<ScannerScreen>
|
|||||||
|
|
||||||
void _requestFreeze() {
|
void _requestFreeze() {
|
||||||
final snapshot = _latestArSnapshot;
|
final snapshot = _latestArSnapshot;
|
||||||
|
final now = DateTime.now();
|
||||||
final isFresh = snapshot != null &&
|
final isFresh = snapshot != null &&
|
||||||
DateTime.now().difference(snapshot.timestamp) <=
|
now.difference(snapshot.timestamp) <= const Duration(milliseconds: 3000);
|
||||||
const Duration(milliseconds: 1500);
|
|
||||||
|
|
||||||
if (isFresh) {
|
if (isFresh && (_liveArItems.isNotEmpty || snapshot.items.isNotEmpty)) {
|
||||||
_freezeWithSnapshot(snapshot);
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -358,11 +448,17 @@ class _ScannerScreenState extends State<ScannerScreen>
|
|||||||
);
|
);
|
||||||
if (!mounted) return;
|
if (!mounted) return;
|
||||||
|
|
||||||
if (_frozenSnapshot != null && success) {
|
if (success) {
|
||||||
setState(() {
|
setState(() {
|
||||||
_sentFrozenKeys.add(item.selectionKey);
|
if (_frozenSnapshot != null) {
|
||||||
|
_sentFrozenKeys.add(item.selectionKey);
|
||||||
|
} else {
|
||||||
|
_sentLiveKeys.add(item.selectionKey);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
} else if (_frozenSnapshot == null) {
|
}
|
||||||
|
|
||||||
|
if (_frozenSnapshot == null) {
|
||||||
ScaffoldMessenger.of(context)
|
ScaffoldMessenger.of(context)
|
||||||
..hideCurrentSnackBar()
|
..hideCurrentSnackBar()
|
||||||
..showSnackBar(
|
..showSnackBar(
|
||||||
@@ -500,6 +596,8 @@ class _ScannerScreenState extends State<ScannerScreen>
|
|||||||
items: _liveArItems,
|
items: _liveArItems,
|
||||||
safeBounds: arSafeBounds,
|
safeBounds: arSafeBounds,
|
||||||
showBadges: aimMode == ScanAimMode.arMulti,
|
showBadges: aimMode == ScanAimMode.arMulti,
|
||||||
|
allowBoxTap: true,
|
||||||
|
sentKeys: _sentLiveKeys,
|
||||||
highlightedIndex: aimMode == ScanAimMode.precision
|
highlightedIndex: aimMode == ScanAimMode.precision
|
||||||
? _precisionTargetIndex
|
? _precisionTargetIndex
|
||||||
: null,
|
: null,
|
||||||
|
|||||||
@@ -142,6 +142,7 @@ class _ZoomPresetsBar extends StatelessWidget {
|
|||||||
|
|
||||||
static const _presets = [
|
static const _presets = [
|
||||||
(label: '1x', scale: 0.0),
|
(label: '1x', scale: 0.0),
|
||||||
|
(label: '1.5x', scale: 0.125),
|
||||||
(label: '2x', scale: 0.25),
|
(label: '2x', scale: 0.25),
|
||||||
(label: '3x', scale: 0.50),
|
(label: '3x', scale: 0.50),
|
||||||
];
|
];
|
||||||
@@ -174,7 +175,7 @@ class _ZoomPresetsBar extends StatelessWidget {
|
|||||||
for (final preset in _presets)
|
for (final preset in _presets)
|
||||||
_ZoomChip(
|
_ZoomChip(
|
||||||
label: preset.label,
|
label: preset.label,
|
||||||
selected: !isExtraZoom && (zoomScale - preset.scale).abs() < 0.09,
|
selected: !isExtraZoom && (zoomScale - preset.scale).abs() < 0.06,
|
||||||
onTap: () => onSelect(preset.scale),
|
onTap: () => onSelect(preset.scale),
|
||||||
),
|
),
|
||||||
if (isExtraZoom)
|
if (isExtraZoom)
|
||||||
|
|||||||
+1
-1
@@ -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
|
# 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
|
# 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.
|
# 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:
|
environment:
|
||||||
sdk: ^3.13.4
|
sdk: ^3.13.4
|
||||||
|
|||||||
@@ -307,6 +307,7 @@ void main() {
|
|||||||
|
|
||||||
expect(find.text('Blocca frame (2 codici)'), findsOneWidget);
|
expect(find.text('Blocca frame (2 codici)'), findsOneWidget);
|
||||||
expect(find.text('1x'), findsOneWidget);
|
expect(find.text('1x'), findsOneWidget);
|
||||||
|
expect(find.text('1.5x'), findsOneWidget);
|
||||||
expect(find.text('2x'), findsOneWidget);
|
expect(find.text('2x'), findsOneWidget);
|
||||||
expect(find.text('3x'), findsOneWidget);
|
expect(find.text('3x'), findsOneWidget);
|
||||||
expect(find.text('Standard'), findsOneWidget);
|
expect(find.text('Standard'), findsOneWidget);
|
||||||
|
|||||||
Reference in new issue
Block a user