makePredictionWithModule method
- required ModelClass modelClass,
- required ModelSize modelSize,
- required Uint8List byteArrayImage,
- required List<
ScannedCodeResult> barcodes, - String? apiKey,
- String? token,
- ShippingLabelOptions? shippingLabelOptions,
Performs OCR prediction using models loaded via ModelManager.
This method allows dynamic switching between different OCRModules without creating new OnDeviceOCRManager instances. Use this when models were loaded using the new ModelManager API.
modelClass - The model class to use for prediction
modelSize - The model size to use for prediction
byteArrayImage - The image to perform OCR on
barcodes - List of detected barcodes (can be empty)
apiKey / token - Optional credentials for cloud address parsing.
Supply apiKey or token when the model was loaded via ModelManager (not
configureOnDeviceOCR) AND shipping-label sender/recipient address parsing is
enabled. ModelManager-loaded models never went through configure(), so on
Android the manager holds no credentials and address parsing silently stays off
without them.
iOS ignores both: OnDeviceOCRManager.shared already captured the credentials
passed to ModelManager.downloadModel/loadModel, and its prediction API takes
no per-call credentials. Passing them is harmless and keeps calling code uniform.
Returns the OCR result via OnDeviceOCRManagerListener.onOnDeviceOCRResult.
Example:
// First load the model via ModelManager
await modelManager.loadModel(
module: OCRModule(modelClass: ModelClass.shippingLabel, modelSize: ModelSize.large),
apiKey: apiKey,
);
// Then make prediction with explicit module
await ocrManager.makePredictionWithModule(
modelClass: ModelClass.shippingLabel,
modelSize: ModelSize.large,
byteArrayImage: imageBytes,
barcodes: detectedBarcodes,
apiKey: apiKey,
);
Implementation
Future<void> makePredictionWithModule({
required ModelClass modelClass,
required ModelSize modelSize,
required Uint8List byteArrayImage,
required List<ScannedCodeResult> barcodes,
String? apiKey,
String? token,
ShippingLabelOptions? shippingLabelOptions,
}) async {
await _channel.invokeMethod('makePredictionWithModule', {
'modelClass': switch (modelClass) {
ModelClass.shippingLabel => 1,
ModelClass.billOfLading => 2,
ModelClass.itemLabel => 3,
ModelClass.documentClassification => 4,
},
'modelSize': switch (modelSize) {
ModelSize.nano => 1,
ModelSize.micro => 2,
ModelSize.small => 3,
ModelSize.medium => 4,
ModelSize.large => 5,
ModelSize.xlarge => 6,
},
'byteArrayImage': byteArrayImage,
'barcodes': barcodes.map((item) => item.toMap()).toList(),
'apiKey': apiKey,
'token': token,
'shippingLabelOptions': shippingLabelOptions?.toMap(),
});
}