controller.dart 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. import 'package:camera/camera.dart';
  2. import 'package:flutter/foundation.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter/services.dart';
  5. import 'package:get/get.dart';
  6. import 'package:image_gallery_saver/image_gallery_saver.dart';
  7. import 'package:vitalapp/architecture/utils/prompt_box.dart';
  8. import 'package:vitalapp/managers/interfaces/cache.dart';
  9. import 'index.dart';
  10. class FacialRecognitionController extends GetxController
  11. with WidgetsBindingObserver {
  12. FacialRecognitionController();
  13. final state = FacialRecognitionState();
  14. List<CameraDescription> _cameras = <CameraDescription>[];
  15. List<CameraDescription> get cameras => _cameras;
  16. CameraController? kCameraController;
  17. double _minAvailableExposureOffset = 0.0;
  18. double _maxAvailableExposureOffset = 0.0;
  19. double _minAvailableZoom = 1.0;
  20. double _maxAvailableZoom = 1.0;
  21. // CameraController? controller;
  22. XFile? imageFile;
  23. XFile? videoFile;
  24. // VideoPlayerController? videoController;
  25. VoidCallback? videoPlayerListener;
  26. bool enableAudio = true;
  27. double _currentScale = 1.0;
  28. double _baseScale = 1.0;
  29. // 屏幕上手指数量
  30. int pointers = 0;
  31. // 当前身份证信息
  32. IdCardInfoModel idCardInfo = IdCardInfoModel();
  33. /// 开始缩放
  34. void handleScaleStart(ScaleStartDetails details) {
  35. _baseScale = _currentScale;
  36. }
  37. /// 缩放更新
  38. Future<void> handleScaleUpdate(ScaleUpdateDetails details) async {
  39. // When there are not exactly two fingers on screen don't scale
  40. if (kCameraController == null || pointers != 2) {
  41. return;
  42. }
  43. _currentScale = (_baseScale * details.scale)
  44. .clamp(_minAvailableZoom, _maxAvailableZoom);
  45. await kCameraController!.setZoomLevel(_currentScale);
  46. }
  47. /// 修改对焦点 TODO
  48. void onViewFinderTap(TapDownDetails details, BoxConstraints constraints) {
  49. if (kCameraController == null) {
  50. return;
  51. }
  52. final CameraController cameraController = kCameraController!;
  53. final Offset offset = Offset(
  54. details.localPosition.dx / constraints.maxWidth,
  55. details.localPosition.dy / constraints.maxHeight,
  56. );
  57. cameraController.setExposurePoint(offset);
  58. cameraController.setFocusPoint(offset);
  59. }
  60. /// 初始化相机
  61. Future<void> initAvailableCameras() async {
  62. try {
  63. _cameras = await availableCameras();
  64. if (_cameras.isNotEmpty) {
  65. // state.isCameraReady = true;
  66. }
  67. print("cameras: ${_cameras.length}");
  68. } on CameraException catch (e) {
  69. print("cameras: ${e.code} ${e.description}");
  70. }
  71. }
  72. /// 启动指定相机
  73. Future<void> onNewCameraSelected(CameraDescription cameraDescription) async {
  74. final CameraController? oldController = kCameraController;
  75. if (oldController != null) {
  76. // `kCameraController` needs to be set to null before getting disposed,
  77. // to avoid a race condition when we use the kCameraController that is being
  78. // disposed. This happens when camera permission dialog shows up,
  79. // which triggers `didChangeAppLifecycleState`, which disposes and
  80. // re-creates the kCameraController.
  81. kCameraController = null;
  82. await oldController.dispose();
  83. }
  84. final CameraController cameraController = CameraController(
  85. cameraDescription,
  86. ResolutionPreset.max,
  87. enableAudio: false,
  88. imageFormatGroup: ImageFormatGroup.jpeg,
  89. );
  90. kCameraController = cameraController;
  91. // If the kCameraController is updated then update the UI.
  92. cameraController.addListener(() {
  93. if (cameraController.value.hasError) {
  94. PromptBox.toast(
  95. "Camera error ${cameraController.value.errorDescription}");
  96. }
  97. });
  98. try {
  99. await cameraController.initialize();
  100. await Future.wait(<Future<Object?>>[
  101. // The exposure mode is currently not supported on the web.
  102. ...!kIsWeb
  103. ? <Future<Object?>>[
  104. cameraController.getMinExposureOffset().then(
  105. (double value) => _minAvailableExposureOffset = value),
  106. cameraController
  107. .getMaxExposureOffset()
  108. .then((double value) => _maxAvailableExposureOffset = value)
  109. ]
  110. : <Future<Object?>>[],
  111. cameraController
  112. .getMaxZoomLevel()
  113. .then((double value) => _maxAvailableZoom = value),
  114. cameraController
  115. .getMinZoomLevel()
  116. .then((double value) => _minAvailableZoom = value),
  117. ]);
  118. } on CameraException catch (e) {
  119. switch (e.code) {
  120. case 'CameraAccessDenied':
  121. PromptBox.toast('You have denied camera access.');
  122. break;
  123. case 'CameraAccessDeniedWithoutPrompt':
  124. // iOS only
  125. PromptBox.toast('Please go to Settings app to enable camera access.');
  126. break;
  127. case 'CameraAccessRestricted':
  128. // iOS only
  129. PromptBox.toast('Camera access is restricted.');
  130. break;
  131. case 'AudioAccessDenied':
  132. PromptBox.toast('You have denied audio access.');
  133. break;
  134. case 'AudioAccessDeniedWithoutPrompt':
  135. // iOS only
  136. PromptBox.toast('Please go to Settings app to enable audio access.');
  137. break;
  138. case 'AudioAccessRestricted':
  139. // iOS only
  140. PromptBox.toast('Audio access is restricted.');
  141. break;
  142. default:
  143. PromptBox.toast('Error: ${e.code}\n${e.description}');
  144. break;
  145. }
  146. }
  147. }
  148. /// 遍历当前相机列表并启动后置相机
  149. void openBackCamera() async {
  150. if (_cameras.isEmpty) {
  151. PromptBox.toast('Error: No cameras found.');
  152. } else {
  153. for (CameraDescription cameraDescription in _cameras) {
  154. if (cameraDescription.lensDirection == CameraLensDirection.back) {
  155. await onNewCameraSelected(cameraDescription);
  156. lockCaptureOrientation();
  157. update();
  158. state.isCameraReady = true;
  159. break;
  160. }
  161. }
  162. }
  163. }
  164. /// 相机锁定旋转
  165. Future<void> lockCaptureOrientation() async {
  166. final CameraController? cameraController = kCameraController;
  167. if (cameraController == null || !cameraController.value.isInitialized) {
  168. PromptBox.toast('Error: select a camera first.');
  169. return;
  170. }
  171. if (!cameraController.value.isCaptureOrientationLocked) {
  172. try {
  173. await cameraController
  174. .lockCaptureOrientation(DeviceOrientation.landscapeLeft);
  175. } on CameraException catch (e) {
  176. PromptBox.toast('Error: ${e.code}\n${e.description}');
  177. }
  178. } else {
  179. PromptBox.toast('Rotation lock is already enabled.');
  180. }
  181. }
  182. /// 执行一次拍摄
  183. Future<XFile?> takePicture() async {
  184. final CameraController? cameraController = kCameraController;
  185. if (cameraController == null || !cameraController.value.isInitialized) {
  186. PromptBox.toast('Error: select a camera first.');
  187. return null;
  188. }
  189. if (cameraController.value.isTakingPicture) {
  190. // A capture is already pending, do nothing.
  191. return null;
  192. }
  193. try {
  194. final XFile file = await cameraController.takePicture();
  195. saveImageToGallery(file);
  196. return file;
  197. } on CameraException catch (e) {
  198. PromptBox.toast('Error: ${e.code}\n${e.description}');
  199. return null;
  200. }
  201. }
  202. /// 测试图像文件缓存,print 遍历输出
  203. void debugShowCache() async {
  204. final cacheManager = Get.find<ICacheManager>();
  205. double cacheSize = await cacheManager.getCacheSize();
  206. print('cacheSize = $cacheSize : ${formatSize(cacheSize)}');
  207. double imageCacheSize = await cacheManager.getImageCacheSize();
  208. print('imageCacheSize = $imageCacheSize : ${formatSize(imageCacheSize)}');
  209. }
  210. static String formatSize(double value) {
  211. List<String> unitArr = ['B', 'K', 'M', 'G'];
  212. int index = 0;
  213. while (value > 1024) {
  214. index++;
  215. value = value / 1024;
  216. }
  217. String size = value.toStringAsFixed(2);
  218. return size + unitArr[index];
  219. }
  220. void saveImageToGallery(XFile image) async {
  221. // 获取图像的字节数据
  222. Uint8List bytes = await image.readAsBytes();
  223. // 将图像保存到相册
  224. await ImageGallerySaver.saveImage(bytes);
  225. print('图像已保存到相册!');
  226. }
  227. /// 发生拍摄身份证事件
  228. void onCaptureIdCardButtonPressed() {
  229. takePicture().then((XFile? file) {
  230. // imageFile = file;
  231. if (file != null) {
  232. print('Picture saved to ${file.path}');
  233. /// TODO 上传给server,获取返回值信息
  234. if (true) {
  235. PromptBox.toast('身份证识别成功');
  236. idCardInfo.localCardImagePath = file.path;
  237. idCardInfo.idCardName = '金阳';
  238. idCardInfo.idCardGender = '女';
  239. idCardInfo.idCardNation = '汉';
  240. idCardInfo.idCardBirthDate = '1980年10月27日';
  241. idCardInfo.idCardAddress = '北京市西城区复兴门外大街999号院11号楼3单元502室';
  242. idCardInfo.idCardNumber = '110101198010270000';
  243. state.isShowIdCardInfoSwitch = true;
  244. state.isIdCardInfoShow = true;
  245. state.isInFaceRecognition = true;
  246. }
  247. debugShowCache();
  248. }
  249. });
  250. }
  251. /// 发生结束录制视频事件
  252. void onStopButtonPressed() {
  253. stopVideoRecording().then((XFile? file) {
  254. if (file != null) {
  255. PromptBox.toast('Video recorded to ${file.path}');
  256. // videoFile = file;
  257. // _startVideoPlayer();
  258. }
  259. update();
  260. });
  261. }
  262. /// 发生开始录制视频事件
  263. void onVideoRecordButtonPressed() {
  264. startVideoRecording().then((_) {
  265. update();
  266. });
  267. }
  268. /// 暂停录制视频
  269. void onPauseButtonPressed() {
  270. pauseVideoRecording().then((_) {
  271. update();
  272. });
  273. }
  274. /// 恢复视频录制
  275. void onResumeButtonPressed() {
  276. resumeVideoRecording().then((_) {
  277. update();
  278. });
  279. }
  280. /// 开始录制视频
  281. Future<void> startVideoRecording() async {
  282. final CameraController? cameraController = kCameraController;
  283. if (cameraController == null || !cameraController.value.isInitialized) {
  284. PromptBox.toast('Error: select a camera first.');
  285. return;
  286. }
  287. if (cameraController.value.isRecordingVideo) {
  288. // A recording is already started, do nothing.
  289. return;
  290. }
  291. try {
  292. await cameraController.startVideoRecording();
  293. } on CameraException catch (e) {
  294. PromptBox.toast('Error: ${e.code}\n${e.description}');
  295. return;
  296. }
  297. }
  298. /// 停止录制视频
  299. Future<XFile?> stopVideoRecording() async {
  300. final CameraController? cameraController = kCameraController;
  301. if (cameraController == null || !cameraController.value.isRecordingVideo) {
  302. return null;
  303. }
  304. try {
  305. return cameraController.stopVideoRecording();
  306. } on CameraException catch (e) {
  307. PromptBox.toast('Error: ${e.code}\n${e.description}');
  308. return null;
  309. }
  310. }
  311. /// 暂停录制视频
  312. Future<void> pauseVideoRecording() async {
  313. final CameraController? cameraController = kCameraController;
  314. if (cameraController == null || !cameraController.value.isRecordingVideo) {
  315. return;
  316. }
  317. try {
  318. await cameraController.pauseVideoRecording();
  319. } on CameraException catch (e) {
  320. PromptBox.toast('Error: ${e.code}\n${e.description}');
  321. rethrow;
  322. }
  323. }
  324. /// 恢复视频录制
  325. Future<void> resumeVideoRecording() async {
  326. final CameraController? cameraController = kCameraController;
  327. if (cameraController == null || !cameraController.value.isRecordingVideo) {
  328. return;
  329. }
  330. try {
  331. await cameraController.resumeVideoRecording();
  332. } on CameraException catch (e) {
  333. PromptBox.toast('Error: ${e.code}\n${e.description}');
  334. rethrow;
  335. }
  336. }
  337. /// 在 widget 内存中分配后立即调用。
  338. @override
  339. void onInit() async {
  340. // await initCamera();
  341. super.onInit();
  342. }
  343. /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  344. @override
  345. void onReady() async {
  346. super.onReady();
  347. await initAvailableCameras();
  348. openBackCamera();
  349. }
  350. /// 在 [onDelete] 方法之前调用。
  351. @override
  352. void onClose() {
  353. super.onClose();
  354. final cacheManager = Get.find<ICacheManager>();
  355. cacheManager.clearApplicationImageCache();
  356. }
  357. /// dispose 释放内存
  358. @override
  359. void dispose() {
  360. super.dispose();
  361. }
  362. @override
  363. void didChangeAppLifecycleState(AppLifecycleState state) {
  364. // FIXME 未执行
  365. super.didChangeAppLifecycleState(state);
  366. print('state = $state');
  367. final CameraController? cameraController = kCameraController;
  368. // App state changed before we got the chance to initialize.
  369. if (cameraController == null || !cameraController.value.isInitialized) {
  370. return;
  371. }
  372. if (state == AppLifecycleState.inactive) {
  373. cameraController.dispose();
  374. } else if (state == AppLifecycleState.resumed) {
  375. onNewCameraSelected(cameraController.description);
  376. }
  377. }
  378. }