photo_classifier_method_channel.dart 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import 'package:flutter/foundation.dart';
  2. import 'package:flutter/services.dart';
  3. import 'photo_classifier_platform_interface.dart';
  4. import 'models.dart';
  5. class MethodChannelPhotoClassifier extends PhotoClassifierPlatform {
  6. /// The method channel used to interact with the native platform.
  7. @visibleForTesting
  8. final methodChannel = MethodChannel(PhotoClassifierPlatform.methodChannelName);
  9. /// The event channel used to receive progress updates.
  10. @visibleForTesting
  11. final eventChannel = EventChannel(PhotoClassifierPlatform.eventChannelName);
  12. /// Stream of classification progress events
  13. Stream<ClassificationEvent?>? _classificationStream;
  14. @override
  15. Stream<ClassificationEvent?> startClassificationStream({
  16. List<PhotoImageClassifyType> types = PhotoImageClassifyType.values,
  17. }) {
  18. _classificationStream = eventChannel
  19. .receiveBroadcastStream()
  20. .map<ClassificationEvent?>((dynamic event) {
  21. if (event is Map) {
  22. return _convertToClassificationEvent(event.cast<dynamic, dynamic>());
  23. } else {
  24. throw PlatformException(
  25. code: 'CLASSIFICATION_ERROR',
  26. message: 'Unknown error occurred in classification process',
  27. details: event,
  28. );
  29. }
  30. });
  31. methodChannel.invokeMethod<bool>(
  32. 'startClassification',
  33. {'types': types.map((e) => e.name).toList()},
  34. ).catchError((error) {
  35. throw PlatformException(
  36. code: 'START_CLASSIFICATION_FAILED',
  37. message: '启动分类失败: ${error is PlatformException ? error.message : error}',
  38. details: error,
  39. );
  40. });
  41. return _classificationStream!;
  42. }
  43. @override
  44. Future configureClassifier({
  45. int? batchSize,
  46. int? maxConcurrentProcessing,
  47. double? similarityThreshold,
  48. }) async {
  49. try {
  50. final args = <String, dynamic>{};
  51. if (batchSize != null) {
  52. args['batchSize'] = batchSize;
  53. }
  54. if (maxConcurrentProcessing != null) {
  55. args['maxConcurrentProcessing'] = maxConcurrentProcessing;
  56. }
  57. if (similarityThreshold != null) {
  58. args['similarityThreshold'] = similarityThreshold;
  59. }
  60. await methodChannel.invokeMethod<bool>('configureClassifier', args);
  61. } on PlatformException catch (e) {
  62. throw Exception('配置分类器失败: ${e.message}');
  63. }
  64. }
  65. @override
  66. Future resetClassifier() async {
  67. try {
  68. // 重置流
  69. _classificationStream = null;
  70. final result = await methodChannel.invokeMethod<bool>('resetClassifier');
  71. return result ?? false;
  72. } on PlatformException catch (e) {
  73. throw Exception('重置分类器失败: ${e.message}');
  74. }
  75. }
  76. @override
  77. Future checkAssetsLoaded() async {
  78. try {
  79. final result = await methodChannel.invokeMethod<bool>('checkAssetsLoaded');
  80. return result ?? false;
  81. } on PlatformException catch (e) {
  82. throw Exception('检查资源加载状态失败: ${e.message}');
  83. }
  84. }
  85. // 将原生事件转换为ClassificationEvent对象
  86. ClassificationEvent? _convertToClassificationEvent(Map<dynamic, dynamic> eventData) {
  87. try {
  88. // 修改这里的类型转换
  89. final progressData = eventData['progress'] as Map<dynamic, dynamic>?;
  90. final resultData = eventData['result'] as Map<dynamic, dynamic>?;
  91. ClassificationProgress? progress;
  92. ClassificationResult? result;
  93. if (progressData != null) {
  94. // 转换为 Map<String, dynamic>
  95. final progressMap = Map<String, dynamic>.from(progressData);
  96. progress = ClassificationProgress.fromJson(progressMap);
  97. }
  98. if (resultData != null) {
  99. // 转换为 Map<String, dynamic>
  100. final resultMap = Map<String, dynamic>.from(resultData);
  101. result = ClassificationResult.fromJson(resultMap);
  102. }
  103. return ClassificationEvent(
  104. progress: progress,
  105. result: result,
  106. );
  107. } catch (e, stackTrace) {
  108. print('转换分类事件失败: $e');
  109. print('堆栈跟踪: $stackTrace');
  110. return null;
  111. }
  112. }
  113. }