controller_plus.dart 4.1 KB

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