view.dart 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import 'package:camera/camera.dart';
  2. import 'package:fis_jsonrpc/rpc.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:get/get.dart';
  5. import 'package:vitalapp/components/appbar.dart';
  6. import 'index.dart';
  7. import 'widgets/widgets.dart';
  8. class FacialRecognitionPage extends GetView<FacialRecognitionController> {
  9. const FacialRecognitionPage({
  10. Key? key,
  11. required this.mode,
  12. this.patientInfo,
  13. }) : super(key: key);
  14. final FacialRecognitionMode mode;
  15. final PatientDTO? patientInfo;
  16. @override
  17. Widget build(BuildContext context) {
  18. return GetBuilder<FacialRecognitionController>(
  19. init: FacialRecognitionController(
  20. mode: mode,
  21. patientInfo: patientInfo,
  22. ),
  23. builder: (_) {
  24. return Scaffold(
  25. appBar: VAppBar(
  26. titleText:
  27. mode == FacialRecognitionMode.faceInput ? "人像采集" : "人脸识别",
  28. ),
  29. body: SafeArea(
  30. child: Obx(
  31. () => controller.state.isCameraReady
  32. ? _buildCameraArea()
  33. : const Center(
  34. child: CircularProgressIndicator(
  35. valueColor: AlwaysStoppedAnimation<Color>(Colors.blue),
  36. ),
  37. ),
  38. ),
  39. ),
  40. );
  41. },
  42. );
  43. }
  44. Widget _buildCameraArea() {
  45. return Row(
  46. children: [
  47. Expanded(
  48. child: ClipRRect(
  49. child: LayoutBuilder(builder: (context, constraints) {
  50. return Stack(
  51. children: <Widget>[
  52. OverflowBox(
  53. maxHeight: constraints.maxHeight,
  54. maxWidth: 2000,
  55. child: Container(
  56. color: Colors.black,
  57. child: Center(
  58. child: _cameraPreviewWidget(),
  59. ),
  60. ),
  61. ),
  62. const Center(child: CameraForFace()),
  63. Align(
  64. alignment: Alignment.bottomRight,
  65. child: _switchCameraLens(),
  66. ),
  67. ],
  68. );
  69. }),
  70. ),
  71. ),
  72. ],
  73. );
  74. }
  75. /// 相机预览
  76. Widget _cameraPreviewWidget() {
  77. final CameraController? cameraController = controller.kCameraController;
  78. if (cameraController == null || !cameraController.value.isInitialized) {
  79. /// 旋转loading
  80. return const Center(
  81. child: CircularProgressIndicator(),
  82. );
  83. } else {
  84. return Listener(
  85. onPointerDown: (_) => controller.pointers++,
  86. onPointerUp: (_) => controller.pointers--,
  87. child: CameraPreview(
  88. cameraController,
  89. child: LayoutBuilder(
  90. builder: (BuildContext context, BoxConstraints constraints) {
  91. return GestureDetector(
  92. behavior: HitTestBehavior.opaque,
  93. onScaleStart: controller.handleScaleStart,
  94. onScaleUpdate: controller.handleScaleUpdate,
  95. onTapDown: (TapDownDetails details) =>
  96. controller.onViewFinderTap(details, constraints),
  97. child: const FaceBoundingBox(),
  98. );
  99. },
  100. ),
  101. ),
  102. );
  103. }
  104. }
  105. Widget _switchCameraLens() {
  106. return Obx(() {
  107. if (controller.state.isRunningFaceRecognition) {
  108. return Container();
  109. }
  110. return GestureDetector(
  111. onTap: () {
  112. if (controller.state.isUsingFrontCamera) {
  113. controller.openBackCamera();
  114. } else {
  115. controller.openFrontCamera();
  116. }
  117. },
  118. child: Container(
  119. padding: const EdgeInsets.all(10.0),
  120. // padding: const EdgeInsets.all(10),
  121. decoration: BoxDecoration(
  122. color: Colors.black.withOpacity(0.2),
  123. borderRadius: BorderRadius.circular(10),
  124. ),
  125. margin: const EdgeInsets.symmetric(horizontal: 40.0, vertical: 20.0),
  126. child: Column(
  127. mainAxisSize: MainAxisSize.min,
  128. children: [
  129. const Icon(
  130. Icons.camera_rear,
  131. size: 30,
  132. color: Colors.white,
  133. ),
  134. const SizedBox(height: 10),
  135. Text(
  136. controller.state.isUsingFrontCamera ? "切换为后置" : "切换为前置",
  137. style: const TextStyle(color: Colors.white),
  138. )
  139. ],
  140. ),
  141. ),
  142. );
  143. });
  144. }
  145. }