├── .gitignore ├── LICENSE ├── README.md ├── TCPReceiver ├── TCPReceiver ├── TCPReceiver.pro ├── main.cpp ├── mainwindow.cpp ├── mainwindow.h └── mainwindow.ui ├── TCPSender ├── TCPSender ├── TCPSender.pro ├── main.cpp ├── mainwindow.cpp ├── mainwindow.h └── mainwindow.ui ├── qt_camera_gui ├── main.cpp ├── mainwindow.cpp ├── mainwindow.h ├── mainwindow.ui ├── qt_camera_gui ├── qt_camera_gui.pro ├── qthreadcamera.cpp ├── qthreadcamera.h ├── qthreadfile.cpp ├── qthreadfile.h ├── qthreadmessgge.cpp └── qthreadmessgge.h └── qt_monitor ├── main.cpp ├── mainwindow.cpp ├── mainwindow.h ├── mainwindow.ui ├── qt_monitor └── qt_monitor.pro /.gitignore: -------------------------------------------------------------------------------- 1 | # C++ objects and libs 2 | *.slo 3 | *.lo 4 | *.o 5 | *.a 6 | *.la 7 | *.lai 8 | *.so 9 | *.dll 10 | *.dylib 11 | 12 | # Qt-es 13 | object_script.*.Release 14 | object_script.*.Debug 15 | *_plugin_import.cpp 16 | /.qmake.cache 17 | /.qmake.stash 18 | *.pro.user 19 | *.pro.user.* 20 | *.qbs.user 21 | *.qbs.user.* 22 | *.moc 23 | moc_*.cpp 24 | moc_*.h 25 | qrc_*.cpp 26 | ui_*.h 27 | *.qmlc 28 | *.jsc 29 | Makefile* 30 | *build-* 31 | 32 | # Qt unit tests 33 | target_wrapper.* 34 | 35 | # QtCreator 36 | *.autosave 37 | 38 | # QtCreator Qml 39 | *.qmlproject.user 40 | *.qmlproject.user.* 41 | 42 | # QtCreator CMake 43 | CMakeLists.txt.user* 44 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 changcongyang 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # qt 2 | 基于qt的监控项目+视频文件传输 3 | testdev 4 | 5 | -------------------------------------------------------------------------------- /TCPReceiver/TCPReceiver: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ccycong/qt/bb7f0e5d97609a144de2bc28e13c7cb1196234f2/TCPReceiver/TCPReceiver -------------------------------------------------------------------------------- /TCPReceiver/TCPReceiver.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2019-07-14T20:04:25 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui 8 | 9 | TARGET = TCPReceiver 10 | TEMPLATE = app 11 | 12 | 13 | SOURCES += main.cpp\ 14 | mainwindow.cpp 15 | 16 | HEADERS += mainwindow.h 17 | 18 | FORMS += mainwindow.ui 19 | 20 | QT += network 21 | -------------------------------------------------------------------------------- /TCPReceiver/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "mainwindow.h" 3 | #include 4 | 5 | int main(int argc, char *argv[]) 6 | { 7 | QApplication a(argc, argv); 8 | MainWindow w; 9 | w.show(); 10 | 11 | QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8")); 12 | 13 | return a.exec(); 14 | } 15 | -------------------------------------------------------------------------------- /TCPReceiver/mainwindow.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | #include "ui_mainwindow.h" 3 | 4 | MainWindow::MainWindow(QWidget *parent) : 5 | QMainWindow(parent), 6 | ui(new Ui::MainWindow) 7 | { 8 | ui->setupUi(this); 9 | 10 | totalBytes = 0; 11 | bytesReceived = 0; 12 | fileNameSize = 0; 13 | 14 | //当发现新连接时发出newConnection()信号 15 | connect(&tcpServer,SIGNAL(newConnection()),this, 16 | SLOT(acceptConnection())); 17 | } 18 | 19 | MainWindow::~MainWindow() 20 | { 21 | delete ui; 22 | } 23 | 24 | 25 | 26 | 27 | void MainWindow::start() //开始监听 28 | { 29 | ui->startButton->setEnabled(false); 30 | bytesReceived =0; 31 | if(!tcpServer.listen(QHostAddress::Any,6666)) 32 | { 33 | qDebug() << tcpServer.errorString(); 34 | close(); 35 | return; 36 | } 37 | ui->serverStatusLabel->setText(tr("监听")); 38 | } 39 | 40 | 41 | 42 | 43 | void MainWindow::acceptConnection() //接受连接 44 | { 45 | tcpServerConnection = tcpServer.nextPendingConnection(); 46 | connect(tcpServerConnection,SIGNAL(readyRead()),this, 47 | SLOT(updateServerProgress())); 48 | connect(tcpServerConnection, 49 | SIGNAL(error(QAbstractSocket::SocketError)),this, 50 | SLOT(displayError(QAbstractSocket::SocketError))); 51 | ui->serverStatusLabel->setText(tr("接受连接")); 52 | tcpServer.close(); 53 | } 54 | 55 | 56 | 57 | 58 | void MainWindow::updateServerProgress() //更新进度条,接收数据 59 | { 60 | QDataStream in(tcpServerConnection); 61 | in.setVersion(QDataStream::Qt_4_6); 62 | if(bytesReceived <= sizeof(qint64)*2) 63 | { //如果接收到的数据小于16个字节,那么是刚开始接收数据,我们保存到//来的头文件信息 64 | if((tcpServerConnection->bytesAvailable() >= sizeof(qint64)*2) 65 | && (fileNameSize == 0)) 66 | { //接收数据总大小信息和文件名大小信息 67 | in >> totalBytes >> fileNameSize; 68 | bytesReceived += sizeof(qint64) * 2; 69 | } 70 | if((tcpServerConnection->bytesAvailable() >= fileNameSize) 71 | && (fileNameSize != 0)) 72 | { //接收文件名,并建立文件 73 | in >> fileName; 74 | ui->serverStatusLabel->setText(tr("接收文件 %1 ...") 75 | .arg(fileName)); 76 | bytesReceived += fileNameSize; 77 | localFile= new QFile(fileName); 78 | if(!localFile->open(QFile::WriteOnly)) 79 | { 80 | qDebug() << "open file error!"; 81 | return; 82 | } 83 | } 84 | else return; 85 | } 86 | if(bytesReceived < totalBytes) 87 | { //如果接收的数据小于总数据,那么写入文件 88 | bytesReceived += tcpServerConnection->bytesAvailable(); 89 | inBlock= tcpServerConnection->readAll(); 90 | localFile->write(inBlock); 91 | inBlock.resize(0); 92 | } 93 | //更新进度条 94 | ui->serverProgressBar->setMaximum(totalBytes); 95 | ui->serverProgressBar->setValue(bytesReceived); 96 | 97 | if(bytesReceived == totalBytes) 98 | { //接收数据完成时 99 | tcpServerConnection->close(); 100 | localFile->close(); 101 | ui->startButton->setEnabled(true); 102 | ui->serverStatusLabel->setText(tr("接收文件 %1 成功!") 103 | .arg(fileName)); 104 | } 105 | } 106 | 107 | 108 | 109 | void MainWindow::displayError(QAbstractSocket::SocketError) //错误处理 110 | { 111 | qDebug() << tcpServerConnection->errorString(); 112 | tcpServerConnection->close(); 113 | ui->serverProgressBar->reset(); 114 | ui->serverStatusLabel->setText(tr("服务端就绪")); 115 | ui->startButton->setEnabled(true); 116 | } 117 | 118 | 119 | 120 | void MainWindow::on_startButton_clicked()//开始监听按钮 121 | { 122 | start(); 123 | } 124 | -------------------------------------------------------------------------------- /TCPReceiver/mainwindow.h: -------------------------------------------------------------------------------- 1 | #ifndef MAINWINDOW_H 2 | #define MAINWINDOW_H 3 | 4 | #include 5 | #include 6 | 7 | namespace Ui { 8 | class MainWindow; 9 | } 10 | 11 | class MainWindow : public QMainWindow 12 | { 13 | Q_OBJECT 14 | 15 | public: 16 | explicit MainWindow(QWidget *parent = 0); 17 | ~MainWindow(); 18 | 19 | private slots: 20 | void on_startButton_clicked(); 21 | void start(); //开始监听 22 | void acceptConnection(); //建立连接 23 | void updateServerProgress(); //更新进度条,接收数据 24 | 25 | //显示错误 26 | void displayError(QAbstractSocket::SocketError socketError); 27 | 28 | private: 29 | Ui::MainWindow *ui; 30 | QTcpServer tcpServer; 31 | QTcpSocket *tcpServerConnection; 32 | qint64 totalBytes; //存放总大小信息 33 | qint64 bytesReceived; //已收到数据的大小 34 | qint64 fileNameSize; //文件名的大小信息 35 | QString fileName; //存放文件名 36 | QFile *localFile; //本地文件 37 | QByteArray inBlock; //数据缓冲区 38 | }; 39 | 40 | #endif // MAINWINDOW_H 41 | -------------------------------------------------------------------------------- /TCPReceiver/mainwindow.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | MainWindow 4 | 5 | 6 | 7 | 0 8 | 0 9 | 400 10 | 300 11 | 12 | 13 | 14 | MainWindow 15 | 16 | 17 | 18 | 19 | 20 | 40 21 | 40 22 | 66 23 | 17 24 | 25 | 26 | 27 | 服务端 28 | 29 | 30 | 31 | 32 | 33 | 40 34 | 90 35 | 291 36 | 23 37 | 38 | 39 | 40 | 0 41 | 42 | 43 | 44 | 45 | 46 | 230 47 | 150 48 | 98 49 | 27 50 | 51 | 52 | 53 | 开始监听 54 | 55 | 56 | 57 | 58 | 59 | 60 | 0 61 | 0 62 | 400 63 | 25 64 | 65 | 66 | 67 | 68 | 69 | TopToolBarArea 70 | 71 | 72 | false 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | -------------------------------------------------------------------------------- /TCPSender/TCPSender: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ccycong/qt/bb7f0e5d97609a144de2bc28e13c7cb1196234f2/TCPSender/TCPSender -------------------------------------------------------------------------------- /TCPSender/TCPSender.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2019-07-14T19:09:17 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui 8 | 9 | TARGET = TCPSender 10 | TEMPLATE = app 11 | 12 | 13 | SOURCES += main.cpp\ 14 | mainwindow.cpp 15 | 16 | HEADERS += mainwindow.h 17 | 18 | FORMS += mainwindow.ui 19 | 20 | QT += network 21 | -------------------------------------------------------------------------------- /TCPSender/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "mainwindow.h" 3 | #include 4 | 5 | int main(int argc, char *argv[]) 6 | { 7 | QApplication a(argc, argv); 8 | MainWindow w; 9 | w.show(); 10 | 11 | QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8")); 12 | 13 | return a.exec(); 14 | } 15 | -------------------------------------------------------------------------------- /TCPSender/mainwindow.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | #include "ui_mainwindow.h" 3 | #include 4 | 5 | MainWindow::MainWindow(QWidget *parent) : 6 | QMainWindow(parent), 7 | ui(new Ui::MainWindow) 8 | { 9 | ui->setupUi(this); 10 | 11 | loadSize = 4*1024; 12 | totalBytes = 0; 13 | bytesWritten = 0; 14 | bytesToWrite = 0; 15 | tcpClient = new QTcpSocket(this); 16 | //当连接服务器成功时,发出connected()信号,我们开始传送文件 17 | connect(tcpClient,SIGNAL(connected()),this,SLOT(startTransfer())); 18 | //当有数据发送成功时,我们更新进度条 19 | connect(tcpClient,SIGNAL(bytesWritten(qint64)),this, 20 | SLOT(updateClientProgress(qint64))); 21 | connect(tcpClient,SIGNAL(error(QAbstractSocket::SocketError)),this, 22 | SLOT(displayError(QAbstractSocket::SocketError))); 23 | //开始使”发送“按钮不可用 24 | //ui->sendButton->setEnabled(false); 25 | } 26 | 27 | MainWindow::~MainWindow() 28 | { 29 | delete ui; 30 | } 31 | 32 | 33 | 34 | 35 | void MainWindow::openFile() //打开文件 36 | { 37 | fileName = QFileDialog::getOpenFileName(this); 38 | if(!fileName.isEmpty()) 39 | { 40 | ui->sendButton->setEnabled(true); 41 | //ui->clientStatusLabel->setText(tr("打开文件 %1 成功!") 42 | //.arg(fileName)); 43 | } 44 | } 45 | 46 | 47 | 48 | 49 | void MainWindow::send() //连接到服务器,执行发送 50 | { 51 | ui->sendButton->setEnabled(false); 52 | bytesWritten = 0; 53 | //初始化已发送字节为0 54 | //ui->clientStatusLabel->setText(tr("连接中...")); 55 | tcpClient->connectToHost("192.168.46.46",6666);//连接 56 | } 57 | 58 | 59 | 60 | 61 | void MainWindow::startTransfer() //实现文件大小等信息的发送 62 | { 63 | localFile = new QFile(fileName); 64 | if(!localFile->open(QFile::ReadOnly)) 65 | { 66 | qDebug() << "open file error!"; 67 | return; 68 | } 69 | 70 | //文件总大小 71 | totalBytes = localFile->size(); 72 | 73 | QDataStream sendOut(&outBlock,QIODevice::WriteOnly); 74 | sendOut.setVersion(QDataStream::Qt_4_6); 75 | QString currentFileName = fileName.right(fileName.size() 76 | - fileName.lastIndexOf('/')-1); 77 | 78 | //依次写入总大小信息空间,文件名大小信息空间,文件名 79 | sendOut << qint64(0) << qint64(0) << currentFileName; 80 | 81 | //这里的总大小是文件名大小等信息和实际文件大小的总和 82 | totalBytes += outBlock.size(); 83 | 84 | sendOut.device()->seek(0); 85 | //返回outBolock的开始,用实际的大小信息代替两个qint64(0)空间 86 | sendOut<write(outBlock); 90 | 91 | //ui->clientStatusLabel->setText(tr("已连接")); 92 | outBlock.resize(0); 93 | } 94 | 95 | 96 | 97 | 98 | 99 | //更新进度条,实现文件的传送 100 | void MainWindow::updateClientProgress(qint64 numBytes) 101 | { 102 | //已经发送数据的大小 103 | bytesWritten += (int)numBytes; 104 | 105 | if(bytesToWrite > 0) //如果已经发送了数据 106 | { 107 | //每次发送loadSize大小的数据,这里设置为4KB,如果剩余的数据不足4KB, 108 | //就发送剩余数据的大小 109 | outBlock = localFile->read(qMin(bytesToWrite,loadSize)); 110 | 111 | //发送完一次数据后还剩余数据的大小 112 | bytesToWrite -= (int)tcpClient->write(outBlock); 113 | 114 | //清空发送缓冲区 115 | outBlock.resize(0); 116 | 117 | } else { 118 | localFile->close(); //如果没有发送任何数据,则关闭文件 119 | } 120 | 121 | //更新进度条 122 | ui->clientProgressBar->setMaximum(totalBytes); 123 | ui->clientProgressBar->setValue(bytesWritten); 124 | 125 | if(bytesWritten == totalBytes) //发送完毕 126 | { 127 | //ui->clientStatusLabel->setText(tr("传送文件 %1 成功") 128 | //.arg(fileName)); 129 | localFile->close(); 130 | tcpClient->close(); 131 | } 132 | } 133 | 134 | 135 | 136 | 137 | void MainWindow::displayError(QAbstractSocket::SocketError) //显示错误 138 | { 139 | qDebug() << tcpClient->errorString(); 140 | tcpClient->close(); 141 | ui->clientProgressBar->reset(); 142 | //ui->clientStatusLabel->setText(tr("客户端就绪")); 143 | ui->sendButton->setEnabled(true); 144 | } 145 | 146 | 147 | 148 | 149 | void MainWindow::on_openButton_clicked()//打开按钮 150 | { 151 | openFile(); 152 | } 153 | 154 | void MainWindow::on_sendButton_clicked()//发送按钮 155 | { 156 | send(); 157 | } 158 | -------------------------------------------------------------------------------- /TCPSender/mainwindow.h: -------------------------------------------------------------------------------- 1 | #ifndef MAINWINDOW_H 2 | #define MAINWINDOW_H 3 | 4 | #include 5 | #include 6 | 7 | namespace Ui { 8 | class MainWindow; 9 | } 10 | 11 | class MainWindow : public QMainWindow 12 | { 13 | Q_OBJECT 14 | 15 | public: 16 | explicit MainWindow(QWidget *parent = 0); 17 | ~MainWindow(); 18 | 19 | private slots: 20 | void send(); //连接服务器 21 | void startTransfer(); //发送文件大小等信息 22 | void updateClientProgress(qint64); //发送数据,更新进度条 23 | void displayError(QAbstractSocket::SocketError); //显示错误 24 | void openFile(); //打开文件 25 | 26 | private slots: 27 | void on_openButton_clicked(); 28 | 29 | void on_sendButton_clicked(); 30 | 31 | private: 32 | Ui::MainWindow *ui; 33 | QTcpSocket *tcpClient; 34 | QFile *localFile; //要发送的文件 35 | qint64 totalBytes; //数据总大小 36 | qint64 bytesWritten; //已经发送数据大小 37 | qint64 bytesToWrite; //剩余数据大小 38 | qint64 loadSize; //每次发送数据的大小 39 | QString fileName; //保存文件路径 40 | QByteArray outBlock; //数据缓冲区,即存放每次要发送的数据 41 | }; 42 | 43 | #endif // MAINWINDOW_H 44 | -------------------------------------------------------------------------------- /TCPSender/mainwindow.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | MainWindow 4 | 5 | 6 | 7 | 0 8 | 0 9 | 400 10 | 214 11 | 12 | 13 | 14 | MainWindow 15 | 16 | 17 | 18 | 19 | 20 | 30 21 | 40 22 | 311 23 | 23 24 | 25 | 26 | 27 | 0 28 | 29 | 30 | 31 | 32 | 33 | 40 34 | 150 35 | 151 36 | 17 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 30 47 | 80 48 | 101 49 | 51 50 | 51 | 52 | 53 | open 54 | 55 | 56 | 57 | 58 | 59 | 230 60 | 80 61 | 101 62 | 51 63 | 64 | 65 | 66 | send 67 | 68 | 69 | 70 | 71 | 72 | 73 | 0 74 | 0 75 | 400 76 | 25 77 | 78 | 79 | 80 | 81 | 82 | TopToolBarArea 83 | 84 | 85 | false 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | -------------------------------------------------------------------------------- /qt_camera_gui/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "mainwindow.h" 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MainWindow w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /qt_camera_gui/mainwindow.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | #include "ui_mainwindow.h" 3 | MainWindow::MainWindow(QWidget *parent) : 4 | QMainWindow(parent), 5 | ui(new Ui::MainWindow) 6 | { 7 | ui->setupUi(this); 8 | //qc 9 | 10 | qc =new qthreadcamera(this); 11 | //qf =new qthreadMessgge(this); 12 | connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(runcamera())); 13 | 14 | //message 15 | server =new QTcpServer(); 16 | server->listen(QHostAddress::Any, 8848); 17 | // emit(sendString("capture")); 18 | connect(server, SIGNAL(newConnection()), this, SLOT(newMessageClient())); 19 | connect(this, SIGNAL(sendString(QString)), ui->textEdit, SLOT(append(QString))); 20 | //connect(qf, SIGNAL(sendMessage(QString)), ui->textEdit, SLOT(append(QString))); 21 | //thread camera tongxin 22 | connect(this,SIGNAL(setencap()),qc,SLOT(setencapfunc()),Qt::QueuedConnection); 23 | connect(this,SIGNAL(setenpre()),qc,SLOT(setenprefunc()),Qt::QueuedConnection); 24 | connect(this,SIGNAL(setuncap()),qc,SLOT(setuncapfunc()),Qt::QueuedConnection); 25 | connect(this,SIGNAL(setunpre()),qc,SLOT(setunprefunc()),Qt::QueuedConnection); 26 | //connect的第五个参数Qt::QueuedConnection表示槽函数由接受信号的线程所执行,如果不加表示槽函数由发出信号的次线程执行 27 | //file 28 | 29 | ///connect(ui->pushButton_2,SIGNAL(clicked()),this,SLOT(test())); 30 | } 31 | 32 | MainWindow::~MainWindow() 33 | { 34 | delete ui; 35 | } 36 | //void MainWindow::test(){ 37 | // emit(setencap()); 38 | //} 39 | 40 | void MainWindow::runcamera(){ 41 | qc->start(); 42 | } 43 | void MainWindow::newMessageClient(){ 44 | //emit(sendString("capture")); 45 | client = server->nextPendingConnection(); 46 | connect(client, SIGNAL(readyRead()), this, SLOT(readMessage())); 47 | client->write("socket is created"); 48 | 49 | } 50 | 51 | void MainWindow::readMessage(){ 52 | emit(sendString("capture")); 53 | QString data = client->readAll(); 54 | if (data=="Capture"){ 55 | client->write("can read cap"); 56 | emit(setencap()); 57 | } 58 | if (data=="Preview"){ 59 | client->write("can read pre"); 60 | emit(setenpre()); 61 | } 62 | if (data=="StopCap"){ 63 | //将接收到的数据存放到变量中 64 | emit(setuncap()); 65 | client->write("can read stopcap"); 66 | } 67 | if (data=="Unpre"){ 68 | emit(setunpre()); 69 | client->write("can read Unpre"); 70 | } 71 | } 72 | void MainWindow::writeMessage(){ 73 | 74 | } 75 | 76 | 77 | -------------------------------------------------------------------------------- /qt_camera_gui/mainwindow.h: -------------------------------------------------------------------------------- 1 | #ifndef MAINWINDOW_H 2 | #define MAINWINDOW_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | namespace Ui { 22 | class MainWindow; 23 | } 24 | 25 | class MainWindow : public QMainWindow 26 | { 27 | Q_OBJECT 28 | 29 | public: 30 | explicit MainWindow(QWidget *parent = 0); 31 | ~MainWindow(); 32 | signals: 33 | void sendString(QString); 34 | void setencap(); 35 | void setenpre(); 36 | void setuncap(); 37 | void setunpre(); 38 | 39 | public slots: 40 | void runcamera(); 41 | void readMessage(); 42 | void writeMessage(); 43 | void newMessageClient(); 44 | //void test(); 45 | 46 | private: 47 | Ui::MainWindow *ui; 48 | QTcpServer *server; 49 | QTcpSocket *client; 50 | 51 | // QFile file;//文件对象 52 | // QString fileName;//文件名 53 | // qint64 fileSize;//文件大小 54 | // qint64 sendSize;//已经发送文件的大小 55 | 56 | QTimer timer; 57 | //QThread *qm; 58 | // QThread *qf; 59 | QThread *qc; 60 | 61 | 62 | }; 63 | 64 | #endif // MAINWINDOW_H 65 | -------------------------------------------------------------------------------- /qt_camera_gui/mainwindow.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | MainWindow 4 | 5 | 6 | 7 | 0 8 | 0 9 | 400 10 | 146 11 | 12 | 13 | 14 | MainWindow 15 | 16 | 17 | 18 | 19 | 20 | 260 21 | 0 22 | 98 23 | 71 24 | 25 | 26 | 27 | start 28 | 29 | 30 | 31 | 32 | 33 | 50 34 | 0 35 | 181 36 | 78 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 0 45 | 0 46 | 400 47 | 25 48 | 49 | 50 | 51 | 52 | 53 | TopToolBarArea 54 | 55 | 56 | false 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /qt_camera_gui/qt_camera_gui: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ccycong/qt/bb7f0e5d97609a144de2bc28e13c7cb1196234f2/qt_camera_gui/qt_camera_gui -------------------------------------------------------------------------------- /qt_camera_gui/qt_camera_gui.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2019-07-13T05:40:21 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui 8 | QT += core gui network 9 | QT += network 10 | TARGET = qt_camera_gui 11 | TEMPLATE = app 12 | 13 | 14 | SOURCES += main.cpp\ 15 | mainwindow.cpp \ 16 | qthreadmessgge.cpp \ 17 | qthreadfile.cpp \ 18 | qthreadcamera.cpp 19 | 20 | HEADERS += mainwindow.h \ 21 | qthreadmessgge.h \ 22 | qthreadfile.h \ 23 | qthreadcamera.h 24 | 25 | FORMS += mainwindow.ui 26 | -------------------------------------------------------------------------------- /qt_camera_gui/qthreadcamera.cpp: -------------------------------------------------------------------------------- 1 | #include "qthreadcamera.h" 2 | 3 | qthreadcamera::qthreadcamera(QObject *parent) : 4 | QThread(parent) 5 | { 6 | } 7 | #define CLEAR(x) memset(&(x), 0, sizeof(x)) 8 | extern "C" void camera(void); 9 | extern "C" void errno_exit(const char *s); 10 | extern "C" int xioctl(int fd, int request, void *arg); 11 | extern "C" inline int clip(int value, int min, int max); 12 | extern "C" void image_save(const void *p, int len); 13 | extern "C" void process_image(const void *p); 14 | extern "C" int read_frame(void); 15 | extern "C" void run1(void); 16 | extern "C" void stop_capturing(void); 17 | extern "C" void start_capturing(void); 18 | extern "C" void uninit_device(void); 19 | extern "C" void init_mmap(void); 20 | extern "C" void init_device(void); 21 | extern "C" void close_device(void); 22 | extern "C" void open_device(void); 23 | 24 | struct buffer 25 | { 26 | void *start; 27 | size_t length; 28 | }; //定义缓冲区结构体 29 | //缓冲区用来临时存放视频的数据 30 | // 31 | static int enpre =0; 32 | static int encap =0; 33 | static const char *dev_name; // 设备名称,可根据设备名打开设备 34 | static int fd = -1; 35 | struct buffer *buffers = NULL; 36 | static unsigned int n_buffers = 0; 37 | static int time_in_sec_capture = 5; 38 | static int fbfd = -1; 39 | static struct fb_var_screeninfo vinfo; 40 | static struct fb_fix_screeninfo finfo; 41 | static char *fbp = NULL; 42 | static long screensize = 0; 43 | // 44 | 45 | // 46 | void qthreadcamera::setencapfunc(){ 47 | encap = 1; 48 | } 49 | void qthreadcamera::setuncapfunc(){ 50 | encap = 2; 51 | } 52 | void qthreadcamera::setenprefunc(){ 53 | enpre =1; 54 | } 55 | void qthreadcamera::setunprefunc(){ 56 | enpre =0; 57 | } 58 | 59 | void qthreadcamera::run() 60 | { 61 | //connect(this,SIGNAL(setencap()),qc,); 62 | dev_name = "/dev/video0"; 63 | 64 | open_device(); 65 | 66 | init_device(); 67 | 68 | start_capturing(); 69 | 70 | run1(); 71 | 72 | stop_capturing(); 73 | 74 | uninit_device(); 75 | 76 | close_device(); 77 | 78 | exit(EXIT_SUCCESS); 79 | 80 | //return 0; 81 | } 82 | 83 | void errno_exit(const char *s) // 错误退出,并打印错误信息 84 | { 85 | fprintf(stderr, "%s error %d, %s\n", s, errno, strerror(errno)); 86 | exit(EXIT_FAILURE); 87 | } 88 | 89 | int xioctl(int fd, int request, void *arg) // 根据参数对设备进行控制 90 | { 91 | int r; 92 | do 93 | r = ioctl(fd, request, arg); 94 | while (-1 == r && EINTR == errno); 95 | return r; 96 | } 97 | //控制ioctl函数使用不同的参数 98 | 99 | inline int clip(int value, int min, int max) 100 | { 101 | return (value > max ? max : value < min ? min : value); 102 | } 103 | //进行转换时候的函数 104 | void image_save(const void *p, int len) 105 | { 106 | static int unclose = 1; 107 | static int count = 0; 108 | static FILE *file; 109 | 110 | // 111 | if (encap ==2){ 112 | count=0; 113 | } 114 | // 115 | if (encap ==1&&count == 0) 116 | { 117 | printf("file wile open"); 118 | file = fopen("camout.yuv", "wb"); 119 | printf("file is open "); 120 | //emit(); 121 | } 122 | if (encap ==1&&count < 500) 123 | { 124 | fwrite(p, 1, len, file); 125 | count++; 126 | } 127 | if (encap ==1&&count == 500) 128 | { 129 | if (unclose != 0){ 130 | printf("sava completed"); 131 | unclose = fclose(file); 132 | //encap == 2 133 | // emit 134 | //count =0; 135 | } 136 | } 137 | if (encap ==2){ //count != 0; 138 | if (unclose != 0){ 139 | printf("abort cap"); 140 | unclose = fclose(file); 141 | //count = 0 142 | //emit 143 | } 144 | } 145 | } 146 | /* 147 | 不管预览在哪里,反正点击开始采集,就执行一系列初始化 148 | 把保存与不保存的功能单独的摘出来 加上500M的功能 149 | 把获得命令行的函数给改了 150 | 不获取命令行了 直接设置设备名称 151 | 不设置具体的时间,时间无限 用一个全局变量的修改行为来响应停止预览 152 | */ 153 | void process_image(const void *p) 154 | { // 图像处理 155 | 156 | //ConvertYUVToRGB32 157 | 158 | //unsigned 159 | char *in = (char *)p; 160 | int width = 320; 161 | int height = 240; 162 | int istride = width * 2; 163 | int x, y, j; 164 | int y0, u, y1, v, r, g, b, c, d, e; 165 | long location = 0; 166 | for (y = 10; y < height + 10; ++y) 167 | { 168 | for (j = 0, x = 10; j < width * 2; j += 4, x += 2) 169 | { 170 | location = (x + vinfo.xoffset) * (vinfo.bits_per_pixel / 8) + 171 | (y + vinfo.yoffset) * finfo.line_length; 172 | 173 | y0 = in[j] - 16; 174 | u = in[j + 1] - 128; 175 | y1 = in[j + 2] - 16; 176 | v = in[j + 3] - 128; 177 | c = y0; 178 | d = u; 179 | e = v; 180 | 181 | r = clip((298 * c + 409 * e + 128) >> 8, 0, 255); 182 | g = clip((298 * c - 100 * d - 208 * e + 128) >> 8, 0, 255); 183 | b = clip((298 * c + 516 * d + 128) >> 8, 0, 255); 184 | 185 | fbp[location + 0] = b; 186 | fbp[location + 1] = g; 187 | fbp[location + 2] = r; 188 | fbp[location + 3] = 255; 189 | 190 | c = y1; 191 | 192 | r = clip((298 * c + 409 * e + 128) >> 8, 0, 255); 193 | g = clip((298 * c - 100 * d - 208 * e + 128) >> 8, 0, 255); 194 | b = clip((298 * c + 516 * d + 128) >> 8, 0, 255); 195 | 196 | fbp[location + 4] = b; 197 | fbp[location + 5] = g; 198 | fbp[location + 6] = r; 199 | fbp[location + 7] = 255; 200 | } 201 | in += istride; 202 | } 203 | } 204 | 205 | int read_frame(void) // 对图像帧进行读取 206 | { 207 | struct v4l2_buffer buf; //这是视频接口V4L2中相关文件定义的,在videodev2.h中 208 | //unsigned int i; 209 | 210 | CLEAR(buf); 211 | buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; // 获取格式 212 | buf.memory = V4L2_MEMORY_MMAP;//内存映射的地址 213 | 214 | if (-1 == xioctl(fd, VIDIOC_DQBUF, &buf)) 215 | { 216 | switch (errno) 217 | { 218 | case EAGAIN: 219 | return 0; 220 | case EIO: 221 | 222 | default: 223 | errno_exit("VIDIOC_DQBUF"); 224 | } 225 | } 226 | 227 | assert(buf.index < n_buffers); 228 | // printf("v4l2_pix_format->field(%d)\n", buf.field); 229 | //assert (buf.field ==V4L2_FIELD_NONE); 230 | if (enpre==1){ 231 | process_image(buffers[buf.index].start); 232 | } 233 | if (encap==1||encap==2){ 234 | image_save(buffers[buf.index].start, buffers[buf.index].length); 235 | } 236 | if (-1 == xioctl(fd, VIDIOC_QBUF, &buf)) 237 | errno_exit("VIDIOC_QBUF"); 238 | return 1; 239 | } 240 | //将视频帧进行转换 YUVtoRGB32 241 | //将buffers的数据进行保存 242 | 243 | void run1(void) //循环采集 244 | { 245 | // if (encap==0){ 246 | // } 247 | //unsigned int count; 248 | int frames; 249 | frames = 30 * time_in_sec_capture; 250 | 251 | while (frames-- > 0) 252 | { 253 | for (;;) 254 | { 255 | fd_set fds; 256 | struct timeval tv; //时间结构体 257 | int r; 258 | FD_ZERO(&fds); //清零 259 | FD_SET(fd, &fds); 260 | 261 | tv.tv_sec = 2; 262 | tv.tv_usec = 0; 263 | 264 | r = select(fd + 1, &fds, NULL, NULL, &tv); //等待一帧图像采集完成,超时定为2秒 265 | 266 | if (-1 == r) 267 | { 268 | if (EINTR == errno) 269 | continue; 270 | errno_exit("select"); 271 | } 272 | 273 | if (0 == r) 274 | { 275 | fprintf(stderr, "select timeout\n"); 276 | exit(EXIT_FAILURE); 277 | } 278 | 279 | if (read_frame()) //读取一帧图像 280 | break; 281 | /*再循环*/ 282 | } 283 | frames++; //for循环,如果不成功就继续循环但是要有超时的判断 284 | } 285 | } 286 | 287 | void stop_capturing(void) //终止采集 288 | { 289 | enum v4l2_buf_type type; 290 | 291 | type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 292 | if (-1 == xioctl(fd, VIDIOC_STREAMOFF, &type)) 293 | errno_exit("VIDIOC_STREAMOFF"); 294 | } 295 | 296 | void start_capturing(void) //开始采集 297 | { 298 | unsigned int i; 299 | enum v4l2_buf_type type; 300 | 301 | for (i = 0; i < n_buffers; ++i) 302 | { 303 | struct v4l2_buffer buf; 304 | CLEAR(buf); 305 | 306 | buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 307 | buf.memory = V4L2_MEMORY_MMAP; 308 | buf.index = i; 309 | 310 | if (-1 == xioctl(fd, VIDIOC_QBUF, &buf)) 311 | errno_exit("VIDIOC_QBUF"); 312 | } 313 | //缓冲队列 314 | 315 | type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 316 | 317 | if (-1 == xioctl(fd, VIDIOC_STREAMON, &type)) 318 | errno_exit("VIDIOC_STREAMON"); 319 | } 320 | 321 | void uninit_device(void) //卸载设备 322 | { 323 | unsigned int i; 324 | 325 | for (i = 0; i < n_buffers; ++i) 326 | if (-1 == munmap(buffers[i].start, buffers[i].length)) 327 | errno_exit("munmap"); 328 | 329 | if (-1 == munmap(fbp, screensize)) 330 | { 331 | printf(" Error: framebuffer device munmap() failed.\n"); 332 | exit(EXIT_FAILURE); 333 | } 334 | free(buffers); 335 | } 336 | 337 | void init_mmap(void) //初始化内存映射 338 | { 339 | struct v4l2_requestbuffers req; 340 | //申请帧缓冲 341 | 342 | //mmap framebuffer 343 | fbp = (char *)mmap(NULL, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fbfd, 0); 344 | // int fbpint =fbp; 345 | // if (fbpint == -1) 346 | // { 347 | // printf("Error: failed to map framebuffer device to memory.\n"); //错误:无法映射到存储设备 348 | // exit(EXIT_FAILURE); 349 | // } 350 | memset(fbp, 0, screensize); 351 | CLEAR(req); 352 | 353 | req.count = 4; 354 | req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 355 | req.memory = V4L2_MEMORY_MMAP; 356 | 357 | if (-1 == xioctl(fd, VIDIOC_REQBUFS, &req)) 358 | { 359 | if (EINVAL == errno) 360 | { 361 | fprintf(stderr, "%s does not support memory mapping\n", dev_name); //...不支持内存映射 362 | exit(EXIT_FAILURE); 363 | } 364 | else 365 | { 366 | errno_exit("VIDIOC_REQBUFS"); 367 | } 368 | } 369 | 370 | if (req.count < 4) 371 | { //if (req.count < 2) 372 | fprintf(stderr, "Insufficient buffer memory on %s\n", dev_name); //缓冲内存不足 373 | exit(EXIT_FAILURE); 374 | } 375 | 376 | buffers = (buffer *)calloc(req.count, sizeof(*buffers)); 377 | //c convert to c++ 378 | 379 | if (!buffers) 380 | { 381 | fprintf(stderr, "Out of memory\n"); //内存不足 382 | exit(EXIT_FAILURE); 383 | } 384 | //将申请到的帧缓冲映射到用户空间,这样就可以直接操作采集到的帧了,而不必去复制 385 | for (n_buffers = 0; n_buffers < req.count; ++n_buffers) 386 | { 387 | struct v4l2_buffer buf; 388 | 389 | CLEAR(buf); 390 | 391 | buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 392 | buf.memory = V4L2_MEMORY_MMAP; 393 | buf.index = n_buffers; 394 | 395 | if (-1 == xioctl(fd, VIDIOC_QUERYBUF, &buf)) 396 | errno_exit("VIDIOC_QUERYBUF"); 397 | 398 | buffers[n_buffers].length = buf.length; 399 | //转换成相对地址:内核-->用户空间.offset:没帧相对及地址的偏移 400 | buffers[n_buffers].start = mmap(NULL, buf.length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, buf.m.offset); 401 | 402 | if (MAP_FAILED == buffers[n_buffers].start) 403 | errno_exit("mmap"); 404 | } 405 | } 406 | 407 | void init_device(void) //初始化设备 408 | { 409 | struct v4l2_capability cap; 410 | struct v4l2_cropcap cropcap; 411 | struct v4l2_crop crop; 412 | struct v4l2_format fmt; 413 | //unsigned int min; 414 | 415 | // Get fixed screen information 416 | //得到固定屏幕的信息 417 | if (-1 == xioctl(fbfd, FBIOGET_FSCREENINFO, &finfo)) 418 | { 419 | printf("Error reading fixed information.\n"); 420 | exit(EXIT_FAILURE); 421 | } 422 | 423 | // Get variable screen information 424 | //得到可变屏幕的信息 425 | if (-1 == xioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo)) 426 | { 427 | printf("Error reading variable information.\n"); 428 | exit(EXIT_FAILURE); 429 | } 430 | 431 | screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8; 432 | //输出可变屏幕和固定屏幕的信息 433 | printf("vinfo: xoffset:%d yoffset:%d bits_per_pixel:%d xres:%d yres:%d\n", vinfo.xoffset, vinfo.yoffset, vinfo.bits_per_pixel, vinfo.xres, vinfo.yres); 434 | //printf("finfo: line_length:%d screensize:%d\n", finfo.line_length, screensize); 435 | 436 | //获取camere的信息,复制到cap中 437 | if (-1 == xioctl(fd, VIDIOC_QUERYCAP, &cap)) 438 | { 439 | 440 | if (EINVAL == errno) 441 | { 442 | fprintf(stderr, "%s is no V4L2 device\n", dev_name); 443 | exit(EXIT_FAILURE); 444 | } 445 | else 446 | { 447 | errno_exit("VIDIOC_QUERYCAP"); 448 | } 449 | } 450 | 451 | printf("cap.capabilities = %d\n", cap.capabilities); 452 | 453 | if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) 454 | { 455 | fprintf(stderr, "%s is no video capture device\n", dev_name); 456 | exit(EXIT_FAILURE); 457 | } 458 | 459 | if (!(cap.capabilities & V4L2_CAP_STREAMING)) 460 | { 461 | fprintf(stderr, "%s does not support streaming i/o\n", dev_name); //#define V4L2_CAP_STREAMING 0x04000000 /* streaming I/O ioctls 不支持流式输入输出*/ 462 | exit(EXIT_FAILURE); 463 | } 464 | 465 | CLEAR(cropcap); 466 | 467 | cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 468 | 469 | //VIDIOC_CROPCAP:查询驱动的修剪能力 470 | if (0 == xioctl(fd, VIDIOC_CROPCAP, &cropcap)) 471 | { 472 | crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 473 | crop.c = cropcap.defrect; 474 | 475 | if (-1 == xioctl(fd, VIDIOC_S_CROP, &crop)) 476 | { 477 | switch (errno) 478 | { 479 | case EINVAL: 480 | break; 481 | default: 482 | break; 483 | } 484 | } 485 | } 486 | else 487 | { 488 | } 489 | 490 | CLEAR(fmt); 491 | /*采集信号的type width height 帧格式*/ 492 | fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 493 | fmt.fmt.pix.width = 320; 494 | fmt.fmt.pix.height = 240; 495 | fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV; //V4L2_PIX_FMT_JPEG;//V4L2_PIX_FMT_YUYV; 496 | 497 | // fmt.fmt.pix.field = V4L2_FIELD_INTERLACED; 498 | 499 | //VIDIOC_S_FMT:设置当前驱动的频捕获格式 500 | if (-1 == xioctl(fd, VIDIOC_S_FMT, &fmt)) 501 | errno_exit("VIDIOC_S_FMT"); 502 | 503 | init_mmap(); 504 | } 505 | 506 | void close_device(void) //关闭设备 507 | { 508 | if (-1 == close(fd)) 509 | errno_exit("close"); 510 | fd = -1; 511 | close(fbfd); 512 | } 513 | 514 | void open_device(void) //打开设备 515 | { 516 | struct stat st; 517 | 518 | if (-1 == stat(dev_name, &st)) 519 | { 520 | fprintf(stderr, "Cannot identify '%s': %d, %s\n", dev_name, errno, strerror(errno)); 521 | exit(EXIT_FAILURE); //无法识别 522 | } 523 | 524 | if (!S_ISCHR(st.st_mode)) 525 | { 526 | fprintf(stderr, "%s is no device\n", dev_name); 527 | exit(EXIT_FAILURE); 528 | } 529 | 530 | //open framebuffer 帧缓冲区 531 | fbfd = open("/dev/fb0", O_RDWR); 532 | if (fbfd == -1) 533 | { 534 | printf("Error: cannot open framebuffer device.\n"); 535 | exit(EXIT_FAILURE); 536 | } 537 | 538 | //open camera 539 | fd = open(dev_name, O_RDWR | O_NONBLOCK, 0); 540 | 541 | if (-1 == fd) 542 | { 543 | fprintf(stderr, "Cannot open '%s': %d, %s\n", dev_name, errno, strerror(errno)); 544 | exit(EXIT_FAILURE); 545 | } 546 | } 547 | -------------------------------------------------------------------------------- /qt_camera_gui/qthreadcamera.h: -------------------------------------------------------------------------------- 1 | #ifndef QTHREADCAMERA_H 2 | #define QTHREADCAMERA_H 3 | #include 4 | #include 5 | extern "C" { 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | } 23 | class qthreadcamera :public QThread 24 | { 25 | Q_OBJECT 26 | public: 27 | explicit qthreadcamera(QObject *parent = 0); 28 | void run(); 29 | //signals: 30 | //void setencap(); 31 | public slots: 32 | void setencapfunc(); 33 | void setenprefunc(); 34 | void setuncapfunc(); 35 | void setunprefunc(); 36 | private: 37 | QString strName; 38 | }; 39 | #endif // QTHREADCAMERA_H 40 | //void MainWindow::closeMessageThread(){ 41 | 42 | // if (qm->isRunning()){ 43 | // qm->quit(); 44 | // } 45 | // if(!(qm->isRunning())){ 46 | 47 | // ui->textEdit->append("MessageThread is quit"); 48 | // } 49 | //} 50 | -------------------------------------------------------------------------------- /qt_camera_gui/qthreadfile.cpp: -------------------------------------------------------------------------------- 1 | //#include "qthreadfile.h" 2 | 3 | //qthreadFile::qthreadFile() 4 | //{ 5 | //} 6 | -------------------------------------------------------------------------------- /qt_camera_gui/qthreadfile.h: -------------------------------------------------------------------------------- 1 | //#ifndef QTHREADFILE_H 2 | //#define QTHREADFILE_H 3 | //#include 4 | //#include 5 | //class qthreadFile :public QThread 6 | //{ 7 | // Q_OBJECT 8 | //public: 9 | // explicit qthreadFile(QObject *parent = 0); 10 | //}; 11 | 12 | //#endif // QTHREADFILE_H 13 | -------------------------------------------------------------------------------- /qt_camera_gui/qthreadmessgge.cpp: -------------------------------------------------------------------------------- 1 | //#include "qthreadmessgge.h" 2 | 3 | //qthreadMessgge::qthreadMessgge(QObject *parent) : 4 | // QThread(parent) 5 | //{ 6 | //} 7 | //void qthreadMessgge::run(){ 8 | // emit(sendMessage("run")); 9 | //} 10 | //void qthreadMessgge::setencapfunc(){ 11 | 12 | // emit(sendMessage("set")); 13 | //} 14 | -------------------------------------------------------------------------------- /qt_camera_gui/qthreadmessgge.h: -------------------------------------------------------------------------------- 1 | //#ifndef QTHREADMESSGGE_H 2 | //#define QTHREADMESSGGE_H 3 | //#include 4 | //#include 5 | //class qthreadMessgge :public QThread 6 | //{ 7 | // Q_OBJECT 8 | //public: 9 | // explicit qthreadMessgge(QObject *parent = 0); 10 | // void run(); 11 | //signals: 12 | // void sendMessage(QString); 13 | //public slots: 14 | // void setencapfunc(); 15 | //}; 16 | 17 | //#endif // QTHREADMESSGGE_H 18 | -------------------------------------------------------------------------------- /qt_monitor/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "mainwindow.h" 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MainWindow w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /qt_monitor/mainwindow.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | #include "ui_mainwindow.h" 3 | 4 | MainWindow::MainWindow(QWidget *parent) : 5 | QMainWindow(parent), 6 | ui(new Ui::MainWindow) 7 | { 8 | ui->setupUi(this); 9 | client =new QTcpSocket(); 10 | 11 | //tcp messsage socket 12 | connect(ui->pushButton_3,SIGNAL(clicked()),this,SLOT(runMessage())); 13 | 14 | // write message 15 | connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(writeMessage())); 16 | connect(ui->pushButton_2,SIGNAL(clicked()),this,SLOT(writeMessage())); 17 | connect(ui->pushButton_4,SIGNAL(clicked()),this,SLOT(writeMessage())); 18 | connect(ui->pushButton_7,SIGNAL(clicked()),this,SLOT(writeMessage())); 19 | 20 | 21 | // printf 22 | connect(this, SIGNAL(sendString(QString)), ui->textEdit, SLOT(append(QString))); 23 | connect(client,SIGNAL(readyRead()),this,SLOT(readMessage())); 24 | } 25 | 26 | MainWindow::~MainWindow() 27 | { 28 | delete ui; 29 | 30 | } 31 | 32 | void MainWindow::runMessage(){ 33 | //client->connectToHost("192.168.46.230",8848); 34 | client->connectToHost(ui->lineEdit->text(),ui->lineEdit_2->text().toInt()); 35 | //client->write("ccy") 36 | } 37 | 38 | void MainWindow::Tran(){ 39 | //QString ip = ui->lineEdit->text(); 40 | //quint8 port = ui->lineEdit_2->text().toInt(); 41 | //client->connectToHost(ui->lineEdit->text(),ui->lineEdit_2->text().toInt()); 42 | 43 | 44 | //client1->connectToHost("localhost",8848); 45 | 46 | } 47 | 48 | void MainWindow::test(){ 49 | // const char na[255]="ccy"; 50 | // char pcCMD[255]; 51 | // sprintf(pcCMD,"mkdir %s",na); 52 | // system(pcCMD); 53 | 54 | // QString data =ui->lineEdit_3->text(); 55 | // client->write(data.toStdString().c_str()); 56 | 57 | 58 | } 59 | void MainWindow::writeMessage(){ 60 | QPushButton *p = (QPushButton*)sender(); 61 | QString name = p->text(); 62 | if(name == "Preview") { 63 | client->write(name.toStdString().c_str()); 64 | } 65 | if(name == "Capture") { 66 | client->write(name.toStdString().c_str()); 67 | } 68 | if(name == "StopCap") { 69 | client->write(name.toStdString().c_str()); 70 | } 71 | if(name == "Unpre") { 72 | client->write(name.toStdString().c_str()); 73 | } 74 | // QString info =ui->lineEdit_3->text(); 75 | // client->write(info.toStdString().c_str()); 76 | } 77 | void MainWindow::readMessage(){ 78 | QString message =client->readAll(); 79 | ui->textEdit->setText(message); 80 | 81 | } 82 | -------------------------------------------------------------------------------- /qt_monitor/mainwindow.h: -------------------------------------------------------------------------------- 1 | #ifndef MAINWINDOW_H 2 | #define MAINWINDOW_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | namespace Ui { 14 | class MainWindow; 15 | } 16 | 17 | class MainWindow : public QMainWindow 18 | { 19 | Q_OBJECT 20 | 21 | public: 22 | explicit MainWindow(QWidget *parent = 0); 23 | ~MainWindow(); 24 | signals: 25 | void sendString(QString); 26 | public slots: 27 | void test(); 28 | void Tran(); 29 | void runMessage(); 30 | void readMessage(); 31 | void writeMessage(); 32 | 33 | private: 34 | Ui::MainWindow *ui; 35 | QTcpSocket *client; 36 | QString info; 37 | QString message; 38 | //QString na; 39 | // QString pcCMD; 40 | QFile file; 41 | QString filename; 42 | qint32 filesize; 43 | qint32 recvsize; 44 | bool isStart; 45 | QThread *qm; 46 | QThread *qf; 47 | }; 48 | #endif // MAINWINDOW_H 49 | -------------------------------------------------------------------------------- /qt_monitor/mainwindow.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | MainWindow 4 | 5 | 6 | 7 | 0 8 | 0 9 | 472 10 | 466 11 | 12 | 13 | 14 | MainWindow 15 | 16 | 17 | 18 | 19 | 20 | 20 21 | 200 22 | 91 23 | 61 24 | 25 | 26 | 27 | Preview 28 | 29 | 30 | 31 | 32 | 33 | 120 34 | 200 35 | 91 36 | 61 37 | 38 | 39 | 40 | Capture 41 | 42 | 43 | 44 | 45 | 46 | 210 47 | 290 48 | 101 49 | 41 50 | 51 | 52 | 53 | connect 54 | 55 | 56 | 57 | 58 | 59 | 220 60 | 200 61 | 91 62 | 61 63 | 64 | 65 | 66 | StopCap 67 | 68 | 69 | 70 | 71 | 72 | 20 73 | 20 74 | 401 75 | 161 76 | 77 | 78 | 79 | 80 | 81 | 82 | 80 83 | 290 84 | 113 85 | 31 86 | 87 | 88 | 89 | 90 | 91 | 92 | 80 93 | 340 94 | 113 95 | 31 96 | 97 | 98 | 99 | 100 | 101 | 102 | 40 103 | 300 104 | 54 105 | 13 106 | 107 | 108 | 109 | IP 110 | 111 | 112 | 113 | 114 | 115 | 30 116 | 340 117 | 54 118 | 13 119 | 120 | 121 | 122 | port 123 | 124 | 125 | 126 | 127 | 128 | 330 129 | 200 130 | 91 131 | 61 132 | 133 | 134 | 135 | Unpre 136 | 137 | 138 | 139 | 140 | 141 | 142 | 0 143 | 0 144 | 472 145 | 25 146 | 147 | 148 | 149 | 150 | 151 | TopToolBarArea 152 | 153 | 154 | false 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | pushButton 164 | clicked() 165 | MainWindow 166 | Pre() 167 | 168 | 169 | 110 170 | 278 171 | 172 | 173 | 66 174 | 283 175 | 176 | 177 | 178 | 179 | pushButton_2 180 | clicked() 181 | MainWindow 182 | Cap() 183 | 184 | 185 | 252 186 | 264 187 | 188 | 189 | 176 190 | 281 191 | 192 | 193 | 194 | 195 | pushButton_3 196 | clicked() 197 | MainWindow 198 | Tran() 199 | 200 | 201 | 219 202 | 428 203 | 204 | 205 | 321 206 | 288 207 | 208 | 209 | 210 | 211 | pushButton_4 212 | clicked() 213 | MainWindow 214 | Stop() 215 | 216 | 217 | 367 218 | 278 219 | 220 | 221 | 319 222 | 107 223 | 224 | 225 | 226 | 227 | 228 | Pre() 229 | Cap() 230 | Tran() 231 | Stop() 232 | Shut() 233 | test() 234 | 235 | 236 | -------------------------------------------------------------------------------- /qt_monitor/qt_monitor: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ccycong/qt/bb7f0e5d97609a144de2bc28e13c7cb1196234f2/qt_monitor/qt_monitor -------------------------------------------------------------------------------- /qt_monitor/qt_monitor.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2019-07-13T05:48:43 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui 8 | QT += core gui network 9 | QT += network 10 | TARGET = qt_monitor 11 | TEMPLATE = app 12 | 13 | 14 | SOURCES += main.cpp\ 15 | mainwindow.cpp 16 | 17 | HEADERS += mainwindow.h 18 | 19 | FORMS += mainwindow.ui 20 | --------------------------------------------------------------------------------