Qt图像udp通信等学习

上传人:平*** 文档编号:12791268 上传时间:2017-10-20 格式:DOC 页数:7 大小:73.50KB
返回 下载 相关 举报
Qt图像udp通信等学习_第1页
第1页 / 共7页
Qt图像udp通信等学习_第2页
第2页 / 共7页
Qt图像udp通信等学习_第3页
第3页 / 共7页
Qt图像udp通信等学习_第4页
第4页 / 共7页
Qt图像udp通信等学习_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《Qt图像udp通信等学习》由会员分享,可在线阅读,更多相关《Qt图像udp通信等学习(7页珍藏版)》请在金锄头文库上搜索。

1、qt4 使用 QUdpSocket 发送数据报 datagrams2013-07-26 | 分类: QT | 标签: | 浏览(0)UDP 服户端绑定广播喝小酒的网摘http:/ = new QUdpSocket(this); QByteArray datagram = ; udpSocket-writeDatagram(datagram.data(), datagram.size(), QHostAddress:Broadcast, 45454); udpSocket-bind(QHostAddress(192.168.1.104), 45454); UDP 客户端广播QUdpSocket

2、*udpsocket1 = new QUdpSocket(this);QByteArray datagram = http:/; udpsocket1-writeDatagram(datagram.data(),datagram.size(), QHostAddress:Broadcast,ui-spinBox-text().toInt();UDP 客户端发送到指定 IPQUdpSocket *udpsocket1 = new QUdpSocket(this);QByteArray datagram = http:/; udpsocket1-writeDatagram(datagram.dat

3、a(),datagram.size(), QHostAddress(192.168.1.104),ui-spinBox-text().toInt();收数据connect(udpsocket, SIGNAL(readyRead(), this, SLOT(readPendingDatagrams(); while (udpsocket-hasPendingDatagrams() QByteArray datagram; datagram.resize(udpsocket-pendingDatagramSize(); QHostAddress sender; quint16 senderPort

4、; udpsocket-readDatagram(datagram.data(), datagram.size(), &sender, &senderPort); qDebug()writeDatagram(datagram.data(),datagram.size(),QHostAddress(192.168.1.100),45454); 6. /sender-writeDatagram(datagram.data(),datagram.size(),QHostAddress:Broadcast,45454); 7. / sender-writeDatagram(datagram.data(

5、),datagram.size(),QHostAddress:LocalHost,45454); 8. delete sender; 9. 接收(服务器端监听)使用 QUdpSocket 的 bind 函数监听某个端口当监听的端口有数据到达时,QUdpSocket 的信号 readyRead()就 emit,然后在对应的槽函数里使用 QUdpSocket 的 readDatagram读取数据void QIODevice:readyRead () signalThis signal is emitted once every time new data is available for read

6、ing from the device. It will only be emitted again once new data is available, such as when a new payload of network data has arrived on your network socket, or when a new block of data has been appended to your device.readyRead() is not emitted recursively; if you reenter the event loop or call wai

7、tForReadyRead() inside a slot connected to the readyRead() signal, the signal will not be reemitted (although waitForReadyRead() may still return true).Note for developers implementing classes derived from QIODevice: you should always emit readyRead() when new data has arrived (do not emit it only b

8、ecause theres data still to be read in your buffers). Do not emit readyRead() in other conditions.cpp view plaincopy1. private: 2. QUdpSocket *receiver; 3. private slots: 4. void processPendingDatagram(); cpp view plaincopy1. receiver = new QUdpSocket(this); 2. receiver-bind(45454,QUdpSocket:ShareAd

9、dress); 3. connect(receiver,SIGNAL(readyRead(),this,SLOT(processPendingDatagram(); cpp view plaincopy1. void Widget:processPendingDatagram() /处理等待的数据报 2. 3. while(receiver-hasPendingDatagrams() /拥有等待的数据报 4. 5. QByteArray datagram; /拥于存放接收的数据报 6. datagram.resize(receiver-pendingDatagramSize(); 7. /让

10、datagram 的大小为等待处理的数据报的大小,这样才能接收到完整的数据 8. receiver-readDatagram(datagram.data(),datagram.size(); 9. /接收数据报,将其存放到 datagram 中 10. ui-label-setText(datagram); 11. /将数据报内容显示出来 12. 13. 用 wireshark 监听 xp 192.168.1.100 和虚拟机 fedora 192.168.1.103 之间的 udp 数据包,如下hello worl 192.168.1.100-fedora 192.168.1.103 (wi

11、reshark 操作:capture-Options 里选择要监视的网卡,然后点 Start。可以选择 capture/capture filters 然后选择 udp only 过滤一下)嗯,这个 QImage 的问题研究好久了,有段时间没用,忘了,已经被两次问到了,突然有点解释不清楚,我汗颜,觉得有必要重新总结下了,不然无颜对自己了。图像的数据是以字节为单位保存的,每一行的字节数必须是 4 的整数倍,不足的补 0。(因为我们使用的是 32 操作系统,因此数据是按照 32 位对齐的,所以每行的字节数必须是 4 的整数倍也就是说每行的数据位必须是 32位的整数倍。)这里是按照我的理解的,貌似错

12、了,修正一下,最近在看数据对齐,这段话先忽略了,没有删掉,是因为,想留个足迹,等我找到合适的答案再贴上来。不过,图像的数据确实是按 32 位对齐的。如果不是整数倍,则根据公式: W = ( w * bitcount + 31 )/32 * 4;注: w 是图像的宽度,bitcount 是图像的位深,即 32、24 等, 计算得到的 W 是程序中图像每行的字节数。这里讲述 QImage 的 32、24、8 位图。图像格式:QImage:Format_RGB32 ,QImage:Format_RGB888,QImage:Format_Indexed8。构造图像:(1)、QImage myImage

13、1 = QImage(filename); 根据文件名打开图像,如果图像本身是 32、24 位的,程序中图像是 32 位的,如果图像本身是 8 位、1 位的,程序中对应为 8 位、1 位。(2)、QImage myImage2 = QImage(width, height, QImage:Format_); 根据图像宽高来构造一幅图像,程序会自动根据图像格式对齐图像数据。操作图像:按照(2)的方式构造图像,在 Debug 下,如果不给图像 myImage2 初值,图像不是黑的, 但 release 下,则构造好的图像默认为黑色。好了,现在我们需要对图像数据操作,32 位图像无疑是最简单的,因为

14、它数据是对齐的。用 width 表示图像宽度,height 表示图像高度。首先熟悉几个函数:a、uchar* bits(); 可以获取图像的首地址b、int byteCount(); 图像的总字节数 c、int bytesPerLine(); 图像每行字节数1、QImage:Format_RGB32,存入格式为 B,G,R,A 对应 0,1,2,3QImage:Format_RGB888,存入格式为 R, G, B 对应 0,1,2QImage:Format_Indexed8,需要设定颜色表,QVector灰度图像颜色表设定:QVector colorTable;for(int k=0;k25

15、6;+k) colorTable.push_back( qRgb(k,k,k) );2、QImage image32 = QImage(width, height, QImage:Format_32); QImage image24 = QImage(width, height, QImage:Format_24);QImage image8 = QImage(width, height, QImage:Format_8);image8.setColorTable(colorTable);3、需要取每个像素处理,采用指针取值,行扫的方式:int lineNum_32 = 0; /行数int pixelsub_32 = 0; /像素下标uchar* imagebits_32 = image32.bits(); /获取图像首地址,32 位图uchar* imagebits24 = image24.bits();u

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号