import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; import 'photo_classifier_platform_interface.dart'; import 'models.dart'; class MethodChannelPhotoClassifier extends PhotoClassifierPlatform { /// The method channel used to interact with the native platform. @visibleForTesting final methodChannel = MethodChannel(PhotoClassifierPlatform.methodChannelName); /// The event channel used to receive progress updates. @visibleForTesting final eventChannel = EventChannel(PhotoClassifierPlatform.eventChannelName); /// Stream of classification progress events Stream? _classificationStream; @override Stream startClassificationStream({ List types = PhotoImageClassifyType.values, }) { _classificationStream = eventChannel .receiveBroadcastStream() .map((dynamic event) { if (event is Map) { return _convertToClassificationEvent(event.cast()); } else { throw PlatformException( code: 'CLASSIFICATION_ERROR', message: 'Unknown error occurred in classification process', details: event, ); } }); methodChannel.invokeMethod( 'startClassification', {'types': types.map((e) => e.name).toList()}, ).catchError((error) { throw PlatformException( code: 'START_CLASSIFICATION_FAILED', message: '启动分类失败: ${error is PlatformException ? error.message : error}', details: error, ); }); return _classificationStream!; } @override Future configureClassifier({ int? batchSize, int? maxConcurrentProcessing, double? similarityThreshold, }) async { try { final args = {}; if (batchSize != null) { args['batchSize'] = batchSize; } if (maxConcurrentProcessing != null) { args['maxConcurrentProcessing'] = maxConcurrentProcessing; } if (similarityThreshold != null) { args['similarityThreshold'] = similarityThreshold; } await methodChannel.invokeMethod('configureClassifier', args); } on PlatformException catch (e) { throw Exception('配置分类器失败: ${e.message}'); } } @override Future resetClassifier() async { try { // 重置流 _classificationStream = null; final result = await methodChannel.invokeMethod('resetClassifier'); return result ?? false; } on PlatformException catch (e) { throw Exception('重置分类器失败: ${e.message}'); } } @override Future checkAssetsLoaded() async { try { final result = await methodChannel.invokeMethod('checkAssetsLoaded'); return result ?? false; } on PlatformException catch (e) { throw Exception('检查资源加载状态失败: ${e.message}'); } } // 将原生事件转换为ClassificationEvent对象 ClassificationEvent? _convertToClassificationEvent(Map eventData) { try { // 修改这里的类型转换 final progressData = eventData['progress'] as Map?; final resultData = eventData['result'] as Map?; ClassificationProgress? progress; ClassificationResult? result; if (progressData != null) { // 转换为 Map final progressMap = Map.from(progressData); progress = ClassificationProgress.fromJson(progressMap); } if (resultData != null) { // 转换为 Map final resultMap = Map.from(resultData); result = ClassificationResult.fromJson(resultMap); } return ClassificationEvent( progress: progress, result: result, ); } catch (e, stackTrace) { print('转换分类事件失败: $e'); print('堆栈跟踪: $stackTrace'); return null; } } }