ImageTest.h 4.7 KB

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