1. Flutter
  2. On-Device AI Scanning (OCR)

Flutter

On-Device AI Scanning (OCR)

The Vision SDK allows for on-device AI scanning (OCR), enabling offline extraction of structured information from documents such as shipping labels. This is ideal when low latency and offline functionality are critical, such as in warehouse and logistics environments.

πŸ› οΈ Step 1: Preparing On-Device OCR

Before using the on-device OCR, you must prepare the model using the following methods. This ensures the necessary AI models are downloaded and ready to use.

        class MyOnDeviceOCRManager {

  final OnDeviceOCRManagerCommunicator onDeviceOCRManagerCommunicator;

  MyOnDeviceOCRManager(OnDeviceOCRManagerListener listener):onDeviceOCRManagerCommunicator = OnDeviceOCRManagerCommunicator(listener);

  void configureOnDeviceOCR(
    {String? apiKey,
    String? token,
    required ModelClass modelClass,
    required ModelSize modelSize}) {
      onDeviceOCRManagerCommunicator.configureOnDeviceOCR(
        apiKey: apiKey,
        token: token,
        modelClass: modelClass,
        modelSize: modelSize,
      );
  }
}

class OnDevceListener implements OnDeviceOCRManagerListener {
  @override void onOnDeviceConfigurationComplete() {}
  @override void onOnDeviceConfigureProgress(double progress) {}
}

      

⚠️ You must wait for the configure process to complete before proceeding to OCR extraction.

🧩 Selecting the Model Type

When preparing for on-device AI scanning, you can choose which document model you want to use by specifying the modelClass parameter. This lets the SDK know which type of document you’re scanning so it can load the appropriate AI model.

Here are the supported options:

πŸ“¦ For Shipping Labels

Use this to scan and extract structured data from shipping labels.

        modelClass: ModelClass.shippingLabel

      

πŸ“„ For Bill of Lading (BOL)

Use this to extract data from Bill of Lading documents.

        modelClass: ModelClass.billOfLading

      

🏷️ For Item Labels

Use this to process and extract structured details from product/item labels.

        modelClass: ModelClass.itemLabel

      

πŸ“ Make sure the model is prepared successfully before scanning. Each model may vary in size and complexity depending on the document type.

🧠 Step 2: Extracting Data from Image

Once the model is prepared successfully, use the following method to perform OCR on a given image.

        class MyOnDeviceOCRManager {

  final OnDeviceOCRManagerCommunicator onDeviceOCRManagerCommunicator;

  MyOnDeviceOCRManager(OnDeviceOCRManagerListener listener):onDeviceOCRManagerCommunicator = OnDeviceOCRManagerCommunicator(listener);

  void getPredictions(Uint8List byteArrayImage, List<String> barcodes) {
    onDeviceOCRManagerCommunicator.getPredictions(byteArrayImage, barcodes);
  }
}

class OnDevceListener implements OnDeviceOCRManagerListener {
  @override void onOnDeviceOCRResult(Map<String, dynamic> result) {}
  @override void onError(String error) {}
}

      

Parameters:

  • byteArrayImage: The Uint8List byte array of image you want to process.
  • barcodes: A list of String values representing barcodes detected in the image (if any).

πŸ” The returned data follows the same structure as the PackageX Cloud OCR API response.

β™² Step 3: Release resources

It is important that when you are done with predictions and you are not planning to get more predictions on-device, or you want to configure a different AI model, then you release the memory being held by AI model. You can do that by using following code:

        class MyOnDeviceOCRManager {

  final OnDeviceOCRManagerCommunicator onDeviceOCRManagerCommunicator;

  MyOnDeviceOCRManager(OnDeviceOCRManagerListener listener):onDeviceOCRManagerCommunicator = OnDeviceOCRManagerCommunicator(listener);

  void release() {
    onDeviceOCRManagerCommunicator.release();
  }
}

      

Available On-Device AI Scanning

Following table explains all the on-device AI scanning options available currently.

Model Class Nano Micro Small Medium Large XLarge
ShippingLabel X βœ“ X X βœ“ X
BillOfLading X X X X βœ“ X
ItemLabel X X X X βœ“ X
DocumentClassification X X X X βœ“ X

βœ… Full Sample Flow

        class MyOnDeviceOCRManager {

  final OnDeviceOCRManagerCommunicator onDeviceOCRManagerCommunicator;

  MyOnDeviceOCRManager(OnDeviceOCRManagerListener listener):onDeviceOCRManagerCommunicator = OnDeviceOCRManagerCommunicator(listener);

  void configureOnDeviceOCR(
    {String? apiKey,
    String? token,
    required ModelClass modelClass,
    required ModelSize modelSize}) {
      onDeviceOCRManagerCommunicator.configureOnDeviceOCR(
        apiKey: apiKey,
        token: token,
        modelClass: modelClass,
        modelSize: modelSize,
      );
  }

  void getPredictions(Uint8List byteArrayImage, List<String> barcodes) {
    onDeviceOCRManagerCommunicator.getPredictions(byteArrayImage, barcodes);
  }

  void release() {
    onDeviceOCRManagerCommunicator.release();
  }
}

class OnDevceListener implements OnDeviceOCRManagerListener {
  @override void onError(String error) {}
  @override void onOnDeviceConfigurationComplete() {}
  @override void onOnDeviceConfigureProgress(double progress) {}
  @override void onOnDeviceOCRResult(Map<String, dynamic> result) {}
}