controller.dart 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'dart:ui';
  4. import 'package:flutter/foundation.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:get/get.dart';
  7. import 'package:vitalapp/architecture/utils/prompt_box.dart';
  8. import 'dart:ui' as ui;
  9. import 'index.dart';
  10. class EcgViewController extends GetxController {
  11. EcgViewController();
  12. /// 每秒的数据量
  13. static int get dataPerSecond => 125;
  14. /// 更新帧率
  15. static int updateFrameRate = 25;
  16. /// 更新周期
  17. static int updatePeriod = 1000 ~/ updateFrameRate;
  18. /// 画布时间跨度(秒)
  19. int get timeSpan => 3;
  20. /// 横坐标数据量
  21. int get xDataCount => dataPerSecond * timeSpan;
  22. /// 所有心电数据
  23. List<int> allPoints = [];
  24. /// 需绘制的新数据
  25. List<int> newPointsToDraw = [];
  26. /// 需绘制的历史数据
  27. List<int> oldPointsToDraw = [];
  28. /// 启动时时间戳
  29. int startTime = DateTime.now().millisecondsSinceEpoch;
  30. /// 当前数据位(根据时间戳计算)
  31. int currentDataIndex = 0;
  32. /// 数据刷新定时器
  33. Timer timer = Timer(Duration.zero, () {});
  34. /// 发生错误时暂停了
  35. bool isPaused = false;
  36. /// 读取到数据的回调
  37. void addData(List<int> data) {
  38. if (allPoints.isEmpty) {
  39. startTime = DateTime.now().millisecondsSinceEpoch;
  40. _startTimer();
  41. }
  42. allPoints.addAll(data);
  43. if (isPaused) {
  44. isPaused = false;
  45. _startTimer();
  46. }
  47. }
  48. /// 打开全屏心电图弹窗
  49. void openFullScreenDialog() {
  50. print("当前点总数为:${allPoints.length}");
  51. if (allPoints.length < dataPerSecond * 30) {
  52. PromptBox.toast("未完成检测,数据量不足");
  53. return;
  54. }
  55. Get.dialog(
  56. const FullScreenEcgDataDialog(),
  57. );
  58. }
  59. // 重置
  60. void reset() {
  61. allPoints.clear();
  62. newPointsToDraw.clear();
  63. oldPointsToDraw.clear();
  64. startTime = DateTime.now().millisecondsSinceEpoch;
  65. currentDataIndex = 0;
  66. timer.cancel();
  67. isPaused = false;
  68. update(['ecg_view']);
  69. }
  70. /// 获取完整心电图的base64(带base64头)
  71. Future<String> getFullDataImageBase64() async {
  72. if (allPoints.length < dataPerSecond * 30) {
  73. PromptBox.toast("未完成检测,数据量不足");
  74. return "";
  75. }
  76. final painter = EcgPainterForAll(
  77. allPoints: allPoints,
  78. yMax: 600,
  79. );
  80. final bgPainter = GridBackgroundPainterForAll();
  81. const size = Size(5000, 650);
  82. // 使用离屏Canvas绘制
  83. final Uint8List? bytes =
  84. await _capturePainterToImage(painter, bgPainter, size);
  85. if (bytes == null) {
  86. return "";
  87. } else {
  88. return _convertToBase64Url(bytes);
  89. }
  90. }
  91. /// 开启定时器,每隔一定时间添加一次数据,并且更新UI
  92. void _startTimer() {
  93. timer = Timer.periodic(
  94. Duration(milliseconds: updatePeriod),
  95. (timer) {
  96. // print("timer: ${timer.tick}");
  97. _updateData();
  98. },
  99. );
  100. }
  101. /// 每帧更新数据
  102. void _updateData() {
  103. // 计算当前数据位
  104. currentDataIndex = (DateTime.now().millisecondsSinceEpoch - startTime) ~/
  105. (1000 ~/ dataPerSecond);
  106. /// 需显示的数据量
  107. int needDataCount = currentDataIndex % xDataCount;
  108. /// 当前周期数
  109. int currentPeriod = currentDataIndex ~/ xDataCount;
  110. // 计算新数据
  111. try {
  112. newPointsToDraw = allPoints.sublist(
  113. currentDataIndex - needDataCount,
  114. currentDataIndex,
  115. );
  116. if (currentPeriod > 0) {
  117. oldPointsToDraw = allPoints.sublist(
  118. (currentPeriod - 1) * xDataCount,
  119. currentPeriod * xDataCount,
  120. );
  121. }
  122. // print(
  123. // "update newPointsToDraw: ${currentDataIndex} ${needDataCount} ${newPointsToDraw.length} --currentPeriod ${currentPeriod}");
  124. } catch (e) {
  125. timer.cancel();
  126. isPaused = true;
  127. }
  128. update(['ecg_view']);
  129. }
  130. /// 将字节数组转换为base64
  131. String _convertToBase64Url(Uint8List imageData) {
  132. String base64Image = base64Encode(imageData);
  133. String base64Url = base64Image;
  134. return base64Url;
  135. }
  136. /// 将CustomPainter绘制的内容转换为图片
  137. Future<Uint8List?> _capturePainterToImage(
  138. CustomPainter painter, CustomPainter bgPainter, Size size) async {
  139. final bounds = Offset.zero & size;
  140. final picture = PictureRecorder();
  141. final pictureCanvas = Canvas(picture);
  142. // 给Canvas设置绘制范围
  143. pictureCanvas.clipRect(bounds);
  144. // 在Canvas上进行绘制
  145. bgPainter.paint(pictureCanvas, size);
  146. painter.paint(pictureCanvas, size);
  147. /// 绘制一圈边框
  148. final borderPaint = Paint()
  149. ..color = Colors.black
  150. ..style = PaintingStyle.stroke
  151. ..strokeWidth = 1.0;
  152. pictureCanvas.drawRect(bounds, borderPaint);
  153. // 结束绘制
  154. final recordedPicture = picture.endRecording();
  155. final image =
  156. await recordedPicture.toImage(size.width.toInt(), size.height.toInt());
  157. // 转换为字节数组
  158. final byteData = await image.toByteData(format: ui.ImageByteFormat.png);
  159. final bytes = byteData?.buffer.asUint8List();
  160. return bytes;
  161. }
  162. // @override
  163. // void onInit() {
  164. // super.onInit();
  165. // }
  166. @override
  167. void onReady() {
  168. super.onReady();
  169. print("onReady");
  170. }
  171. @override
  172. void onClose() {
  173. super.onClose();
  174. timer.cancel();
  175. }
  176. }