controller_plus.dart 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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/components/alert_dialog.dart';
  6. import 'package:vitalapp/pages/home/controller.dart';
  7. import 'package:vitalapp/rpc.dart';
  8. import 'package:vitalapp/store/store.dart';
  9. import 'package:fis_common/extensions/string.dart';
  10. import 'package:fis_common/logger/logger.dart';
  11. import 'controller.dart';
  12. import 'facial_recognition_capturer.dart';
  13. class FacialRecognitionControllerPlus extends FacialRecognitionController {
  14. FacialRecognitionControllerPlus({
  15. required super.mode,
  16. super.patientInfo,
  17. this.isVital,
  18. });
  19. FacialRecognitionCapturer? facialRecognitionCapturer;
  20. final bool? isVital;
  21. @override
  22. void doFacialRecognition() async {
  23. facialRecognitionCapturer = FacialRecognitionCapturer();
  24. final capturer = facialRecognitionCapturer!;
  25. capturer.frameCachedEvent.addListener((sender, e) {
  26. if (e.isNotEmpty) {
  27. // 更新预览图像
  28. state.processingImageLocalPath = e;
  29. }
  30. });
  31. capturer.successEvent.addListener(_onCaptureSuccess);
  32. capturer.failEvent.addListener(_onCaptureFail);
  33. capturer.timeoutEvent.addListener(_onCaptureTimeout);
  34. await capturer.init(kCameraController, faceDetector);
  35. await capturer.run();
  36. }
  37. @override
  38. void doCancelCapture() {
  39. facialRecognitionCapturer?.stop();
  40. facialRecognitionCapturer = null;
  41. super.doCancelCapture();
  42. }
  43. @override
  44. void dispose() {
  45. // TODO: implement dispose
  46. super.dispose();
  47. }
  48. void _onCaptureSuccess(_, String e) async {
  49. try {
  50. // url让RPC分析
  51. PatientBaseDTO result =
  52. await rpc.vitalPatient.getPatientBaseByFaceImageAsync(
  53. GetPatientBaseByFaceImageRequest(
  54. token: Store.user.token,
  55. image: e,
  56. ),
  57. );
  58. final type = result.faceScanErrorType;
  59. if (type == FaceScanErrorTypeEnum.Success) {
  60. doCancelCapture();
  61. finishFaceDetection(result);
  62. } else if (type == FaceScanErrorTypeEnum.NoCreated) {
  63. doCancelCapture();
  64. await Get.dialog(
  65. VAlertDialog(
  66. title: "提示",
  67. width: 500,
  68. showCancel: true,
  69. content: Container(
  70. height: 32,
  71. padding: const EdgeInsets.symmetric(horizontal: 24),
  72. alignment: Alignment.center,
  73. child: const Text(
  74. "未检测到当前人脸匹配的身份证,请先录入身份证信息",
  75. style: TextStyle(fontSize: 20),
  76. ),
  77. ),
  78. onConfirm: () {
  79. Get.back();
  80. Get.find<HomeController>().switchNavByName(
  81. "/patient/create",
  82. {"headImage": e, "isVital": isVital},
  83. );
  84. },
  85. onCanceled: () {
  86. Get.back();
  87. },
  88. cancelLabel: "取消",
  89. confirmLabel: "去录入",
  90. ),
  91. barrierDismissible: false,
  92. barrierColor: Colors.black.withOpacity(.4),
  93. );
  94. Get.back();
  95. } else {
  96. if (result.errorMessage.isNotNullOrEmpty) {
  97. logger.e(result.errorMessage!);
  98. }
  99. PromptBox.toast('无法识别面部信息,请确保面部清晰可见');
  100. _continueCapture();
  101. }
  102. } catch (e) {
  103. logger.e("FacialRecognitionController onCaptureSuccess handle error.", e);
  104. PromptBox.toast('无法识别面部信息');
  105. _continueCapture();
  106. } finally {
  107. state.processingImageLocalPath = '';
  108. }
  109. }
  110. void _onCaptureFail(_, e) async {
  111. facialRecognitionCapturer?.callPause();
  112. // 错误提示
  113. PromptBox.toast(e.message);
  114. // 给错误提示一点显示时间
  115. await Future.delayed(const Duration(milliseconds: 1000));
  116. facialRecognitionCapturer?.callContinue();
  117. }
  118. void _onCaptureTimeout(_, e) {
  119. // 结束
  120. PromptBox.toast('人脸识别失败,请先录入人脸信息或更新人脸信息');
  121. doCancelCapture();
  122. }
  123. /// 发送继续采集信号
  124. void _continueCapture() {
  125. facialRecognitionCapturer?.callContinue();
  126. }
  127. }