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
777 stars 454 forks source link

Camera is not working #1078

Open kcs-ravindrapatil opened 1 month ago

kcs-ravindrapatil commented 1 month ago

Hello folks I have facing some issue while opening scanner, when I opening scanner then it will display info icon instead of camera.

image

camera is not detect properly and not scanning, am providing some code

Declaration/Functions/Methods:

  @override
  void dispose() {
    cameraController.stop();
    cameraController.dispose();
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
    startCamera();
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    if (state == AppLifecycleState.inactive || state == AppLifecycleState.paused) {
      cameraController.stop();
      print("Lifecycle stop is call....");
    } else if (state == AppLifecycleState.resumed) {
      startCamera();
      print("Lifecycle resume is call....");
    }
  }

  Future<void> startCamera() async {
    if (cameraController != null) {
      await cameraController.start();
    }
  }

  void resetScanner() async {
    await cameraController.stop();
    await Future.delayed(const Duration(milliseconds: 500)); // Wait for the scanner to stop
    await cameraController.start();
  }

UI:

MobileScanner(
                                fit: BoxFit.fill,
                                controller: MobileScannerController(detectionSpeed: DetectionSpeed.noDuplicates),
                                onDetect: (capture) {
                                  print("-->>" + capture.barcodes.toString());
                                  final List<Barcode> barcodes = capture.barcodes;
                                  for (final barcode in barcodes) {
                                    if (barcode.rawValue != null) {
                                      print("Barcode value: ${barcode.rawValue!}");
                                    // Going to next screen after successfully scanned qr 
                                     } else {
                                      print("Something Wrong");
                                    }
                                  }
                                },
                              ),
navaronbracke commented 1 month ago

The black screen with (i) icon is the default error builder for the MobileScanner widget. What error details (message + stacktrace) do you get in the MobileScannerException ? (you can inspect it using the MobileScanner.errorBuilder property)

kcs-ravindrapatil commented 1 month ago

@navaronbracke Yes I know this is error builder but it gives the below error

MobileScannerException: code genericError, message: Called start() while already started

navaronbracke commented 1 month ago

Aha, I see. Does this happen after you grant permissions?

I have a fix in progress for the underlying "Called start() while already started" error. The fix would just ignore the second attempt at calling start, if the controller has been started before. We do a similar thing for the stop method (not trying to stop when already stopped)

kcs-ravindrapatil commented 1 month ago

@navaronbracke I understand, waiting for solving the issue.

In my case for below steps:

  1. When I am scanning it 1st time it will working well and redirect to next(result) screen
  2. When am back from result screen it work for scanning the QR but camera is hide
  3. If am go back to all screens and scan it again the camera is not display and not scanning
  4. When am restart or kill the app then above issue has been faced.
nkesarwani7 commented 1 month ago

i'm also facing this same issue, here is small demo video: https://drive.google.com/file/d/1oL6nkTKZ8rPatdIuFLGE8WIYQyxl4Egm/view?usp=sharing