perf(scanner): add adaptive cooldown backoff for repeated OCR misses
This commit is contained in:
parent
7c25ab7c6f
commit
edacf7ad1a
1 changed files with 14 additions and 1 deletions
|
|
@ -37,6 +37,7 @@ class _ScannerScreenState extends State<ScannerScreen>
|
||||||
static const _scanCooldownSuccess = Duration(milliseconds: 1500);
|
static const _scanCooldownSuccess = Duration(milliseconds: 1500);
|
||||||
static const _scanCooldownNoMatch = Duration(milliseconds: 2200);
|
static const _scanCooldownNoMatch = Duration(milliseconds: 2200);
|
||||||
static const _scanCooldownError = Duration(milliseconds: 2600);
|
static const _scanCooldownError = Duration(milliseconds: 2600);
|
||||||
|
static const _scanCooldownNoMatchMax = Duration(milliseconds: 5000);
|
||||||
|
|
||||||
CameraController? _cameraController;
|
CameraController? _cameraController;
|
||||||
late final TextRecognizer _textRecognizer;
|
late final TextRecognizer _textRecognizer;
|
||||||
|
|
@ -52,6 +53,7 @@ class _ScannerScreenState extends State<ScannerScreen>
|
||||||
DateTime _nextScanAllowedAt = DateTime.fromMillisecondsSinceEpoch(0);
|
DateTime _nextScanAllowedAt = DateTime.fromMillisecondsSinceEpoch(0);
|
||||||
bool _isInitializingCamera = false;
|
bool _isInitializingCamera = false;
|
||||||
String? _cameraError;
|
String? _cameraError;
|
||||||
|
int _consecutiveMisses = 0;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
|
|
@ -202,13 +204,23 @@ class _ScannerScreenState extends State<ScannerScreen>
|
||||||
if (!mounted) return;
|
if (!mounted) return;
|
||||||
|
|
||||||
if (found != null) {
|
if (found != null) {
|
||||||
|
_consecutiveMisses = 0;
|
||||||
_nextScanAllowedAt = DateTime.now().add(_scanCooldownSuccess);
|
_nextScanAllowedAt = DateTime.now().add(_scanCooldownSuccess);
|
||||||
setState(() => _lastDetected = found);
|
setState(() => _lastDetected = found);
|
||||||
if (widget.onDetected != null) {
|
if (widget.onDetected != null) {
|
||||||
await _submitDetected(found);
|
await _submitDetected(found);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
_nextScanAllowedAt = DateTime.now().add(_scanCooldownNoMatch);
|
_consecutiveMisses += 1;
|
||||||
|
final missBackoffMs = (_scanCooldownNoMatch.inMilliseconds +
|
||||||
|
(_consecutiveMisses * 300))
|
||||||
|
.clamp(
|
||||||
|
_scanCooldownNoMatch.inMilliseconds,
|
||||||
|
_scanCooldownNoMatchMax.inMilliseconds,
|
||||||
|
);
|
||||||
|
_nextScanAllowedAt = DateTime.now().add(
|
||||||
|
Duration(milliseconds: missBackoffMs),
|
||||||
|
);
|
||||||
setState(() {
|
setState(() {
|
||||||
_scanAccepted = false;
|
_scanAccepted = false;
|
||||||
_scanNotFound = true;
|
_scanNotFound = true;
|
||||||
|
|
@ -216,6 +228,7 @@ class _ScannerScreenState extends State<ScannerScreen>
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
_consecutiveMisses += 1;
|
||||||
_nextScanAllowedAt = DateTime.now().add(_scanCooldownError);
|
_nextScanAllowedAt = DateTime.now().add(_scanCooldownError);
|
||||||
if (!mounted) return;
|
if (!mounted) return;
|
||||||
logError('scanner.capture', e);
|
logError('scanner.capture', e);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue