|
@@ -19,7 +19,9 @@ import 'index.dart';
|
|
|
|
|
|
class FacialRecognitionController extends GetxController
|
|
|
with WidgetsBindingObserver {
|
|
|
- FacialRecognitionController();
|
|
|
+ FacialRecognitionController({required this.mode});
|
|
|
+
|
|
|
+ final FacialRecognitionMode mode;
|
|
|
|
|
|
final state = FacialRecognitionState();
|
|
|
|
|
@@ -342,11 +344,29 @@ class FacialRecognitionController extends GetxController
|
|
|
void onCaptureFaceButtonPressed() {
|
|
|
// runDetectionTimer();
|
|
|
/// 接口测试
|
|
|
- rpcTest2();
|
|
|
+ ///
|
|
|
+ if (mode == FacialRecognitionMode.faceRecognition) {
|
|
|
+ doFacialRecognitionTimes = 0;
|
|
|
+ state.isRunningFaceRecognition = true;
|
|
|
+ doFacialRecognition(); // 人脸识别
|
|
|
+ } else {
|
|
|
+ rpcTest2(); // 人脸录入
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- void rpcTest() async {
|
|
|
+ /// 人脸识别执行次数
|
|
|
+ int doFacialRecognitionTimes = 0;
|
|
|
+ void doFacialRecognition() async {
|
|
|
+ doFacialRecognitionTimes++;
|
|
|
+ if (doFacialRecognitionTimes == 10) {
|
|
|
+ // 尝试十次后宣告失败
|
|
|
+ state.isRunningFaceRecognition = false;
|
|
|
+ PromptBox.toast('人脸识别失败,请稍后重试');
|
|
|
+ return;
|
|
|
+ }
|
|
|
if (kCameraController == null) {
|
|
|
+ state.isRunningFaceRecognition = false;
|
|
|
+ PromptBox.toast('相机启动失败,请稍后重试');
|
|
|
return;
|
|
|
}
|
|
|
final XFile? file = await takePicture();
|
|
@@ -359,52 +379,40 @@ class FacialRecognitionController extends GetxController
|
|
|
|
|
|
if (faceNum == 0) {
|
|
|
PromptBox.toast('请将面部保持在识别框内');
|
|
|
- return;
|
|
|
+ await Future.delayed(const Duration(seconds: 2));
|
|
|
} else if (faceNum > 1) {
|
|
|
PromptBox.toast('请保持只有一张面部在识别范围内');
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- /// TODO 上传图像到云然后传给后端
|
|
|
- final url = await rpc.storage.upload(file);
|
|
|
- print('⭐⭐⭐⭐⭐⭐⭐⭐ url: $url');
|
|
|
- try {
|
|
|
- PatientBaseDTO result =
|
|
|
- await rpc.patient.getPatientBaseByFaceImageAsync(
|
|
|
- GetPatientBaseByFaceImageRequest(
|
|
|
- token: Store.user.token,
|
|
|
- image: url,
|
|
|
- ),
|
|
|
+ await Future.delayed(const Duration(seconds: 2));
|
|
|
+ } else {
|
|
|
+ /// TODO 上传图像到云然后传给后端
|
|
|
+ final url = await rpc.storage.upload(
|
|
|
+ file,
|
|
|
+ fileType: 'png',
|
|
|
);
|
|
|
- print(result);
|
|
|
- if (result.faceScanErrorType == FaceScanErrorTypeEnum.Success) {
|
|
|
- PromptBox.toast('人脸识别成功,身份证号 ${result.cardNo}}');
|
|
|
- finishFaceDetection(result);
|
|
|
- } else {
|
|
|
- // 暂先将 API 错误和 查无此人都归为识别错误
|
|
|
- PromptBox.toast('人脸识别失败,请重试');
|
|
|
+ print('⭐⭐⭐⭐⭐⭐⭐⭐ url: $url');
|
|
|
+ try {
|
|
|
+ PatientBaseDTO result =
|
|
|
+ await rpc.patient.getPatientBaseByFaceImageAsync(
|
|
|
+ GetPatientBaseByFaceImageRequest(
|
|
|
+ token: Store.user.token,
|
|
|
+ image: url,
|
|
|
+ ),
|
|
|
+ );
|
|
|
+ print(result);
|
|
|
+ if (result.faceScanErrorType == FaceScanErrorTypeEnum.Success) {
|
|
|
+ PromptBox.toast('人脸识别成功,身份证号 ${result.cardNo}}');
|
|
|
+ finishFaceDetection(result);
|
|
|
+ } else {
|
|
|
+ // 暂先将 API 错误和 查无此人都归为识别错误
|
|
|
+ PromptBox.toast('人脸识别失败: $doFacialRecognitionTimes');
|
|
|
+ await Future.delayed(const Duration(seconds: 2));
|
|
|
+ }
|
|
|
+ } catch (e) {
|
|
|
+ logger.e("getPatientBaseByFaceImageAsync failed: $e", e);
|
|
|
}
|
|
|
- } catch (e) {
|
|
|
- logger.e("getPatientBaseByFaceImageAsync failed: $e", e);
|
|
|
}
|
|
|
-
|
|
|
- // if (timer.tick == 1 || kFrameImageSize == Size.zero) {
|
|
|
- // Size imageSize = await getImageSize(file);
|
|
|
- // kFrameImageSize = imageSize;
|
|
|
- // }
|
|
|
- // int kTime = DateTime.now().millisecondsSinceEpoch;
|
|
|
- // print('⭐⭐⭐⭐⭐⭐⭐⭐ capture time: ${kTime - lastCaptureTime} ms');
|
|
|
- // lastCaptureTime = kTime;
|
|
|
-
|
|
|
- // /// 记录用时 ms
|
|
|
-
|
|
|
- // int endTime = DateTime.now().millisecondsSinceEpoch;
|
|
|
- // print('⭐⭐⭐⭐⭐⭐⭐⭐ detection time: ${endTime - lastCaptureTime} ms');
|
|
|
- // update(['face_bounding_box']);
|
|
|
- // if (timer.tick >= 10) {
|
|
|
- // finishFaceDetection(); // TODO 接入真实的判断条件
|
|
|
- // }
|
|
|
}
|
|
|
+ doFacialRecognition();
|
|
|
}
|
|
|
|
|
|
/// 人脸录入测试
|
|
@@ -429,12 +437,15 @@ class FacialRecognitionController extends GetxController
|
|
|
}
|
|
|
|
|
|
/// TODO 上传图像到云然后传给后端
|
|
|
- final url = await rpc.storage.upload(file);
|
|
|
+ final url = await rpc.storage.upload(
|
|
|
+ file,
|
|
|
+ fileType: 'png',
|
|
|
+ );
|
|
|
print('⭐⭐⭐⭐⭐⭐⭐⭐ url: $url');
|
|
|
try {
|
|
|
bool result = await rpc.patient.savePatientBaseByFaceImageAsync(
|
|
|
SavePatientBaseByFaceImageRequest(
|
|
|
- cardNo: '320520200006190612',
|
|
|
+ cardNo: '320581199906190615',
|
|
|
token: Store.user.token,
|
|
|
image: url,
|
|
|
),
|
|
@@ -818,3 +829,12 @@ class FaceRecognitionResult {
|
|
|
required this.address,
|
|
|
});
|
|
|
}
|
|
|
+
|
|
|
+/// 运行模式
|
|
|
+enum FacialRecognitionMode {
|
|
|
+ /// 人脸识别(用于登录)
|
|
|
+ faceRecognition,
|
|
|
+
|
|
|
+ /// 人脸录入
|
|
|
+ faceInput,
|
|
|
+}
|