controller_plus.dart 3.7 KB

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