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
877 stars 510 forks source link

Can't read barcode flutter #1151

Open DevMansoor opened 2 months ago

DevMansoor commented 2 months ago

I'm encountering an issue with barcode scanning using mobile_scanner While the scanner works perfectly with barcodes on paper, it struggles to read barcodes printed on boxes.

The problem occurs even in well-lit conditions, and I’ve tried adjusting the distance and angle, but the barcode on the box still isn’t being detected. Attached is an image of the box in question.

What could be causing this issue?

WhatsApp Image 2024-08-15 at 3 37 07 PM

Here is the my code:

 Widget oneTapedQuickScanBarCode(BuildContext context) {
    barcode = null;
    return LayoutBuilder(
      builder: (context, constraints) {
        final Size layoutSize = constraints.biggest;
        final double scanWindowWidth = 250.w;
        final double scanWindowHeight = 110.h; 
        final Rect scanWindow = Rect.fromCenter(
          center: layoutSize.center(Offset.zero),
          width: scanWindowWidth,
          height: scanWindowHeight,
        );

        return MobileScanner(
          scanWindow: scanWindow,
          overlayBuilder: (context, constraints) {
            return Stack(
              children: [
                Positioned.fill(
                  child: Container(
                    decoration: ShapeDecoration(
                      shape: QrScannerOverlayShape(
                        borderColor: Colors.white,
                        borderRadius: 10,
                        borderLength: 20,
                        borderWidth: 5,
                        cutOutWidth: scanWindowWidth,
                        cutOutHeight: scanWindowHeight,
                      ),
                    ),
                  ),
                ),
              ],
            );
          },
          onDetect: (capture) async {
            if (capture.image != null) {

              img.Image? fullImage = img.decodeImage(capture.image!);

              if (fullImage != null) {

                final int cropX = (fullImage.width - scanWindowWidth) ~/ 2;
                final int cropY = (fullImage.height - scanWindowHeight) ~/ 2;
                final int cropWidth = scanWindowWidth.toInt();
                final int cropHeight = scanWindowHeight.toInt();

                img.Image croppedImage =
                    img.copyCrop(fullImage, x: cropX, y: cropY, width: cropWidth, height: cropHeight);

                final Uint8List croppedImageBytes = Uint8List.fromList(img.encodeJpg(croppedImage));

                image = croppedImageBytes;
                barcode = capture.barcodes.first.rawValue;
                debugPrint('Barcode value: $barcode');

                await _scannerController.stop();
                _scannerController.dispose();
                if (widget.passDataBack) {
                  Get.back(result: capture);
                  return;
                }

                setState(() {});
                var bottomNavbarController = Get.find<BottomNavController>();
                await bottomNavbarController.validateIMEI(barcode!, image);
              }
            }
          },
          controller: _scannerController,
        );
      },
    );
  }

  void initController() {
    _scannerController = MobileScannerController(
      detectionSpeed: DetectionSpeed.normal,
      returnImage: true,
      useNewCameraSelector: true,
      torchEnabled: _isFlashOn,
    );
  }
navaronbracke commented 2 months ago

Does not using a scan window have any effect? Just to be sure.

If that isn't the case, then I think this might be a problem with MLKit's detection of the barcode.

DevMansoor commented 2 months ago

I tried but still same issue, any idea how can I resolve this issue ??

navaronbracke commented 2 months ago

Hmm, then this seems to be an MLKit issue. Unfortunately I don't think they have a newer version available (I think we already use the latest version)

Does using the bundled versus unbundled MLKit make any difference?

navaronbracke commented 2 months ago

It seems there are updates to MLKit available, that we missed. I'll put this on the tracker for the next release.

We can update to the following versions Bundled: 17.2.0 -> 17.3.0 Unbundled: 18.3.0 -> 18.3.1

I'll have to take a look at the changelog, though.

DevMansoor commented 2 months ago

Thanks, can you confirm when you'll release next update as we are stuck on this issue,

DevMansoor commented 2 months ago

one more thing i can read top bar code (8806094018431) but not the bottomLeft IMEI, What can be the reason ??

DevMansoor commented 2 months ago

LfEb3 How can I add a blue animated horizontal line that moves up and down to focus on the barcode, and as soon as it aligns with the barcode, it reads it?

navaronbracke commented 2 months ago

Thanks, can you confirm when you'll release next update as we are stuck on this issue,

Version 5.2.0 should be available on Pub.dev. This version includes this fix.

navaronbracke commented 2 months ago

one more thing i can read top bar code (8806094018431) but not the bottomLeft IMEI, What can be the reason ??

I am not sure why that is the case.

How can I add a blue animated horizontal line that moves up and down to focus on the barcode, and as soon as it aligns with the barcode, it reads it?

I'm not sure if that is possible right now. You can try using the scan window to make the scan region smaller. But for the line to "scan the barcode when it aligns", we would have to expose position information of the scanned barcode (where in the output image is the barcode when it is scanned)

You could try drawing the corner shape & line with a custom painter, though. You'll want to align it with your scan window.

DevMansoor commented 2 months ago

I tried after you update package, it captured first the barcode but now it is not capturing the barcode and its taking too much time to read the barcode