controller_plus.dart 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import 'package:fis_jsonrpc/rpc.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:get/get.dart';
  4. import 'package:vitalapp/architecture/utils/prompt_box.dart';
  5. import 'package:vitalapp/rpc.dart';
  6. import 'package:vitalapp/store/store.dart';
  7. import 'package:fis_common/extensions/string.dart';
  8. import 'package:fis_common/logger/logger.dart';
  9. import 'controller.dart';
  10. import 'facial_recognition_capturer.dart';
  11. class FacialRecognitionControllerPlus extends FacialRecognitionController {
  12. FacialRecognitionControllerPlus({required super.mode, super.patientInfo});
  13. FacialRecognitionCapturer? facialRecognitionCapturer;
  14. @override
  15. void doFacialRecognition() async {
  16. facialRecognitionCapturer = FacialRecognitionCapturer();
  17. final capturer = facialRecognitionCapturer!;
  18. capturer.frameCachedEvent.addListener((sender, e) {
  19. if (e.isNotEmpty) {
  20. // 更新预览图像
  21. state.processingImageLocalPath = e;
  22. }
  23. });
  24. capturer.successEvent.addListener(_onCaptureSuccess);
  25. capturer.failEvent.addListener(_onCaptureFail);
  26. capturer.timeoutEvent.addListener(_onCaptureTimeout);
  27. await capturer.init(kCameraController, faceDetector);
  28. await capturer.run();
  29. }
  30. @override
  31. void doCancelCapture() {
  32. facialRecognitionCapturer?.stop();
  33. facialRecognitionCapturer = null;
  34. super.doCancelCapture();
  35. }
  36. @override
  37. void dispose() {
  38. // TODO: implement dispose
  39. super.dispose();
  40. }
  41. void _onCaptureSuccess(_, String e) async {
  42. try {
  43. // url让RPC分析
  44. PatientBaseDTO result = await rpc.patient.getPatientBaseByFaceImageAsync(
  45. GetPatientBaseByFaceImageRequest(
  46. token: Store.user.token,
  47. image: e,
  48. ),
  49. );
  50. final type = result.faceScanErrorType;
  51. if (type == FaceScanErrorTypeEnum.Success) {
  52. doCancelCapture();
  53. finishFaceDetection(result);
  54. } else if (type == FaceScanErrorTypeEnum.NoCreated) {
  55. doCancelCapture();
  56. PromptBox.toast('识别到未采集过的人脸信息');
  57. } else {
  58. if (result.errorMessage.isNotNullOrEmpty) {
  59. logger.e(result.errorMessage!);
  60. }
  61. PromptBox.toast('无法识别面部信息,请确保面部清晰可见');
  62. _continueCapture();
  63. }
  64. } catch (e) {
  65. logger.e("FacialRecognitionController onCaptureSuccess handle error.", e);
  66. PromptBox.toast('无法识别面部信息');
  67. _continueCapture();
  68. } finally {
  69. state.processingImageLocalPath = '';
  70. }
  71. }
  72. void _onCaptureFail(_, e) async {
  73. facialRecognitionCapturer?.callPause();
  74. // 错误提示
  75. PromptBox.toast(e.message);
  76. // 给错误提示一点显示时间
  77. await Future.delayed(const Duration(milliseconds: 1000));
  78. facialRecognitionCapturer?.callContinue();
  79. }
  80. void _onCaptureTimeout(_, e) {
  81. // 结束
  82. PromptBox.toast('人脸识别失败,请先录入人脸信息或更新人脸信息');
  83. doCancelCapture();
  84. }
  85. /// 发送继续采集信号
  86. void _continueCapture() {
  87. facialRecognitionCapturer?.callContinue();
  88. }
  89. }