mobile_scanner icon indicating copy to clipboard operation
mobile_scanner copied to clipboard

onDetect called multiple times with option DetectionSpeed.noDuplicates

Open janftmo opened this issue 1 year ago • 23 comments

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).

janftmo avatar Feb 23 '23 11:02 janftmo

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

utkuvrs avatar Feb 23 '23 12:02 utkuvrs

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

  • I scan a qr code, if I don't move the camera qr is not scanned again
  • If I move the camera around or shake it a little, the same qr is read again even though DetectionSpeed.noDuplicates is set

hsynaksu avatar Feb 23 '23 13:02 hsynaksu

On which device is this happening?

juliansteenbakker avatar Feb 23 '23 22:02 juliansteenbakker

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

  • Pixel 4 API 30 - Android Emulator
  • Samsung Galaxy A02 Android version 11 - Physical device

utkuvrs avatar Feb 24 '23 06:02 utkuvrs

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

devqmr avatar Feb 26 '23 07:02 devqmr

I happened to me on Android 12, Pixel 3

janftmo avatar Feb 27 '23 11:02 janftmo

I am having same issue with 3.0.0 update

indra58 avatar Feb 28 '23 10:02 indra58

I'm having the same issue on macOS 13.

MangoSwirl avatar Mar 02 '23 23:03 MangoSwirl

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

dwzrlp avatar Mar 08 '23 10:03 dwzrlp

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

yoyo-ridebeam avatar Mar 16 '23 11:03 yoyo-ridebeam

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

utkuvrs avatar Mar 16 '23 12:03 utkuvrs

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 avatar Mar 20 '23 13:03 Silverviql

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

Silverviql avatar Mar 21 '23 11:03 Silverviql

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

Zerocchi avatar Mar 22 '23 07:03 Zerocchi

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) { ```

alvarosantisteban avatar Mar 29 '23 17:03 alvarosantisteban

Any workaround this problem??

SushilGhorasaini1 avatar Apr 04 '23 07:04 SushilGhorasaini1

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();
  }
}

utkuvrs avatar Apr 04 '23 12:04 utkuvrs

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.

famasf1 avatar Apr 09 '23 18:04 famasf1

fixed? #594

rapan931 avatar Aug 17 '23 04:08 rapan931

The same issue... with Xiaomi

any solution?

angelru avatar Oct 13 '23 14:10 angelru

I have the same issue

rasphlat avatar Mar 06 '24 11:03 rasphlat

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

Gegerout avatar Apr 10 '24 16:04 Gegerout