CVideoCaptureWorker.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include "../pch/pch.h"
  2. #include "CVideoCaptureWorker.h"
  3. #include "../tool/CAppEnv.h"
  4. CVideoCaptureWorker::CVideoCaptureWorker()
  5. {
  6. }
  7. CVideoCaptureWorker::~CVideoCaptureWorker()
  8. {
  9. }
  10. //开始工作
  11. void CVideoCaptureWorker::Start()
  12. {
  13. m_is_work = true;
  14. //持续获取摄像头数据,直到程序退出,这里开启一个线程来处理摄像头数据的获取,避免阻塞主线程
  15. std::thread(&CVideoCaptureWorker::HandleVideoCapture, this).detach();
  16. CAppEnv::GetInstance()->AddWorkerNum();
  17. }
  18. //停止工作
  19. void CVideoCaptureWorker::Stop()
  20. {
  21. m_is_work = false;
  22. }
  23. // 新增:自动检测可用摄像头索引
  24. int CVideoCaptureWorker::findAvailableCamera()
  25. {
  26. for (int i = 0; i < 10; i++)
  27. {
  28. // 检测前10个索引
  29. cv::VideoCapture tempCap(i, cv::CAP_DSHOW);
  30. if (tempCap.isOpened())
  31. {
  32. tempCap.release();
  33. return i;
  34. }
  35. }
  36. return -1;
  37. }
  38. void CVideoCaptureWorker::HandleVideoCapture()
  39. {
  40. int cameraIndex = findAvailableCamera();
  41. if (cameraIndex == -1)
  42. {
  43. DEBUG_LOG("未找到任何可用摄像头!");
  44. return;
  45. }
  46. cv::VideoCapture cap(cameraIndex, cv::CAP_DSHOW);
  47. if (!cap.isOpened())
  48. {
  49. CSystem::my_sleep(1);
  50. DEBUG_LOG("打开摄像头失败,正在重试...");
  51. return;
  52. }
  53. DEBUG_LOG("摄像头打开成功!");
  54. /*
  55. 160×120(QQVGA):适用于低带宽或嵌入式场景,资源占用极低
  56. 320×240(QVGA):轻量级视频传输常用,适合实时性要求高的应用
  57. 640×480(VGA):OpenCV 默认常用分辨率,兼容性强,广泛用于基础图像处理
  58. 800×600(SVGA):中等清晰度,适用于需要细节但不追求高清的场景
  59. 1024×768(XGA):较高清分辨率,适合文档扫描或静态图像采集
  60. 1280×720(720p HD):标准高清分辨率,广泛用于人脸识别、目标检测等视觉任务
  61. 1920×1080(1080p Full HD):全高清分辨率,提供高质量图像,适用于高精度视觉分析
  62. */
  63. int width = 800;
  64. int height = 600;
  65. cap.set(cv::CAP_PROP_FRAME_WIDTH, width);
  66. cap.set(cv::CAP_PROP_FRAME_HEIGHT, height);
  67. // 验证设置是否成功
  68. double actualWidth = cap.get(cv::CAP_PROP_FRAME_WIDTH);
  69. double actualHeight = cap.get(cv::CAP_PROP_FRAME_HEIGHT);
  70. std::cout << "设置的分辨率: " << width << " x " << height << std::endl;
  71. std::cout << "实际分辨率: " << actualWidth << " x " << actualHeight << std::endl;
  72. while (m_is_work == true)
  73. {
  74. // 读取摄像头帧
  75. cap >> m_frame;
  76. if (m_frame.empty())
  77. {
  78. DEBUG_LOG("读取摄像头帧失败,正在重试...");
  79. CSystem::my_sleep(1);
  80. continue;
  81. }
  82. // 控制帧率,避免占用过多CPU资源,这里设置为30帧每秒
  83. Sleep(1000 / 30);
  84. //什么都不用做,只需要把摄像头读取的帧放在成员变量里就行,后续其他地方需要用到摄像头数据的时候直接从成员变量里取就行了
  85. // 显示摄像头帧(可选)
  86. //cv::imshow("Camera", m_frame);
  87. //if (cv::waitKey(30) >= 0) break; // 按任意键退出
  88. }
  89. // 程序走到这里,说明收银系统被退出了,这里不释放资源也无所谓了,释放资源太慢了要卡住几百毫秒
  90. //cap.release();
  91. //cv::destroyAllWindows();
  92. //走到这里说明线程要退出了,做一些清理工作
  93. CAppEnv::GetInstance()->DelWorkerNum();
  94. return;
  95. }
  96. void CVideoCaptureWorker::GetFrame(cv::Mat& frame)
  97. {
  98. frame = m_frame.clone();
  99. }