juliansteenbakker / mobile_scanner

A universal scanner for Flutter based on MLKit. Uses CameraX on Android and AVFoundation on iOS.
BSD 3-Clause "New" or "Revised" License
756 stars 446 forks source link

onDetect called multiple times with option DetectionSpeed.noDuplicates #510

Open janftmo opened 1 year ago

janftmo commented 1 year ago

Hi! Thanks for this package. But there is one thing which I think is broken.

It's exactly same as this issue https://github.com/juliansteenbakker/mobile_scanner/issues/358 and it happens in version 3.0.0.

In a nutshell, when I set DetectionSpeed.noDuplicates, onDetect fires multiple times (4-6).

utkuvrs commented 1 year ago

I am able to duplicate the issue as well. Using 3.0.0

hsynaksu commented 1 year ago

I am also facing the same problem, when I set DetectionSpeed.noDuplicates what happens is:

juliansteenbakker commented 1 year ago

On which device is this happening?

utkuvrs commented 1 year ago

@juliansteenbakker , I was able to replicate this issue on:

devqmr commented 1 year ago

I face the same issue on the last release mobile_scanner: ^3.0.0 On Android 10, Samsung Note 9

janftmo commented 1 year ago

I happened to me on Android 12, Pixel 3

indra58 commented 1 year ago

I am having same issue with 3.0.0 update

MangoSwirl commented 1 year ago

I'm having the same issue on macOS 13.

dwzrlp commented 1 year ago

I'm having the same issue on Android 13, VIVO Y22s

yoyo-ridebeam commented 1 year ago

I'm having the same issue on Android Pixel 3 XL, with package version 3.0.0

utkuvrs commented 1 year ago

I was able to solve this issue by changing to qr_code_scanner package.

Silverviql commented 1 year ago

version 3.1.0 of mi 9t physical device in discoverySpeed ​​controller: DetectionSpeed.noDuplicates, scan works 3 times

as hsynaksu said, if you shake the phone while scanning, it manages to scan 3-4 times

Silverviql commented 1 year ago

One solution, after getting the first value call controller.dispose();

Zerocchi commented 1 year ago

Same issue. It's only happening on Android device. iOS does not have this problem.

alvarosantisteban commented 1 year ago

As a workaround, you can change the detectionTimeoutMs to a value where it doesn't happen anymore. In my case, I used 500. You might be able to make it smaller, tho.


return MobileScanner(
  controller: MobileScannerController(
    detectionSpeed: DetectionSpeed.normal,
    facing: CameraFacing.back,
    detectionTimeoutMs: 500,
  ),
  startDelay: false,
  onDetect: (capture) { ```
SushilGhorasaini1 commented 1 year ago

Any workaround this problem??

utkuvrs commented 1 year ago

I have found a package that fixed my problem qr_code_scanner as this issue is still open for over a month now.

import 'package:qr_code_scanner/qr_code_scanner.dart';
import 'package:vibration/vibration.dart';
import 'package:flutter/material.dart';
class QRScannerWidget extends StatefulWidget {
  QRScannerWidget({super.key, required this.scannerType, this.receiver});
  ScannerTypes scannerType;
  ReceiverModel? receiver;
  @override
  _QRScannerWidgetState createState() => _QRScannerWidgetState();
}

class _QRScannerWidgetState extends State<QRScannerWidget> {
  final GlobalKey _qrKey = GlobalKey(debugLabel: 'QR');
  late QRViewController _controller;
  String _qrCode = '';
  bool _isScanning = false;

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Expanded(
          flex: 5,
          child: QRView(
            key: _qrKey,
            onQRViewCreated: _onQRViewCreated,
          ),
        ),
        Expanded(
          flex: 1,
          child: Center(
            child: Text(
              'QR Code: $_qrCode',
              style: TextStyle(fontSize: 20, color: AppColors.white),
            ),
          ),
        ),
      ],
    );
  }

  void _onQRViewCreated(QRViewController controller) {
    setState(() {
      _controller = controller;
      _controller.resumeCamera();
    });
    _controller.scannedDataStream.listen((scanData) {
      if (_isScanning) {
        return;
      }
      setState(() {
        _isScanning = true;
        _qrCode = scanData.code!;
        print("QRCODE: $_qrCode");
      });
      _controller.pauseCamera();
      Future.delayed(Duration(seconds: BLogic.cameraPauseSeconds)).then((_) {
        if (!mounted) {
          _controller.resumeCamera();
          return;
        }
        widget.scannerType == ScannerTypes.route
            ? _scanInRoute(_qrCode)
            : _scanInReceiver(_qrCode);
        setState(() {
          print("QRCODE: $_qrCode");
          _isScanning = false;
        });
        _controller.resumeCamera();
      });
    });
  }

  @override
  void dispose() {
    // _controller.pauseCamera();
    _controller.dispose();
    super.dispose();
  }
}
famasf1 commented 1 year ago

Came here to confirm that both my Android API 33 Pixel 6 Pro (emulator) and OPPO Reno 5 Pro (my device) both shared this issue. I'm gonna guess there are larger problem at hands since this seems to happened only on Android in general.

My workaround to this issue for now is to force controller to stop and start again after delaying with Future.delay for a few seconds. It's not perfect (sometimes it suddenly pop twice but very rarely) but it works fine. The true downside is that it will make your camera turned black for a few seconds. Probably bad for user experience standpoint.

  ///Detect algo
  Future<Firestore_Shipment_Data?> onDetect(BarcodeCapture barcode) async {
    capture = barcode;
    setState(
      () {
        controller.stop();
        this.barcode = barcode.barcodes.first;
      },
    );
    Future.delayed(const Duration(milliseconds: 100)).then((_) async {
      const scannerAlert = "sound/Barcode-scanner-beep-sound.mp3";
      AudioPlayer player = AudioPlayer();
      await player.play(AssetSource(scannerAlert));
    }).then((_) {
      Future.delayed(const Duration(seconds: 1, milliseconds: 500))
          .then((_) async {
        controller.start();
      });
    });

EDIT : typo.

rapan931 commented 9 months ago

fixed? #594

angelru commented 7 months ago

The same issue... with Xiaomi

any solution?

rasphlat commented 2 months ago

I have the same issue

Gegerout commented 1 month ago

I have the same issue. Please fix this bug. One year passed...

javaone199 commented 3 weeks ago

Same issue for Android 14 (API 34) real device. onDetect is called 3 times after a barcode is detected.