juliuscanute / qr_code_scanner

QR Code Scanner for Flutter
BSD 2-Clause "Simplified" License
996 stars 745 forks source link

[BUG] Can't scan QR after "resume" #471

Open Amattia opened 2 years ago

Amattia commented 2 years ago

I need to show a particular activity if the qrcode reader was able to read the code in the QR image. So I built a function inside "controller.scannedDataStream.listen ((scanData)" which after appropriate checks shows a new screen, to do this I wrote:

var future = Navigator.push (context, MaterialPageRoute (builder: (context) => const ScanKeys (), settings: RouteSettings (arguments: badgeCode)));
  future.then ((value) async {
  await controller? .resumeCamera ();
}); 

When the new activity (B) is started, the camera preview is paused, which is why in the "future.then" of the first activity (A) I specified the "resumeCamera" so that I can start up the qr reader again when activity B is closed.

When I close activity B, it happens that the preview starts working again but the code present in controller.scannedDataStream.listen doesn't seem to work anymore.

Flutter information [✓] Flutter (Channel stable, 2.8.0, on macOS 12.0.1 21A559 darwin-x64, locale en-IT) • Flutter version 2.8.0 at /Volumes/Samsung_USB/Android/flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision cf44000065 (7 days ago), 2021-12-08 14:06:50 -0800 • Engine revision 40a99c5951 • Dart version 2.15.0

[✓] Android toolchain - develop for Android devices (Android SDK version 32.0.0) • Android SDK at /Volumes/Samsung_USB/Android/sdk • Platform android-32, build-tools 32.0.0 • ANDROID_HOME = /Volumes/Samsung_USB/Android/sdk • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7281165) • All Android licenses accepted.

[!] Xcode - develop for iOS and macOS (Xcode 12.5.1) • Xcode at /Applications/Xcode.app/Contents/Developer ! Flutter recommends a minimum Xcode version of 13.0.0. Download the latest version or update via the Mac App Store. • CocoaPods version 1.10.1

[✓] Connected device (2 available) • SM T515 (mobile) • R52N50JW3RV • android-arm • Android 11 (API 30) • Chrome (web) • chrome • web-javascript • Google Chrome 96.0.4664.110

! Doctor found issues in 1 category.

Device (please complete the following information):

digarahayu commented 2 years ago

i got same issue testing on iPhone

Y07yoyo commented 2 years ago

I am doing something similar where when I get a barcode result I navigate to another page:

controller.scannedDataStream.listen((scanData) {
      if (!scanned) {
        scanned = true;
        setState(() {
          if (scanData.code != null && scanData.code.isNotEmpty) {
            controller.stopCamera();
            Navigator.pushAndRemoveUntil(
                context,
                PageTransition(
                  type: PageTransitionType.fade,
                  duration: Duration(milliseconds: 300),
                  reverseDuration: Duration(milliseconds: 300),
                  child: CheckingBookingWidget(barcode: scanData),
                ),
                (r) => false,
              );

It then process the info and comes back to the scanning page. Once in a while, when it comes back, the camera preview is black and I cannot scan any barcode. Can anyone help me with this?

wyzard commented 2 years ago

I have a similar problem. Have you solved the problem?

koukibadr commented 2 years ago

I have a similar problem.

hyejin-1010 commented 2 years ago

I have a similar problem.

joy-sec commented 2 years ago

@Amattia did you try this one on the QrView page

 @override
  void reassemble() {
    super.reassemble();
    if (Platform.isAndroid) {
      controller!.pauseCamera();
    } else if (Platform.isIOS) {
      controller!.resumeCamera();
    }
  }
iamsahilsonawane commented 2 years ago

@Amattia Mine works if I remove the reassemble override

JettChen12 commented 1 year ago

I found the solution.

The camera will not auto start when I navigate to qrView in android, so I need to add this line on onQRViewCreated.

  void _onQRViewCreated(QRViewController controller) async {
    await controller.resumeCamera();
  }

But in ios, it will auto start, and the line I added cause this issue.

the solution is

  void _onQRViewCreated(QRViewController controller) async {
   if (Platform.isAndroid) {
      await controller.resumeCamera();
    }
  }
flyingV805 commented 7 months ago

I found a solution for this problem on iOS (iPhone XR).

In my case, it was enough to call controller.resumeCamera(); with a slight delay. This code solved the problem for me

Future<void>.delayed(Duration(milliseconds: 200), (){
   controller.resumeCamera();
});

Hope it helps