etCameraTest.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. #include "etCameraTest.h"
  2. #include "ui_etCameraTest.h"
  3. #include <QScrollBar>
  4. #include <QHBoxLayout>
  5. #include <QLabel>
  6. #include <QPalette>
  7. #include "CAppInfo.h"
  8. #include "awMsgBox.h"
  9. #include "logproc.h"
  10. #include "CCommonTools.h"
  11. etCameraTest::etCameraTest(QWidget *parent) :
  12. QWidget(parent),
  13. ui(new Ui::etCameraTest)
  14. {
  15. ui->setupUi(this);
  16. setStyleSheet(g_appInfoPtr->m_sQssStr);
  17. m_nCameraOpenStatus = 2;
  18. // m_cam.set(CV_CAP_PROP_FOURCC, CV_FOURCC('F', 'M', 'P', '4'));
  19. // int inWidth = ui->widget_etct_camera->width();
  20. // m_cam.set(CV_CAP_PROP_FRAME_WIDTH, inWidth);
  21. // int inHeight = ui->widget_etct_camera->height();
  22. // m_cam.set(CV_CAP_PROP_FRAME_HEIGHT, inHeight);
  23. // m_cam.set(CV_CAP_PROP_FPS, 5);
  24. // m_cam.set(CV_CAP_PROP_BRIGHTNESS, 60);//亮度
  25. // m_cam.set(CV_CAP_PROP_CONTRAST, 30);//对比度 40
  26. // m_cam.set(CV_CAP_PROP_HUE, 50);//色调 50
  27. // m_cam.set(CV_CAP_PROP_EXPOSURE, 60);//曝光 50 获取摄像头参数
  28. // m_pinitTimer = std::make_shared<QTimer>();
  29. // m_pinitTimer->setInterval(1000);
  30. // connect(m_pinitTimer.get(), &QTimer::timeout, this, [&](){
  31. // m_pinitTimer->stop();
  32. // bool bOpened = true;
  33. // if(!m_cam.open(0))
  34. // {
  35. // ShowMsg(QString::fromLocal8Bit("打开摄像头失败"), this, MSG_ICON_TYPE::mit_error);
  36. // bOpened = false;
  37. // }
  38. // m_pVideoTimer->start();
  39. // ui->tablewt_envTest->item(0, 0)->setText(QString::fromLocal8Bit("摄像头正常启用"));
  40. // QWidget *widget = new QWidget;
  41. // QHBoxLayout *layout = new QHBoxLayout;
  42. // widget->setLayout(layout);
  43. // QLabel *icon = new QLabel;
  44. // if(bOpened)
  45. // {
  46. // ui->tablewt_envTest->item(0, 1)->setText(QString::fromLocal8Bit("正常"));
  47. // icon->setFixedSize(QSize(g_appInfoPtr->m_fRate * 12, g_appInfoPtr->m_fRate * 9));
  48. // icon->setPixmap(QPixmap(":/images/icon-et-item-pass.png").scaled(g_appInfoPtr->m_fRate * 12, g_appInfoPtr->m_fRate * 9));
  49. // }
  50. // else
  51. // {
  52. // ui->tablewt_envTest->item(0, 1)->setText(QString::fromLocal8Bit("异常"));
  53. // icon->setFixedSize(QSize(g_appInfoPtr->m_fRate * 8, g_appInfoPtr->m_fRate * 8));
  54. // icon->setPixmap(QPixmap(":/images/icon-et-iten-unpass.png").scaled(g_appInfoPtr->m_fRate * 8, g_appInfoPtr->m_fRate * 8));
  55. // }
  56. // layout->addWidget(icon);
  57. // layout->addStretch();
  58. // ui->tablewt_envTest->setCellWidget(0, 2, widget);
  59. // });
  60. //ui->tablewt_envTest->item(0, 0)->setText(QString::fromLocal8Bit("摄像头正常启用"));
  61. // if(bOpened)
  62. // {
  63. // ui->tablewt_envTest->item(0, 1)->setText(QString::fromLocal8Bit("正常"));
  64. // icon->setFixedSize(QSize(g_appInfoPtr->m_fRate * 12, g_appInfoPtr->m_fRate * 9));
  65. // icon->setPixmap(QPixmap(":/images/icon-et-item-pass.png").scaled(g_appInfoPtr->m_fRate * 12, g_appInfoPtr->m_fRate * 9));
  66. // }
  67. // else
  68. // {
  69. // ui->tablewt_envTest->item(0, 1)->setText(QString::fromLocal8Bit("异常"));
  70. // icon->setFixedSize(QSize(g_appInfoPtr->m_fRate * 8, g_appInfoPtr->m_fRate * 8));
  71. // icon->setPixmap(QPixmap(":/images/icon-et-iten-unpass.png").scaled(g_appInfoPtr->m_fRate * 8, g_appInfoPtr->m_fRate * 8));
  72. // }
  73. m_pVideoTimer = std::make_shared<QTimer>();
  74. m_pVideoTimer->setInterval(200);
  75. connect(m_pVideoTimer.get(), &QTimer::timeout, this, [&](){
  76. if (!m_bSetCameraStaus)
  77. {
  78. m_bSetCameraStaus = true;
  79. setCameraStaus(true);
  80. }
  81. cv::Mat frame;
  82. {
  83. std::scoped_lock lock(m_imageMutex);
  84. frame = m_nCurImage;
  85. }
  86. if(frame.empty())
  87. {
  88. qDebug()<<"widgetCameraTest frame is empty";
  89. return;
  90. }
  91. QImage img = CCommonTools::Mat2QImage(frame);
  92. ui->widget_etct_camera->setAutoFillBackground(true);
  93. QPalette palette;
  94. palette.setBrush(QPalette::Window, QBrush(img.scaled(ui->widget_etct_camera->width(), ui->widget_etct_camera->height())));
  95. ui->widget_etct_camera->setPalette(palette);
  96. });
  97. }
  98. etCameraTest::~etCameraTest()
  99. {
  100. m_pVideoTimer->stop();
  101. g_clientVideoProcPtr->stopTest(true);
  102. awMsgBox::clear(this);
  103. delete ui;
  104. }
  105. void etCameraTest::setCameraStaus(bool bOpened)
  106. {
  107. QWidget *widget = new QWidget;
  108. QHBoxLayout *layout = new QHBoxLayout;
  109. widget->setLayout(layout);
  110. QLabel *icon = new QLabel;
  111. ui->tablewt_envTest->item(0, 0)->setText(QString::fromLocal8Bit("摄像头正常启用"));
  112. ui->tablewt_envTest->item(0, 1)->setText(QString::fromLocal8Bit("异常"));
  113. icon->setFixedSize(QSize(g_appInfoPtr->m_fRate * 8, g_appInfoPtr->m_fRate * 8));
  114. icon->setPixmap(QPixmap(":/images/icon-et-iten-unpass.png").scaled(g_appInfoPtr->m_fRate * 8, g_appInfoPtr->m_fRate * 8));
  115. if(bOpened)
  116. {
  117. ui->tablewt_envTest->item(0, 1)->setText(QString::fromLocal8Bit("正常"));
  118. icon->setFixedSize(QSize(g_appInfoPtr->m_fRate * 12, g_appInfoPtr->m_fRate * 9));
  119. icon->setPixmap(QPixmap(":/images/icon-et-item-pass.png").scaled(g_appInfoPtr->m_fRate * 12, g_appInfoPtr->m_fRate * 9));
  120. }
  121. else
  122. {
  123. ui->tablewt_envTest->item(0, 1)->setText(QString::fromLocal8Bit("异常"));
  124. icon->setFixedSize(QSize(g_appInfoPtr->m_fRate * 8, g_appInfoPtr->m_fRate * 8));
  125. icon->setPixmap(QPixmap(":/images/icon-et-iten-unpass.png").scaled(g_appInfoPtr->m_fRate * 8, g_appInfoPtr->m_fRate * 8));
  126. }
  127. layout->addWidget(icon);
  128. layout->addStretch();
  129. ui->tablewt_envTest->setCellWidget(0, 2, widget);
  130. }
  131. void etCameraTest::onRenderVideoFrame(const char* userId, TRTCVideoStreamType streamType, TRTCVideoFrame* frame)
  132. {
  133. if(g_clientVideoProcPtr->isCameraTest())
  134. {
  135. __int64 nServerTime = g_appInfoPtr->serverMTime();
  136. if(nServerTime - m_lastFaceTime >= 100)
  137. {
  138. m_lastFaceTime = nServerTime;
  139. cv::Mat matImg;
  140. cv::cvtColor(cv::Mat(frame->height, frame->width, CV_8UC4, frame->data), matImg, CV_RGBA2RGB);
  141. if(matImg.empty())
  142. {
  143. qDebug()<<"widgetCameraTest frame is empty";
  144. return;
  145. }
  146. std::scoped_lock lock(m_imageMutex);
  147. m_nCurImage = matImg;
  148. }
  149. }
  150. }
  151. int etCameraTest::setUI(const int nLeft, const int nTop, const int nWidth)
  152. {
  153. setGeometry(nLeft, nTop, nWidth, g_appInfoPtr->m_fRate*507);
  154. ui->widget_etct_camera->setGeometry(g_appInfoPtr->m_fRate*30, g_appInfoPtr->m_fRate*30,
  155. g_appInfoPtr->m_fRate*536, g_appInfoPtr->m_fRate*300);
  156. ui->btn_IsNotSelf->setGeometry(ui->widget_etct_camera->x() + ui->widget_etct_camera->width() + g_appInfoPtr->m_fRate*20,
  157. ui->widget_etct_camera->y(), g_appInfoPtr->m_fRate*184, g_appInfoPtr->m_fRate*140);
  158. ui->btn_IsNotSelf->setIconSize(QSize(g_appInfoPtr->m_fRate*32, g_appInfoPtr->m_fRate*32));
  159. ui->btn_IsNotSelf->setIcon(QIcon(":/images/icon-is-not-self.png"));
  160. ui->btn_IsSelf->setGeometry(ui->btn_IsNotSelf->x(), ui->btn_IsNotSelf->y() + ui->btn_IsNotSelf->height() + g_appInfoPtr->m_fRate*20,
  161. ui->btn_IsNotSelf->width(), ui->btn_IsNotSelf->height());
  162. ui->btn_IsSelf->setIconSize(QSize(g_appInfoPtr->m_fRate*32, g_appInfoPtr->m_fRate*32));
  163. ui->btn_IsSelf->setIcon(QIcon(":/images/icon-is-self.png"));
  164. ui->label_HLine->setGeometry(ui->widget_etct_camera->x(), ui->widget_etct_camera->y() + ui->widget_etct_camera->height() + g_appInfoPtr->m_fRate*30,
  165. width() - g_appInfoPtr->m_fRate*30*2, g_appInfoPtr->m_fRate*1 < 1 ? 1 : g_appInfoPtr->m_fRate*1);
  166. ui->tablewt_envTest->setGeometry(ui->widget_etct_camera->x(), ui->label_HLine->y() + ui->label_HLine->height(),
  167. width() - g_appInfoPtr->m_fRate*30*2, height() - ui->label_HLine->y() - ui->label_HLine->height());
  168. ui->tablewt_envTest->setEditTriggers(QAbstractItemView::NoEditTriggers);
  169. //整行选中的方式
  170. ui->tablewt_envTest->setSelectionBehavior(QAbstractItemView::SelectRows);
  171. //设置为只能选中一行
  172. ui->tablewt_envTest->setSelectionMode(QAbstractItemView::SingleSelection);
  173. //隐藏列表头
  174. ui->tablewt_envTest->verticalHeader()->setVisible(false);
  175. //隐藏边框
  176. ui->tablewt_envTest->setShowGrid(false);
  177. //表头不高亮显示
  178. ui->tablewt_envTest->horizontalHeader()->setHighlightSections(false);
  179. //设置行数
  180. ui->tablewt_envTest->setRowCount(2);
  181. //设置列数
  182. ui->tablewt_envTest->setColumnCount(3);
  183. ui->tablewt_envTest->setHorizontalScrollMode(QTableWidget::ScrollPerPixel);
  184. ui->tablewt_envTest->horizontalScrollBar()->setSingleStep(g_appInfoPtr->m_fRate*5);
  185. QStringList sHeadStr;
  186. sHeadStr << QString::fromLocal8Bit("检测项") <<
  187. QString::fromLocal8Bit("值") <<
  188. QString::fromLocal8Bit("状态");
  189. ui->tablewt_envTest->setHorizontalHeaderLabels(sHeadStr);
  190. ui->tablewt_envTest->horizontalHeader()->setDefaultAlignment(Qt::AlignLeft | Qt::AlignVCenter);
  191. ui->tablewt_envTest->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed);
  192. ui->tablewt_envTest->verticalHeader()->setDefaultSectionSize(g_appInfoPtr->m_fRate*48);
  193. ui->tablewt_envTest->setColumnWidth(0, g_appInfoPtr->m_fRate*525);
  194. ui->tablewt_envTest->setColumnWidth(1, g_appInfoPtr->m_fRate*170);
  195. ui->tablewt_envTest->setColumnWidth(2, g_appInfoPtr->m_fRate*24);
  196. ui->tablewt_envTest->horizontalHeader()->setStretchLastSection(true);
  197. for(int i = 0; i < 2; i++)
  198. {
  199. ui->tablewt_envTest->setItem(i, 0, new QTableWidgetItem(QString::fromLocal8Bit("")));
  200. ui->tablewt_envTest->setItem(i, 1, new QTableWidgetItem(QString::fromLocal8Bit("")));
  201. }
  202. setCameraStaus(false);
  203. g_clientVideoProcPtr->startTest(this, true);
  204. m_pVideoTimer->start();
  205. return height();
  206. }
  207. int etCameraTest::widgetHeight()
  208. {
  209. return height();
  210. }
  211. int etCameraTest::getCheckStatus()
  212. {
  213. return m_nCameraOpenStatus;
  214. }
  215. void etCameraTest::on_btn_IsSelf_clicked()
  216. {
  217. ui->tablewt_envTest->item(1, 0)->setText(QString::fromLocal8Bit("视频显示的是电脑操作者本人"));
  218. ui->tablewt_envTest->item(1, 1)->setText(QString::fromLocal8Bit("正常"));
  219. QWidget *widget = new QWidget;
  220. QHBoxLayout *layout = new QHBoxLayout;
  221. widget->setLayout(layout);
  222. QLabel *icon = new QLabel;
  223. icon->setFixedSize(QSize(g_appInfoPtr->m_fRate * 12, g_appInfoPtr->m_fRate * 9));
  224. icon->setPixmap(QPixmap(":/images/icon-et-item-pass.png").scaled(g_appInfoPtr->m_fRate * 12, g_appInfoPtr->m_fRate * 9));
  225. layout->addWidget(icon);
  226. layout->addStretch();
  227. ui->tablewt_envTest->setCellWidget(1, 2, widget);
  228. m_nCameraOpenStatus = 1;
  229. }
  230. void etCameraTest::on_btn_IsNotSelf_clicked()
  231. {
  232. ui->tablewt_envTest->item(1, 0)->setText(QString::fromLocal8Bit("视频显示的是电脑操作者本人"));
  233. ui->tablewt_envTest->item(1, 1)->setText(QString::fromLocal8Bit("请检查摄像头"));
  234. QWidget *widget = new QWidget;
  235. QHBoxLayout *layout = new QHBoxLayout;
  236. widget->setLayout(layout);
  237. QLabel *icon = new QLabel;
  238. icon->setFixedSize(QSize(g_appInfoPtr->m_fRate * 8, g_appInfoPtr->m_fRate * 8));
  239. icon->setPixmap(QPixmap(":/images/icon-et-iten-unpass.png").scaled(g_appInfoPtr->m_fRate * 8, g_appInfoPtr->m_fRate * 8));
  240. layout->addWidget(icon);
  241. layout->addStretch();
  242. ui->tablewt_envTest->setCellWidget(1, 2, widget);
  243. m_nCameraOpenStatus = 0;
  244. }