日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

QT+Opencv视频文件TCP网络传输

發布時間:2023/12/9 c/c++ 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 QT+Opencv视频文件TCP网络传输 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

這幾天一直在看TCP網絡這塊,希望實現網絡攝像頭實時監控,現在實現了一個基本的demo,以后將在這個基礎上實現更多的功能,在這里我是在客戶端傳送視頻,然后在服務器顯示。
前面我還寫了一個基本的UDP消息傳輸:http://blog.csdn.net/u013812682/article/details/52149665,有興趣的可以看看交流,不多說了,看效果和代碼。

客戶端:

效果圖:

代碼:
client.h

#ifndef CLIENT_H #define CLIENT_H#include <QWidget> #include<QtNetwork> #include<QTcpServer> #include<QTcpSocket> #include<QImage> #include<QImageReader> #include<QTime> #include<QDebug> #include<QMessageBox> #include<QFileDialog> #include<opencv2/highgui/highgui.hpp> #include<opencv2/imgproc/imgproc.hpp> #include<opencv2/core/core.hpp> using namespace cv; namespace Ui { class Client; }class Client : public QWidget {Q_OBJECTpublic:explicit Client(QWidget *parent = 0);~Client();qint64 blockSize;QTcpSocket* tcpSocket;VideoCapture cap;QTimer* timer;private slots:void displayError(QAbstractSocket::SocketError);void requestNewFortune();void enableGetFortuneButton();void SendData();private:Ui::Client *ui; };#endif // CLIENT_H

client.cpp

#include "client.h" #include "ui_client.h"Client::Client(QWidget *parent) :QWidget(parent),ui(new Ui::Client) {ui->setupUi(this);ui->Fortune_ptn->setGeometry(10,30,40,40);ui->quit_ptn->setGeometry(100,30,40,40);tcpSocket=new QTcpSocket(this);timer=new QTimer(this);cap.open("test.avi");if(!cap.isOpened()){QMessageBox::information(this,tr("提示"),tr("視頻沒有打開"));}connect(ui->Fortune_ptn,SIGNAL(clicked(bool)),this,SLOT(requestNewFortune()));connect(ui->quit_ptn,SIGNAL(clicked(bool)),this,SLOT(enableGetFortuneButton()));connect(timer,SIGNAL(timeout()),this,SLOT(SendData()));connect(tcpSocket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(displayError(QAbstractSocket::SocketError))); }Client::~Client() {delete ui; }void Client::displayError(QAbstractSocket::SocketError) {qDebug()<<"傳輸失敗!"; }void Client::requestNewFortune() {timer->start(30);ui->Fortune_ptn->setEnabled(true); }void Client::enableGetFortuneButton() {ui->Fortune_ptn->setEnabled(true);tcpSocket->disconnectFromHost();timer->stop(); }void Client::SendData() {blockSize=0;tcpSocket->abort();tcpSocket->connectToHost(QHostAddress::LocalHost,8888);Mat frame;cap>>frame;cvtColor(frame,frame,CV_BGR2RGB);QByteArray byte;QBuffer buf(&byte);QImage image((unsigned const char*)frame.data,frame.cols,frame.rows,QImage::Format_RGB888);image.save(&buf,"JPEG");QByteArray ss=qCompress(byte,1);QByteArray vv=ss.toBase64();QByteArray ba;QDataStream out(&ba,QIODevice::WriteOnly);out.setVersion(QDataStream::Qt_5_6);out<<(quint64)0;out<<vv;out.device()->seek(0);out<<(quint64)(ba.size()-sizeof(quint64));tcpSocket->write(ba);ui->image_label->setPixmap(QPixmap::fromImage(image));ui->image_label->resize(image.size());update();}

服務器端:

效果圖:

代碼:
server.h

#ifndef SERVER_H #define SERVER_H#include <QWidget> #include<QTcpSocket> #include<QTcpServer> #include<QString> #include<QtNetwork> #include<QMessageBox> #include<QImage>namespace Ui { class Server; }class Server : public QWidget {Q_OBJECTpublic:explicit Server(QWidget *parent = 0);~Server();QTcpServer *tcpServer;QTcpSocket *tcpServerConnection;QStringList *fortunes;QImage *img;quint64 basize;public slots:void sendFortune();QByteArray GetPicData(QString fromPic);void DisplayError(QAbstractSocket::SocketError socketError);void ReadMyData();void ShowImage(QByteArray ba);private:Ui::Server *ui; };#endif // SERVER_H

server.cpp

#include "server.h" #include "ui_server.h"Server::Server(QWidget *parent) :QWidget(parent),ui(new Ui::Server) {ui->setupUi(this);tcpServer = new QTcpServer(this);if(!tcpServer->listen(QHostAddress::Any,8888)){QMessageBox::critical(this,tr("Fortune Server"),tr("Unable to start the server:%l.").arg(tcpServer->errorString()));close();return;}connect(tcpServer, SIGNAL(newConnection()), this, SLOT(sendFortune())); }Server::~Server() {delete ui; }void Server::sendFortune() {basize=0;tcpServerConnection = tcpServer->nextPendingConnection();connect(tcpServerConnection,SIGNAL(readyRead()),this,SLOT(ReadMyData()));connect(tcpServerConnection,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(DisplayError(QAbstractSocket::SocketError)));} QByteArray Server::GetPicData(QString fromPic) {QImage img(fromPic);QByteArray block;QBuffer buf(&block);img.save(&buf,"JPEG");//按照JPG解碼保存數據QByteArray cc = qCompress(block,1);QByteArray hh;hh=cc.toBase64();//base64數據return hh; }void Server::DisplayError(QAbstractSocket::SocketError socketError) {tcpServerConnection->close(); } void Server::ReadMyData() {QByteArray message;//存放從服務器接收到的字符串QDataStream in(tcpServerConnection);in.setVersion(QDataStream::Qt_5_6);if (basize==0){//判斷接收的數據是否有兩字節(文件大小信息)//如果有則保存到basize變量中,沒有則返回,繼續接收數據if (tcpServerConnection->bytesAvailable()<(int)sizeof(quint64)){return;}in>>basize;}//如果沒有得到全部數據,則返回繼續接收數據if (tcpServerConnection->bytesAvailable()<basize){return;}in>>message;//將接收到的數據存放到變量中ShowImage(message); }void Server::ShowImage(QByteArray ba) {QString ss=QString::fromLatin1(ba.data(),ba.size());QByteArray rc;rc=QByteArray::fromBase64(ss.toLatin1());QByteArray rdc=qUncompress(rc);QImage img;img.loadFromData(rdc);ui->image_label->setPixmap(QPixmap::fromImage(img));ui->image_label->resize(img.size());update(); }

后期我將嘗試云服務器端的服務器與本地客戶端實現,如果有同學希望一起交流可以留言給我,也可以發我郵箱,dearbigboy@163.com.

總結

以上是生活随笔為你收集整理的QT+Opencv视频文件TCP网络传输的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。