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

客户端代码: 

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_OBJECT

public:
    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

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46

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_OBJECT

public:
    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

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

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();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83

服务器端代码:


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值