1. Flutter
  2. Single Barcode Scanning

Flutter

Single Barcode Scanning

Single barcode scanning allows you to detect and process one barcode at a time within the scanning window. This ensures precise detection by limiting the scan area, improving accuracy and performance.

⚙️ Setting Single Barcode Scanning

To enable single barcode scanning, you need to call the following method from FlutterToPluginCommunicator:

        class VisionSDKStatelessWidget extends StatelessWidget {
  const VisionSDKStatelessWidget({super.key, required this.listener});

  final PluginToFlutterCommunicator listener;

  @override
  Widget build(BuildContext context) {
    return VisionCameraWidget(
      listener: listener,
      onViewCreated: (FlutterToPluginCommunicator sender) {
        sender.setCaptureModeBarcode();
        sender.setMultipleScanEnabled(false);
        sender.startCamera();
      },
    );
  }
}

      

Select the camera lens you want to use for barcode scanning:

        sender.setCameraSettings(CameraSettings(cameraLensFace: CameraLensFace.back));

      

🤖 Auto Mode Barcode Scanning

For automatic barcode capture, call the following method:

        sender.setScanAuto();

      

✋ Manual Mode Barcode Scanning

For manual barcode capture, call the following method:

        sender.setScanManual();

      

Afterwards, when you want to capture, call the following method:

        sender.capturePhoto();

      

📦 Callback Method for Barcode Values

When a barcode is successfully scanned, the following callback method is called:

        class Listener implements PluginToFlutterCommunicator {
  @override
  void onCodesReceived(List<ScannedCodeResult> scannedCodeResults) {
    
  }
}

      

Note that, once onCodesReceived or onImageCaptured callbacks are called, VisionSDK will stop analyzing camera feed for text or barcodes/QR codes. This is to prevent extra processing and battery consumption. When client wants to start analyzing camera feed again, after consuming the results of previous scan, client needs to call the following function:

        sender.rescan()

      

❗️Error Handling Callback

If an error occurs during the barcode scanning process, the following callback method will be triggered. You can implement this to handle any failures during scanning, gracefully.

        class Listener implements PluginToFlutterCommunicator {
  @override
  void onScanError(String error) {
    
  }
}

      

Use this method to log errors, display messages to the user, or perform recovery actions when scanning fails.

🧾 Barcode Object Structure

The ScannedCodeResult object returned in the callback contains the following properties:

        class ScannedCodeResult {
  final String scannedCode;
  final BarcodeSymbology symbology;
  final Map<String, String>? gs1ExtractedInfo;
}

      

🧭 Detection Indicators

To receive detection indicators (without actual values), implement the following callback method:

        class Listener implements PluginToFlutterCommunicator {
  @override
  void onDetectionResult(bool isText, bool isBarcode, bool isQrCode, bool isDocument) {
    
  }
}

      
  • isText: Indicates presence of text
  • isBarcode: Indicates presence of a barcode
  • isQrCode: Indicates presence of a QR code
  • isDocument: Indicates presence of a document

🔘 Enable Detection Indicators

You can toggle detection indicators as follows:

        class VisionSDKStatelessWidget extends StatelessWidget {
  const VisionSDKStatelessWidget({super.key, required this.listener});

  final PluginToFlutterCommunicator listener;

  @override
  Widget build(BuildContext context) {
    return VisionCameraWidget(
      listener: listener,
      onViewCreated: (FlutterToPluginCommunicator sender) {
        sender.setObjectDetectionSettings(
          ObjectDetectionSettings(
            isTextIndicationOn: true,
            isBarcodeOrQRCodeIndicationOn: true,
            isDocumentIndicationOn: true
          )
        );
      },
    );
  }
}

      

🎯 Focus Area Configuration

In single barcode scanning, you can also define the scan region. A scan region is a rectangular area inside VisionCameraWidget. If you enable focus area, only the barcodes inside that rectangular area will be sent in onCodesReceived. To define focus area, use the object of FocusRegionManager that you can get after camera is started:

        class VisionSDKStatelessWidget extends StatelessWidget {
  const VisionSDKStatelessWidget({super.key, required this.listener});

  final PluginToFlutterCommunicator listener;

  @override
  Widget build(BuildContext context) {
    return VisionCameraWidget(
      listener: listener,
      onViewCreated: (FlutterToPluginCommunicator sender) {
        sender.setFocusSettings(
          FocusSettings(
            focusImage: '', // Base 64 image
            focusImageRect: Rect.zero,
            shouldDisplayFocusImage: false,
            shouldScanInFocusImageRect: false,
            focusImageTintColor: '#FFFFFF66',
            focusImageHighlightedColor: '#FFFFFF',
            showCodeBoundariesInMultipleScan: true,
            validCodeBoundaryBorderColor: '#00FF00',
            validCodeBoundaryBorderWidth: 2,
            validCodeBoundaryFillColor: '#00AA0044',
            invalidCodeBoundaryBorderColor: '#AA0000',
            invalidCodeBoundaryBorderWidth: 2,
            invalidCodeBoundaryFillColor: '#AA0000',
            showDocumentBoundaries: true,
            documentBoundaryBorderColor: '#FFD966',
            documentBoundaryFillColor: '#FFD96644',
          )
        );
      },
    );
  }
}

      

For assistance of scanning region visually during development or debugging:

shouldDisplayFocusImage = true

🧪 Sample Code

To start scanning for barcodes, QR codes, text or documents, use the startCamera method. See the code below for example:

        class VisionSDKStatelessWidget extends StatelessWidget {
  const VisionSDKStatelessWidget({super.key, required this.listener});

  final PluginToFlutterCommunicator listener;

  @override
  Widget build(BuildContext context) {
    return VisionCameraWidget(
      listener: listener,
      onViewCreated: (FlutterToPluginCommunicator sender) {
        sender.setCaptureModeBarcode();
        sender.setMultipleScanEnabled(false);
        sender.setCameraSettings(CameraSettings(cameraLensFace: CameraLensFace.back));
        sender.setScanAuto();
        sender.setObjectDetectionSettings(
          ObjectDetectionSettings(
            isTextIndicationOn: true,
            isBarcodeOrQRCodeIndicationOn: true,
            isDocumentIndicationOn: true
          )
        );
        sender.startCamera();
      },
    );
  }
}

class Listener implements PluginToFlutterCommunicator {
  @override void onCameraStarted() {}
  @override void onCameraStopped() {}
  @override void onCodesReceived(List<ScannedCodeResult> scannedCodeResults) {}
  @override void onDetectionResult(bool isText, bool isBarcode, bool isQrCode, bool isDocument) {}
  @override void onImageCaptured(Uint8List byteArrayImage, List<ScannedCodeResult> scannedCodeResults) {}
  @override void onMatchingResult(Map<String, dynamic> result) {}
  @override void onOnlineBOLResult(Map<String, dynamic> result) {}
  @override void onOnlineDocumentClassificationResult(Map<String, dynamic> result) {}
  @override void onOnlineItemLabelResult(Map<String, dynamic> result) {}
  @override void onOnlineSLResult(Map<String, dynamic> result) {}
  @override void onScanError(String error) {}
}