CVideoCaptureWorker.cpp 3.5 KB

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