jhoogstraat / fast_barcode_scanner

A flutter plugin that allows your users to scan any type of barcode on iOS and Android
40 stars 62 forks source link

Error: Camera is already initialized #28

Closed seth35us closed 2 years ago

seth35us commented 3 years ago

I copied the example code from the update-config branch into a new project with a bottom navigation bar. When switching tabs, I get both camera initialization and camera dispose errors.

I have included a sample main class for your review. So far, I have not figured out the root cause of the issue.

You should be able to just replace your main and replicate the issue. Please let me know if you need more information on this.

main.dart.zip

jhoogstraat commented 3 years ago

Please try the latest commit. It should work now 👍🏼

seth35us commented 3 years ago

Your fix worked great! However, I am having issues reading anything other than QR codes on iOS. I am testing on iOS 14.7.1 Here is my code:

` @override Widget build(BuildContext context) { // ignore: avoid_print print('Build QR View');

final List<BarcodeType> barcodeTypes = [
  BarcodeType.aztec,
  BarcodeType.code128,
  BarcodeType.code39,
  BarcodeType.code93,
  BarcodeType.dataMatrix,
  BarcodeType.ean13,
  BarcodeType.ean8,
  BarcodeType.itf,
  BarcodeType.pdf417,
  BarcodeType.qr,
  BarcodeType.upcA,
  BarcodeType.upcE
];

if (Platform.isAndroid) {
  // this code doesn't work on iOS yet
  barcodeTypes.add(BarcodeType.codabar);
}

if (Platform.isIOS) {
  // this code doesn't work on Android yet
  barcodeTypes.add(BarcodeType.code39mod43);
  barcodeTypes.add(BarcodeType.interleaved);
}

return FutureBuilder<bool>(
    future: hasCameraPermission(context),
    builder: (BuildContext context, AsyncSnapshot<bool> snapshot) {
      Widget child;
      if (snapshot.hasData) {
        if (snapshot.data == true) {
          child = BarcodeCamera(
            types: barcodeTypes,
            mode: sharedPrefs.batchScan
                ? DetectionMode.pauseVideo
                : DetectionMode.pauseVideo,
            position: cameraPosition,
            onScan: (barcode) => onScan(barcode),
            children: [
              const ScanOverlay(),
              if (sharedPrefs.batchScan) const BlurPreviewOverlay(),
            ],
          );
        } else {
          child = Container(
            color: Colors.black,
          );
        }
      } else {
        child = Container(
          color: Colors.black,
        );
      }
      return child;
    });

}

Future hasCameraPermission(BuildContext context) async { final perComp = Completer();

PermissionsHelper().handleCameraPermission(context, (val) {
  if (!perComp.isCompleted) {
    perComp.complete(val);
  }
});

return perComp.future;

} `

jhoogstraat commented 3 years ago

That's weird. I did some tests with EAN13 earlier and it worked fine.

I am thinking about adding unit tests with sample code images to check if every type is recognized as expected.

seth35us commented 3 years ago

Not sure why iOS wasn’t scanning. I tried again today and it was working perfectly. I tried every possible barcode type. Thanks for checking.

The only other problem I had was on Android calling resumeScanner didn’t work after scanning and staying on the same page on Android. I had to call pauseCamera and resumeCamera on Android for it to scan again. I used pauseDetection after scanning.

jhoogstraat commented 3 years ago

Are you using DetectionMode.pauseDetection? The behavior should be the same on iOS and Android.

Edit: Does it work on the current main branch? If not, I'll look into the pauseDetection issue.

guilhermedaldim commented 2 years ago

https://github.com/jhoogstraat/fast_barcode_scanner/issues/27#issuecomment-922474865

@jhoogstraat i'm having the same issue.

seth35us commented 2 years ago

It looks like version 1.1.2 fixed this. I am using DetectionMode.pauseVideo and CameraController.instance.resumeDetector(). @guilhermedaldim are you still seeing the issue with those settings on the most recent build?

guilhermedaldim commented 2 years ago

I no longer see this issue in the latest version.

@jhoogstraat adjusted this as commented here https://github.com/jhoogstraat/fast_barcode_scanner/issues/27#issuecomment-923951603.