#include "mainwindow.h" #include "ui_mainwindow.h" #include #include #include #include #include #include #include "commonhelper.h" #include "frameless_helper.h" #include #pragma comment(lib, "winmm") #pragma comment(lib, "ws2_32") MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); // m_bMousePressed = false; // m_bWindowMenu = false; // m_bThreadRunning_CaptureDeviceData = false; m_ptrThread_CaptureDeviceData = nullptr; m_bEnableCapture = false; m_nImageMoveIndex = 0; // 初始化界面 InitStyle(); // this->setFixedWidth(1360); ui->wgtTitleBar->setFixedWidth(1350); // m_varCaptureData.InitQueue(DefaultMaxImageSize, 6); m_varY16Data.InitQueue(DefaultImageWidth * DefaultImageHeight * 2, 6); // 加载信号槽函数连接 Connect(); InitOpenGLView(); m_pTestDataBuffer = new byte[IMG_WIDTH * IMG_HEIGHT * 4 * 2 + 4096]; for (int h = 0; h < IMG_HEIGHT * 2; h++) { memset(m_pTestDataBuffer + h * IMG_WIDTH * 4, h, IMG_WIDTH * 4); } m_pSendDataBuffer = new byte[IMG_WIDTH * IMG_HEIGHT * 2 + 4096]; memset(m_pSendDataBuffer, 0, IMG_WIDTH * IMG_HEIGHT * 2); m_pUdpSocket = nullptr; } MainWindow::~MainWindow() { if (nullptr != m_pTestDataBuffer) { delete [] m_pTestDataBuffer; m_pTestDataBuffer = nullptr; } for (int i = 0; i < 2; i++) { if (nullptr != m_pOpenGLView[i]) { delete m_pOpenGLView[i]; m_pOpenGLView[i] = nullptr; } } // StopThread_DealDeviceData(); Disconnect(); delete ui; } bool MainWindow::CaptureData(byte* pFrameData, int nFrameDataLen) { return true; } void MainWindow::InitOpenGLView() { ui->m_openGLWidget_View01->Init(IMG_WIDTH, IMG_HEIGHT); m_pOpenGLView[0] = ui->m_openGLWidget_View01; ui->m_openGLWidget_View02->Init(IMG_WIDTH, IMG_HEIGHT); m_pOpenGLView[1] = ui->m_openGLWidget_View02; } void MainWindow::Connect() { // 通用 connect(ui->m_pushButton_MinMenu, &QPushButton::clicked, this, &MainWindow::OnButtonClicked); connect(ui->m_pushButton_CloseMenu, &QPushButton::clicked, this, &MainWindow::OnButtonClicked); connect(ui->m_pushButton_Settings, &QPushButton::clicked, this, &MainWindow::OnButtonClicked); connect(ui->m_pushButton_WindowMenu, &QPushButton::clicked, this, &MainWindow::OnButtonClicked); // 视频采集 connect(ui->m_pushButton_CaptureData, &QPushButton::clicked, this, &MainWindow::OnButtonClicked_Capture); connect(&m_varTimer, &QTimer::timeout, this, &MainWindow::OnTimer); m_varTimer.setTimerType(Qt::PreciseTimer); m_varTimer.start(40); } void MainWindow::Disconnect() { // 通用 disconnect(ui->m_pushButton_MinMenu, &QPushButton::clicked, this, &MainWindow::OnButtonClicked); disconnect(ui->m_pushButton_CloseMenu, &QPushButton::clicked, this, &MainWindow::OnButtonClicked); disconnect(ui->m_pushButton_Settings, &QPushButton::clicked, this, &MainWindow::OnButtonClicked); disconnect(ui->m_pushButton_WindowMenu, &QPushButton::clicked, this, &MainWindow::OnButtonClicked); // 视频采集 disconnect(ui->m_pushButton_CaptureData, &QPushButton::clicked, this, &MainWindow::OnButtonClicked_Capture); m_varTimer.stop(); disconnect(&m_varTimer, &QTimer::timeout, this, &MainWindow::OnTimer); } void MainWindow::OnButtonClicked() { // 最小化 if (sender() == ui->m_pushButton_MinMenu) { this->showMinimized(); } // 退出程序 if (sender() == ui->m_pushButton_CloseMenu) { qApp->exit(); } // 调光设置窗口 if (sender() == ui->m_pushButton_WindowMenu) { m_bWindowMenu = !m_bWindowMenu; if (m_bWindowMenu) { this->setFixedWidth(660 + 300); ui->wgtTitleBar->setFixedWidth(650 + 300); } else { this->setFixedWidth(660); ui->wgtTitleBar->setFixedWidth(650); } } } void MainWindow::OnButtonClicked_Capture() { // 采集数据 if (sender() == ui->m_pushButton_CaptureData) { m_bEnableCapture = !m_bEnableCapture; if (m_bEnableCapture) { //StartThread_DealDeviceData(); if (nullptr != m_pUdpSocket) { m_pUdpSocket->close(); delete m_pUdpSocket; m_pUdpSocket = nullptr; } m_pUdpSocket = new QUdpSocket(this); m_pUdpSocket->bind(5566); m_pUdpSocket->setSocketOption(QAbstractSocket::SocketOption::SendBufferSizeSocketOption, 512 * 1024); ui->m_pushButton_CaptureData->setText(tr("停止采集")); } else { //StopThread_DealDeviceData(); if (nullptr != m_pUdpSocket) { m_pUdpSocket->close(); delete m_pUdpSocket; m_pUdpSocket = nullptr; } ui->m_pushButton_CaptureData->setText(tr("开始采集")); } } } void MainWindow::OnComboxIndexChanged(int nIndex) { } void MainWindow::InitStyle() { m_bMousePressed = false; //安装事件监听器,让标题栏识别鼠标双击 ui->lblTitle->installEventFilter(this); this->setWindowFlags(/*Qt::FramelessWindowHint | Qt::WindowSystemMenuHint |*/ Qt::WindowMinimizeButtonHint); FramelessHelper* pHelper = new FramelessHelper(this); pHelper->activateOn(this); //激活当前窗体 pHelper->setTitleHeight(ui->wgtTitleBar->height()); //设置窗体的标题栏高度 CommonHelper::SetStyle("blue");//"blue"//"dark" } bool MainWindow::eventFilter(QObject* obj, QEvent* event) { //if (ui->lblTitle == obj) { if (event->type() == QEvent::MouseButtonPress) { QMouseEvent* mouseEvent = dynamic_cast(event); if (mouseEvent->button() == Qt::LeftButton) { m_bMousePressed = true; m_ptMouse = mouseEvent->globalPos() - pos(); mouseEvent->accept(); } return true; } else if (event->type() == QEvent::MouseMove) { QMouseEvent* mouseEvent = dynamic_cast(event); if (m_bMousePressed && this->windowState() != Qt::WindowMaximized) { if (mouseEvent->globalY() <= qApp->desktop()->availableGeometry().bottom()) { move(mouseEvent->globalPos() - m_ptMouse); mouseEvent->accept(); } else { qApp->desktop()->cursor().setPos( qApp->desktop()->cursor().pos().x(), qApp->desktop()->availableGeometry().bottom() ); } } return true; } else if (event->type() == QEvent::MouseButtonRelease) { QMouseEvent* mouseEvent = dynamic_cast(event); if (mouseEvent->button() == Qt::LeftButton) { m_bMousePressed = false; mouseEvent->accept(); } return true; } } return QMainWindow::eventFilter(obj, event); } void MainWindow::OnTimer() { if (m_bEnableCapture) { m_nImageMoveIndex += 1; if (m_nImageMoveIndex >= IMG_HEIGHT) { m_nImageMoveIndex = 0; } m_pOpenGLView[0]->UpdateImageData(&m_pTestDataBuffer[IMG_WIDTH * 4 * m_nImageMoveIndex]); m_pOpenGLView[1]->UpdateImageData(&m_pTestDataBuffer[IMG_WIDTH * 4 * m_nImageMoveIndex]); if (nullptr != m_pUdpSocket) { byte* pDataAddr = &m_pTestDataBuffer[IMG_WIDTH * 4 * m_nImageMoveIndex]; uint16_t* pSendDataAddr = (uint16_t*)m_pSendDataBuffer; for (int i = 0; i < IMG_WIDTH * IMG_HEIGHT; i++) { pSendDataAddr[i] = *(uint16_t*)&pDataAddr[i * 4]; } byte szSendMsg[1284] = {0}; for (int i = 0; i < IMG_HEIGHT / 2; i++) { memset(szSendMsg, 0, sizeof(szSendMsg)); memcpy(&szSendMsg[4], &pSendDataAddr[i * 640], 1280); uint16_t* pSendMsgAddr = (uint16_t*)szSendMsg; pSendMsgAddr[0] = 0x55aa; pSendMsgAddr[1] = i; // m_pUdpSocket->waitForBytesWritten(); if (!m_pUdpSocket->writeDatagram(QByteArray((char*)szSendMsg, 1284), QHostAddress("127.0.0.1"), 8888)) { QMessageBox msgBox; msgBox.setText("send failed!"); msgBox.exec(); } } } } } void MainWindow::ThreadEntry(ThreadRunFunPtr pRunFun, void* pOwner) { MainWindow* pThis = reinterpret_cast(pOwner); if (NULL == pThis) { return; } (pThis->*pRunFun)(); } void MainWindow::ThreadFun_CaptureDeviceData() { timeBeginPeriod(1); byte* pReadData = new byte[2688 * 1024 + 4096]; std::chrono::system_clock::time_point tm2_2 = std::chrono::system_clock::now(); int nTimeOutCount = 0; while (m_bThreadRunning_CaptureDeviceData) { int nRes = 0; byte szSendMsg[1284]; for (int i = 0; i < IMG_HEIGHT / 2; i++) { memset(szSendMsg, 0, sizeof(szSendMsg)); memset(&szSendMsg[4], i + 1, 1280); uint16_t* pSendMsgAddr = (uint16_t*)szSendMsg; pSendMsgAddr[0] = 0x55aa; pSendMsgAddr[1] = i; // uint16_t szSendMsgT[642] = {0}; memcpy(szSendMsgT, szSendMsg, 1284); m_pUdpSocket->waitForBytesWritten(); if (!m_pUdpSocket->writeDatagram(QByteArray((char*)szSendMsg, 1284), QHostAddress("127.0.0.1"), 8888)) { qDebug("send datagram failed!!!"); } //std::this_thread::sleep_for(std::chrono::microseconds(1)); } std::this_thread::sleep_for(std::chrono::milliseconds(25)); //m_varCaptureData.PushBack(pReadData, DefaultMaxImageSize, nRes); } timeEndPeriod(1); } bool MainWindow::StartThread_DealDeviceData() { StopThread_DealDeviceData(); if (nullptr != m_pUdpSocket) { m_pUdpSocket->close(); delete m_pUdpSocket; m_pUdpSocket = nullptr; } m_pUdpSocket = new QUdpSocket(this); m_pUdpSocket->bind(5566); { //int nSize = m_pUdpSocket->socketOption(QAbstractSocket::SocketOption::SendBufferSizeSocketOption).toInt(); SOCKET nNativeSock = static_cast((const qintptr)m_pUdpSocket->socketDescriptor()); int nSendBufferSize = 128 * 1024; //int nEC = setsockopt(nNativeSock, SOL_SOCKET, SO_SNDBUF, (char*)&nSendBufferSize, 4); /* int nVal = 0; int nLen = sizeof(nVal); nEC = getsockopt(nNativeSock, SOL_SOCKET, SO_SNDBUF, (char*)&nVal, &nLen); */ // nSize = m_pUdpSocket->socketOption(QAbstractSocket::SocketOption::SendBufferSizeSocketOption).toInt(); } // m_bThreadRunning_CaptureDeviceData = true; m_ptrThread_CaptureDeviceData = new std::thread(std::bind(&MainWindow::ThreadEntry, &MainWindow::ThreadFun_CaptureDeviceData, (void*)this)); return true; } void MainWindow::StopThread_DealDeviceData() { // if (m_bThreadRunning_CaptureDeviceData) { m_bThreadRunning_CaptureDeviceData = false; if (nullptr != m_ptrThread_CaptureDeviceData) { m_ptrThread_CaptureDeviceData->join(); delete m_ptrThread_CaptureDeviceData; m_ptrThread_CaptureDeviceData = nullptr; } // if (nullptr != m_pUdpSocket) { m_pUdpSocket->close(); delete m_pUdpSocket; m_pUdpSocket = nullptr; } } }