| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- 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<ClassificationEvent?>? _classificationStream;
-
- @override
- Stream<ClassificationEvent?> startClassificationStream({
- List<PhotoImageClassifyType> types = PhotoImageClassifyType.values,
- }) {
- _classificationStream = eventChannel
- .receiveBroadcastStream()
- .map<ClassificationEvent?>((dynamic event) {
- if (event is Map) {
- return _convertToClassificationEvent(event.cast<dynamic, dynamic>());
- } else {
- throw PlatformException(
- code: 'CLASSIFICATION_ERROR',
- message: 'Unknown error occurred in classification process',
- details: event,
- );
- }
- });
- methodChannel.invokeMethod<bool>(
- '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 = <String, dynamic>{};
-
- if (batchSize != null) {
- args['batchSize'] = batchSize;
- }
-
- if (maxConcurrentProcessing != null) {
- args['maxConcurrentProcessing'] = maxConcurrentProcessing;
- }
-
- if (similarityThreshold != null) {
- args['similarityThreshold'] = similarityThreshold;
- }
-
- await methodChannel.invokeMethod<bool>('configureClassifier', args);
- } on PlatformException catch (e) {
- throw Exception('配置分类器失败: ${e.message}');
- }
- }
-
- @override
- Future resetClassifier() async {
- try {
- // 重置流
- _classificationStream = null;
-
- final result = await methodChannel.invokeMethod<bool>('resetClassifier');
- return result ?? false;
- } on PlatformException catch (e) {
- throw Exception('重置分类器失败: ${e.message}');
- }
- }
-
-
- @override
- Future checkAssetsLoaded() async {
- try {
- final result = await methodChannel.invokeMethod<bool>('checkAssetsLoaded');
- return result ?? false;
- } on PlatformException catch (e) {
- throw Exception('检查资源加载状态失败: ${e.message}');
- }
- }
-
- // 将原生事件转换为ClassificationEvent对象
- ClassificationEvent? _convertToClassificationEvent(Map<dynamic, dynamic> eventData) {
- try {
- // 修改这里的类型转换
- final progressData = eventData['progress'] as Map<dynamic, dynamic>?;
- final resultData = eventData['result'] as Map<dynamic, dynamic>?;
-
- ClassificationProgress? progress;
- ClassificationResult? result;
- if (progressData != null) {
- // 转换为 Map<String, dynamic>
- final progressMap = Map<String, dynamic>.from(progressData);
- progress = ClassificationProgress.fromJson(progressMap);
- }
- if (resultData != null) {
- // 转换为 Map<String, dynamic>
- final resultMap = Map<String, dynamic>.from(resultData);
- result = ClassificationResult.fromJson(resultMap);
- }
- return ClassificationEvent(
- progress: progress,
- result: result,
- );
- } catch (e, stackTrace) {
- print('转换分类事件失败: $e');
- print('堆栈跟踪: $stackTrace');
- return null;
- }
- }
- }
|