您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 信息化管理 > 使用Qt将一系列图片通过网络发送到客户端动态显示
使用Qt将一系列图片通过网络发送到客户端动态显示实现功能:循环读取服务器端上的10张图片并通过网络发送到客户端,客户端实现动态显示。代码如下:服务器端-----------》[cpp]viewplaincopy1./*************ServerMain.cpp***************/2.3.#includeServerStream.h4.#includeQtGui/QApplication5.6.7.intmain(intargc,char*argv[])8.{9.QApplicationa(argc,argv);10.ServerStreamstream;11.returna.exec();12.}[cpp]viewplaincopy1./**************ServerStream.h***************/2.3.#ifndefIMAGESERVER_H_4.#defineIMAGESERVER_H_5.6.#includeiostream7.#includeQtNetwork/QHostAddress8.#includeQtNetwork/QTcpSocket9.#includeQtNetwork/QTcpServer10.#includeQtGui/QImage11.#includeQtCore/QThread12.#includeQtGui/QWidget13.14.15.16.classServerStream:publicQObject{17.Q_OBJECT18.private:19.QTcpServerserver;20.QTcpSocket*socket;//socket对象21.voidinitNetWork();22.voiddelay(intlen);23.public:24.ServerStream(QObject*parent=0);25.~ServerStream();26.QByteArrayformHead(QByteArrayby);27.publicslots:28.voiddealConnection();29.};30.31.#endif[cpp]viewplaincopy1./****************ServerStream.cpp*****************/2.3.#includeServerStream.h4.#includestring5.#includeQtCore/QFile6.#includeQtCore/QThread7.8.9.ServerStream::ServerStream(QObject*parent):QObject(parent)10.{11.initNetWork();12.connect(&server,SIGNAL(newConnection()),this,SLOT(dealConnection()));13.}14.15.ServerStream::~ServerStream(){16.17.}18.19.20.//初始化网络21.voidServerStream::initNetWork(){22.std::coutinitingnetwork...std::endl;23.server.setParent(this);24.server.listen(QHostAddress::Any,8867);25.std::coutnetworkinited!std::endl;26.}27.28.//当有连接时,将图片发送过去29.voidServerStream::dealConnection(){30.intnum=0;31.longlen=0;32.socket=server.nextPendingConnection();33.for(num=1;num=10;num++){34.QFilefile(/images/+QString::number(num)+.jpg);35.file.open(QIODevice::ReadOnly);//在Qt中,文件需要打开了才能进行操作,这点与java不同36.QByteArrayby;37.by=file.readAll();38.len=by.length();//获取数据的总长度39.by.prepend(formHead(QByteArray::number((qlonglong)len)));//将要发送的数据总长度加在数据的最前10个字节中,不足的在前面补零。40.len=socket-write(by);41.socket-flush();42.std::coutlenbyteshavebeenwritten!std::endl;43.delay(7000);44.if(num==10)num=1;45.}46.}47.48.49.//将数据长度封装在10个字节的范围内,不足的在前面补零50.QByteArrayServerStream::formHead(QByteArrayby){51.intlen=by.length();52.inti=0;53.for(i=10-len;i0;i--){54.by=by.prepend('0');55.}56.returnby;57.}58.59.//线程延时不能用,只好用最原始的延时方法了60.voidServerStream::delay(intlen){61.volatileintm=0;62.volatileintn=0;63.for(m=0;mlen;m++){64.for(n=0;nlen;n++);65.}66.}运行效果截图:客户端------------------》[cpp]viewplaincopy1./**************Main.cpp******************/2.3.#includeImageStream.h4.#includeQtGui/QApplication5.6.intmain(intargc,char*argv[])7.{8.QApplicationa(argc,argv);9.ImageStream*is=newImageStream();10.is-show();11.returna.exec();12.}[cpp]viewplaincopy1./*****************ImageStream.h***********************/2.3.#ifndefIMAGESTREAM_H_4.#defineIMAGESTREAM_H_5.6.#includeQtGui/QWidget7.#includeQtGui/QLabel8.#includeiostream9.#includeQtGui/QPalette10.#includeQtNetwork/QHostAddress11.#includeQtNetwork/QTcpSocket12.13.14.15.classImageStream:publicQWidget{16.Q_OBJECT17.private:18.QHostAddresshostAddress;//主机地址19.QTcpSocketclient;//socket对象20.QLabel*ql_show;21.QByteArrayimageData;//存放接收到的图片数据的QByteArray22.boolhasReadHead;//是否接收到了当前所接收图片的第一个数据包。23.longavalibleNum;24.voidinitNetWork();25.public:26.ImageStream(QWidget*qw=0);27.~ImageStream();28.voidchangeFace();29.publicslots:30.voidgetSocketImage();//获取从服务器端传来的图片数据31.32.};33.34.#endif[cpp]viewplaincopy1./********************ImageStream.cpp***********************/2.3.#includeImageStream.h4.#includeQtCore/QFile5.#includeQtCore/QByteArray6.7.ImageStream::ImageStream(QWidget*qw):QWidget(qw){8.initNetWork();9.this-setGeometry(0,0,500,400);10.ql_show=newQLabel(this);11.ql_show-setGeometry(50,50,400,300);12.ql_show-setAutoFillBackground(true);13.hasReadHead=false;14.avalibleNum=0;15.}16.17.18.ImageStream::~ImageStream(){19.20.}21.22.23.24.25.//当有数据包发送来时,解析第一个数据包并获取所有数据包的总长度,获取的第二个以上的数据包不做此处理。26.voidImageStream::getSocketImage(){27.if(!hasReadHead){28.imageData.clear();//第一个数据包发来时,先清空存储图片数据的空间29.QByteArrayby=client.readAll();30.avalibleNum=by.left(10).toLong();//找出第一个数据包的前10个字节,由此获得数据总长度。31.by.remove(0,10);//移除前10个字节32.imageData.append(by);33.hasReadHead=true;34.if(imageData.length()=avalibleNum){//判断数据是否接收完毕35.std::coutreceiveaimage,length=avalibleNumstd::endl;36.changeFace();37.hasReadHead=false;38.}39.}else{40.QByteArrayby=client.readAll();41.imageData.append(by);42.if(imageData.length()=avalibleNum){//判断数据是否接收完毕43.std::coutreceiveaimage,length=avalibleNumstd::endl;44.changeFace();45.hasReadHead=false;46.}47.}48.}49.50.51.voidImageStream::changeFace(){52.QPalettepalette;53.QImageimage=QImage::fromData(imageData,jpg);54.palette.setBrush(QPalette::Background,QBrush(image));55.ql_show-setPalette(palette);56.}57.58.voidImageStream::initNetWork(){59.std::coutinitingnetworkstd::endl;60.hostAddress.setAddress(172.23.33.61);61.client.setParent(this);62.client.connectToHost(hostAddress,8867);63.connect(&client,SIGNAL(readyRead()),th
本文标题:使用Qt将一系列图片通过网络发送到客户端动态显示
链接地址:https://www.777doc.com/doc-2687104 .html