ImageTest.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #ifndef IMAGE_TEST_H
  2. #define IMAGE_TEST_H
  3. #include <opencv4/opencv2/opencv.hpp>
  4. #include <string>
  5. #include <sys/time.h>
  6. #include <chrono>
  7. #include <fstream>
  8. #include <fstream>
  9. #include <atomic>
  10. #include <thread>
  11. #include <mutex>
  12. namespace UsbTest
  13. namespace UsbTest
  14. {
  15. void saveImageFromData(unsigned char *data, int width, int height, const std::string &filename = "./output.png");
  16. class HighResolutionTimer
  17. {
  18. public:
  19. // 开始计时
  20. void start()
  21. {
  22. start_time = std::chrono::high_resolution_clock::now();
  23. }
  24. // 停止计时
  25. void stop()
  26. {
  27. end_time = std::chrono::high_resolution_clock::now();
  28. }
  29. // 获取经过的时间(以秒为单位)
  30. double elapsed() const
  31. {
  32. return std::chrono::duration<double>(end_time - start_time).count();
  33. }
  34. // 获取经过的时间(以毫秒为单位)
  35. double elapsed_milliseconds() const
  36. {
  37. return std::chrono::duration<double, std::milli>(end_time - start_time).count();
  38. }
  39. // 获取经过的时间(以微秒为单位)
  40. double elapsed_microseconds() const
  41. {
  42. return std::chrono::duration<double, std::micro>(end_time - start_time).count();
  43. }
  44. protected:
  45. std::chrono::high_resolution_clock::time_point start_time;
  46. std::chrono::high_resolution_clock::time_point end_time;
  47. };
  48. class GlobalResolutionTimer: public HighResolutionTimer
  49. {
  50. public:
  51. static GlobalResolutionTimer& getInstance()
  52. {
  53. static GlobalResolutionTimer instance;
  54. return instance;
  55. }
  56. void start()
  57. {
  58. start_time = std::chrono::high_resolution_clock::now();
  59. }
  60. std::string pressStopWatchString()
  61. {
  62. end_time = std::chrono::high_resolution_clock::now();
  63. return "Elapsed Time: " + std::to_string(elapsed_milliseconds()) + " ms";
  64. }
  65. };
  66. class SimulateTrigger
  67. {
  68. public:
  69. static SimulateTrigger& getInstance()
  70. {
  71. static SimulateTrigger instance;
  72. return instance;
  73. }
  74. void startTrigger()
  75. {
  76. if (!triggering)
  77. {
  78. triggering = true;
  79. triggerStatus.resize(3);
  80. triggerStatus[0]=false;
  81. triggerStatus[1]=false;
  82. triggerStatus[2]=false;
  83. triggerThread = std::thread(&SimulateTrigger::triggerLoop, this);
  84. }
  85. }
  86. void stopTrigger()
  87. {
  88. if (triggering)
  89. {
  90. triggering = false;
  91. if (triggerThread.joinable())
  92. {
  93. triggerThread.join();
  94. }
  95. }
  96. }
  97. bool isTriggering(int index)
  98. {
  99. bool status = triggerStatus[index];
  100. std::lock_guard<std::mutex> lock(triggerMutex);
  101. triggerStatus[index] = false;
  102. return status;
  103. }
  104. private:
  105. SimulateTrigger() : triggering(false) {}
  106. ~SimulateTrigger()
  107. {
  108. stopTrigger();
  109. }
  110. // Delete copy constructor and assignment operator to prevent copying
  111. SimulateTrigger(const SimulateTrigger&) = delete;
  112. SimulateTrigger& operator=(const SimulateTrigger&) = delete;
  113. void triggerLoop()
  114. {
  115. while (triggering)
  116. {
  117. std::this_thread::sleep_for(std::chrono::milliseconds(188));
  118. if (triggering)
  119. {
  120. // Simulate the trigger signal
  121. std::lock_guard<std::mutex> lock(triggerMutex);
  122. triggerStatus[0]=true;
  123. triggerStatus[1]=true;
  124. triggerStatus[2]=true;
  125. }
  126. }
  127. }
  128. std::atomic<bool> triggering;
  129. std::thread triggerThread;
  130. std::mutex triggerMutex;
  131. std::vector<bool> triggerStatus;
  132. };
  133. class TimeRecorder
  134. {
  135. public:
  136. static TimeRecorder& getInstance()
  137. {
  138. static TimeRecorder instance;
  139. return instance;
  140. }
  141. bool init()
  142. {
  143. log_file = std::fstream("TimeLog.txt", std::ios::app);
  144. }
  145. void recordTime(std::string &message)
  146. {
  147. if (log_file.is_open())
  148. {
  149. log_file << message << std::endl;
  150. }
  151. }
  152. private:
  153. TimeRecorder() {}
  154. ~TimeRecorder()
  155. {
  156. if (log_file.is_open())
  157. {
  158. log_file.close();
  159. }
  160. }
  161. std::fstream log_file;
  162. };
  163. }
  164. #endif // IMAGE_TEST_H