31 lines
787 B
Dart
31 lines
787 B
Dart
|
|
import 'dart:io';
|
||
|
|
|
||
|
|
import 'package:flutter/services.dart';
|
||
|
|
|
||
|
|
import 'ocr_backend.dart';
|
||
|
|
|
||
|
|
/// Platform OCR backend. Uses the Windows built-in OCR engine exposed through
|
||
|
|
/// the native `badnote/ocr` MethodChannel.
|
||
|
|
class NativeOcrBackend implements OcrBackend {
|
||
|
|
static const _channel = MethodChannel('badnote/ocr');
|
||
|
|
|
||
|
|
@override
|
||
|
|
String get name => 'native';
|
||
|
|
|
||
|
|
@override
|
||
|
|
Future<bool> isAvailable() async => Platform.isWindows;
|
||
|
|
|
||
|
|
@override
|
||
|
|
Future<String?> recognize(Uint8List pngBytes) async {
|
||
|
|
if (!Platform.isWindows) return null;
|
||
|
|
try {
|
||
|
|
final result = await _channel.invokeMethod<String>('recognize', pngBytes);
|
||
|
|
final text = result?.trim();
|
||
|
|
if (text == null || text.isEmpty) return null;
|
||
|
|
return text;
|
||
|
|
} catch (_) {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|