Allows the recognition of optical encoded information related to SBB use cases. | Read-only mirror of https://github.com/SchweizerischeBundesbahnen/data-scanner-flutter — SBB. Issues & pull requests at the source.
  • Dart 99.3%
  • Swift 0.6%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
renovate[bot] f1c8bf78ce
chore(deps): update plugin org.jetbrains.kotlin.android to v2.5.0 (#78)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2026-12-02 16:00:28 +01:00
.github chore(deps): update gradle to v9 (#83) 2026-11-27 15:57:43 +01:00
assets feat: add initial structure for open source repo 2024-12-16 16:43:49 +01:00
bin feat!: add SPM support from community port 2026-11-27 13:04:39 +01:00
ci Release 5.0.0 (#80) 2026-11-27 16:01:05 +01:00
example chore(deps): update plugin org.jetbrains.kotlin.android to v2.5.0 (#78) 2026-12-02 16:00:28 +01:00
lib fix(android): avoid oversized NV21 buffer and unused plane copy per frame (#87) 2026-11-27 15:01:39 +01:00
test chore(deps): update gradle to v9 (#83) 2026-11-27 15:57:43 +01:00
.gitignore chore: fix ios example app build by resetting ios example app dir (#3) 2024-12-18 17:46:46 +01:00
.pubignore Chore/prepare for pub dev (#6) 2024-12-18 19:23:59 +01:00
analysis_options.yaml chore: apply stricter linting rules (#84) 2026-11-27 13:39:04 +01:00
CHANGELOG.md Release 5.0.0 (#80) 2026-11-27 16:01:05 +01:00
CODE_OF_CONDUCT.md Initial commit 2024-12-11 14:59:31 +01:00
CODING_STANDARDS.md feat: add initial structure for open source repo 2024-12-16 16:43:49 +01:00
CONTRIBUTING.md feat: add initial structure for open source repo 2024-12-16 16:43:49 +01:00
LICENSE feat: add initial structure for open source repo 2024-12-16 16:43:49 +01:00
pubspec.yaml Release 5.0.0 (#80) 2026-11-27 16:01:05 +01:00
README.md feat!: add SPM support from community port 2026-11-27 13:04:39 +01:00
RELEASE.md feat: add initial structure for open source repo 2024-12-16 16:43:49 +01:00
renovate.json chore: add renovate json 2024-12-16 17:34:53 +01:00

This package allows the recognition of optical encoded information related to SBB use cases. It is built on top of google_mlkit and runs solely on-device.

Table Of Contents

Click to expand

Getting-Started

Supported platforms

Android badge iOS badge

Preconditions

This package uses the Flutter Camera plugin to access the device camera. Make sure to follow their installation instructions.

Furthermore, follow the platform requirements on google_mlkit_barcode_scanning and google_mlkit_text_recognition, including the minimum iOS and Android SDK versions. For iOS integration using Swift Package Manager (SPM), complete the additional steps below.

iOS setup with Swift Package Manager

Follow Flutter's Swift Package Manager integration instructions and use iOS 15.5 or later.

Complete the ML Kit SPM installation steps in your app target:

  • Add -ObjC and -all_load to Other Linker Flags for Debug, Profile, and Release, preserving $(inherited) and existing flags.

  • For text recognition, add a Run Script phase to Runner after Copy Bundle Resources, disable Based on dependency analysis, and set User Script Sandboxing to No (as for Flutter's build scripts):

    set -e
    cd "$FLUTTER_APPLICATION_PATH"
    "$FLUTTER_ROOT/bin/dart" run sbb_data_scanner:install_ios_ocr_resources
    

    This downloads LatinOCRResources.bundle once per ML Kit version, validates the ZIP, caches it under ~/Library/Caches/sbb_data_scanner/ocr/, and copies it into the app before codesigning on each build. Subsequent builds work offline; CI needs network access on a cache miss. No bundle needs to be checked in or added to Copy Bundle Resources manually. Remove any existing manual bundle entry to avoid duplicate copies. Barcode-only apps need no script or resource bundle.

The script selects the release matching google-mlkit-swiftpm in ios/Runner.xcworkspace/xcshareddata/swiftpm/Package.resolved automatically. Each version has its own cache; the first build after an upgrade requires network access. Missing release assets fail the build; branch/revision pins require manual setup. ZIP validation detects corruption but does not verify a pinned SHA-256 checksum. The downloaded model remains subject to Google's ML Kit terms.

A missing OCR bundle causes MLKTextRecognizerInternalErrorCreationFailure: Invalid model path at runtime, even when the build succeeds.

In Code Usage

After importing the library, you can add a DataScanner to your widget tree. You can control its size by wrapping it in a SizedBox for example. If there are no size constraints (e.g. in a Row or Column), the DataScanner will not exceed the screen's width and/or height (depending on orientation).

SizedBox(
  height: 250,
  width: 400,
  child: DataScanner( /* ... */ ),
)

If you want a full-page scanner, you can use the DataScannerPage widget instead.

Navigator.of(context).push(
  MaterialPageRoute(
    builder: (context) => DataScannerPage( /* ... */ ),
  ),
);

Documentation

Features

Feature iOS Android
Optical Character Recognition ✅ ✅
Barcode Scanning ✅ ✅
QR Code Scanning ✅ ✅
UIC Number recognition ✅ ✅

DataScanner Configuration

You can configure the DataScanner by passing a DataScannerConfiguration<T> into it, where T stands for whichever type a successful extraction will return. This will be covered in detail in the Extractors section, but is commonly inferred by the compiler. Almost all fields have default values, the exceptions being the required values processor and extractor which will be covered in their respective sections.

DataScanner(
  scannerConfiguration: DataScannerConfiguration<T>(
    processors: // Covered in their own section
    extractors: // Covered in their own section

    routeObserver: RouteObserver<ModalRoute>(),

    onExtracted: (T value) {
      // Called for every extracted value which is
      // a) inside the detection area,
      // b) not null, and
      // c) not equal to the previously extracted value
    },

    onPermissionDenied: () {
      // Called when the app does not have camera permissions
    },

    onError: (String error) {
      // Called when the library encounters any other camera error
    },

    showTorchToggle: true,
    torchToggleMargin: EdgeInsets.all(32),
    torchToggleAlignment: Alignment.bottomCenter,

    showOverlay: true,
    upperHelper: Text('Appears above the detection area'),
    lowerHelper: Text('Appears below the detection area'),

    detectionAreaHeight: 75,
    detectionAreaWidth: 250,
    detectionAreaMode: DetectionAreaMode.containsRect,

    detectionOutline: DetectionOutlineConfig(
      margin: 5,
      padding: 10,

      activeOutlineColor: Colors.green,
      inactiveOutlineColor: Colors.grey,
      outlineWidth: 2,
      cornerRadius: 5,

      enableLabel: true,
      labelColor: Colors.white,
      labelSize: 12,
      labelPosition: DetectionLabelPosition.bottomLeft,
    ),

    enableZoom: true,
    onZoomChanged: (double zoom) {
      // is called if zoom changes with a minimum change sensitivity of 0.01
    }
  ),
)

Vision Processors

The purpose of vision processors is to detect elements in an image and return their value and position relative to an origin point (usually the top left corner). The library ships with three preconfigured vision processors, all based on Google ML Kit:

Processor Name Note
TextRecognitionVisionProcessor Recognize text in images. Only supports the Latin script. Please contact a maintainer for other scripts.
OneDimensionalBarcodeVisionProcessor This extractor extracts the value of UIC numbers and provides information and detailed descriptions for it.
TwoDimensionalBarcodeVisionProcessor This extractor extracts the value of GS1 codes and provides information and detailed descriptions for it.

Add a list of processor(s) to the DataScannerConfiguration processors argument:

DataScanner(
  scannerConfiguration: DataScannerConfiguration(
    // ...
    processors: [
      TextRecognitionVisionProcessor(),
      // OneDimensionalBarcodeVisionProcessor(),
      // TwoDimensionalBarcodeVisionProcessor()
    ],
  ),
)

Additionally, it is possible create your own custom vision processor by implementing the VisionProcessor interface. This allows the library to also detect elements other than text, QR-codes and barcodes:

abstract class VisionProcessor {
  Future<Map<Rect, String?>> getBoundingBoxes({
    required CameraImage image,
    required int imageRotation,
  });
}

Extractors

An extractor takes the values detected by the vision processor, and tries to extract data in a certain format (like URLs, UIC Numbers or GS1 codes). At the same time, it also works as a filter, since detected values which to not match the given format are dismissed. The library ships with three preconfigured extractors, which can be supplied to the DataScannerConfiguration via the extractors parameter.

Extractor Name Return Type (<T> of DataScannerConfiguration) Note
RawTextExtractor String? Only for unit testing
UICDetailsExtractor UICDetails? This extractor extracts the value of UIC numbers and provides information and detailed descriptions for it.
GS1DetailsExtractor GS1Details? This extractor extracts the value of GS1 codes and provides information and detailed descriptions for it.

Exemplary UICDetailsExtractor implementation:

DataScanner(
  scannerConfiguration: DataScannerConfiguration<UICDetails>(
    // ...
    extractors: [
      UICDetailsExtractor( /* ... */ ),
    ],
  ),
)
Custom Extractors

But what about URLs, phone numbers, and brands owned by Nestlé, etc.? Fear not, it's quite simple to extract these kind of formats. Simply implement the Extractor<T> interface, and you're good to go.

abstract class Extractor<T> {
  T? extract(String? input);
}

The extract method must return a nullable type. So, when do we return null? It's simple: When the value cannot be extracted due to formatting, missing components in input, or an error. This causes the ScannerConfiguration.onExracted to not be fired, as the detected value has an invalid format.

For example, here is a simple CompoundSentenceExtractor which only extracts values which are a compund sentence, as indicated by a comma and trailing period.

class CompoundSentenceExtractor implements Extractor<String> {
  String? extract(String? input) {
    if (input == null) return null;

    final containsComma = input.contains(',');
    final isSentence = input.endsWith('.');

    // Why yes, of course ",." is a valid compound sentence!
    return containsComma && isSentence ? input : null;
  }
}

You can now use your own extractor:

DataScanner(
  scannerConfiguration: DataScannerConfiguration<String>(
    // ...
    extractors: [
      CompoundSentenceExtractor(),
    ],
  ),
)

License

This project is licensed under MIT.

Contributing

This repository includes a CONTRIBUTING.md file that outlines how to contribute to the project, including how to submit bug reports, feature requests, and pull requests.

Maintainer

Credits

In addition to the contributors on Github, we thank the following people for their work on previous versions:

  • Ulrich Raab (previous maintainer)

Coding Standards

See CODING_STANDARDS.md.

Code of Conduct

See CODE_OF_CONDUCT.md.