resolveOcrEngine function

Future<OcrEngine> resolveOcrEngine({
  1. OcrHostApi? hostApi,
})

Picks the OcrEngine appropriate for the current device: on iOS, always AppleVisionOcrEngine; on Android, GoogleOcrEngine or HuaweiOcrEngine depending on which mobile-services stack is installed.

Throws OneIdConfigurationException on an Android device with neither GMS nor HMS installed.

Implementation

Future<OcrEngine> resolveOcrEngine({OcrHostApi? hostApi}) async {
  final api = hostApi ?? OcrHostApi();

  if (Platform.isIOS) {
    return AppleVisionOcrEngine(hostApi: api);
  }

  final type = DeviceServicesType.parse(
    await guardPlatformCall(api.getDeviceServicesType),
  );

  return switch (type) {
    DeviceServicesType.gms => GoogleOcrEngine(hostApi: api),
    DeviceServicesType.hms => HuaweiOcrEngine(hostApi: api),
    DeviceServicesType.none => throw const OneIdConfigurationException(
        'No supported mobile services (Google Play Services or Huawei '
        'Mobile Services) found on this device — OCR is unavailable.',
      ),
  };
}