首页 > 代码库 > 4.录屏软件录屏端和接收端程序
4.录屏软件录屏端和接收端程序
1 录屏端程序案例
T30DeskSend.pro |
SOURCES+=\ main.cpp\ Sender.cpp
HEADERS+=\ Sender.h
QT+=network widgets |
Sender.h |
#ifndef SENDER_H #define SENDER_H #include <QObject> #include <QUdpSocket> class Sender : public QObject { Q_OBJECT public: explicit Sender(QObject *parent = 0); QUdpSocket* _socket; void sendFrame(const QByteArray& buf); signals: public slots: void slotSendOneFrame(); }; #endif // SENDER_H |
Sender.cpp |
#include "sender.h" #include <QApplication> #include <QScreen> #include <QDesktopWidget> #include <QPixmap> #include <QBuffer> #include <QHostAddress> #include <QDateTime> Sender::Sender(QObject *parent) : QObject(parent) { _socket = new QUdpSocket; } void Sender::sendFrame(const QByteArray &buf) { int blockSize = 1024; //获得块的大小 int blockCount = buf.size() / blockSize + (buf.size() % blockSize > 0 ? 1 : 0); //qint64表示的是:typedef long long,currentMSecsSinceEpoch获得时间额毫秒值 qint64 timestamp = QDateTime::currentMSecsSinceEpoch(); for(int i = 0; i < blockCount; i++) { QByteArray block; //追加byte数组到byte数组中 block.append((char*)×tamp, sizeof(timestamp)); block.append(char(blockCount)); block.append(char(i + 1)); block += buf.mid(i * blockSize,blockSize); //向指定地址的机器上发送数据,如果成功返回的是块的大小,如果不成功返回-1 if(_socket->writeDatagram(block, block.size(), QHostAddress("127.0.0.1"), 10001) != block.size()){ qDebug() <<"send error"<<i<<blockCount; } else { qDebug() << "send block "<< i << blockCount; } } } void Sender::slotSendOneFrame() { //capture desktop QPixmap pixmap = QApplication::screens().at(0)->grabWindow(QApplication::desktop()->winId()); //pixmap.save("../desktop.jpg"); QBuffer memFile; //QBuffer.open()函数从QIODevice::open()函数重载实现 memFile.open(QIODevice::ReadWrite); pixmap.save(&memFile,"JPG"); memFile.close(); QByteArray buf = memFile.buffer(); //发送数据 sendFrame(buf); } |
main.cpp |
#include "sender.h" #include <QApplication> #include <QTimer> int main(int argc,char* argv[]) { QApplication app(argc,argv); Sender* sender = new Sender; //这个静态方法调用在给定的时间之后调用信号 QTimer::singleShot(1, sender, SLOT(slotSendOneFrame())); QTimer* timer = new QTimer; app.connect(timer, SIGNAL(timeout()), sender, SLOT(slotSendOneFrame())); timer->setInterval(1000 * 1800); //开启定时器 timer->start(); return app.exec(); } |
编写录屏软件接收端
T31DeskShow.pro |
SOURCES+=\ main.cpp\ Recver.cpp\ MyWidget.cpp
HEADERS+=\ Recver.h\ MyWidget.h
QT+=widgets gui network |
Recver.h |
#ifndef RECVER_H #define RECVER_H #include <QObject> #include <QUdpSocket> class Recver : public QObject { Q_OBJECT public: explicit Recver(QObject *parent = 0); QUdpSocket* _socket; QList<QByteArray> _blocks; qint64 _timestamp; signals: void sigFrameArrive(QByteArray ba); public slots: void slotDataArrive(); }; #endif // RECVER_H |
Recver.cpp |
#include "Recver.h" #include <QDebug> Recver::Recver(QObject *parent) : QObject(parent) { _socket = new QUdpSocket; //绑定端口 _socket->bind(10001); // _socket->setSocketOption(); //Sets the given option to the value described by value. _socket->setSocketOption(QAbstractSocket::ReceiveBufferSizeSocketOption, 1024*1024*8); QVariant var = _socket->socketOption(QAbstractSocket::ReceiveBufferSizeSocketOption); qDebug() << "recv buffer is"<< var; connect(_socket, SIGNAL(readyRead()), this, SLOT(slotDataArrive())); _timestamp = 0; } void Recver::slotDataArrive() { //如果至少一个数据包等待读取的时候,返回true,否则返回false while(_socket->hasPendingDatagrams()) { int size = _socket->pendingDatagramSize(); QByteArray buf(size,0); _socket->readDatagram(buf.data(),buf.size()); qint64 timestamp = *(qint64*)buf.data(); if(timestamp > _timestamp) { qDebug() << "timestamp > _timestamp"; _timestamp = timestamp; _blocks.clear(); } else if(timestamp < _timestamp) { qDebug() << "timestamp < _timestamp"; continue; } //emit this->sigFrameArrive(buf); _blocks.append(buf); int totalBlockCount = (uchar)_blocks.at(0).at(8); qDebug() << "recv block" << (int)totalBlockCount << (int)(uchar)buf[9]; //qDebug() << totalBlockCount << _blocks.size(); //如果块儿的大小 == 总块儿的数量 if(_blocks.size() == totalBlockCount) { qDebug() << "recy frame"; QByteArray frame; for(int i = 0; i < _blocks.size();i++) { frame += _blocks.at(i).mid(10); } _blocks.clear(); emit this->sigFrameArrive(frame); } } } |
MyWidget.h |
#ifndef MYWIDGET_H #define MYWIDGET_H #include <QWidget> class MyWidget : public QWidget { Q_OBJECT public: explicit MyWidget(QWidget *parent = 0); QByteArray _buf; void paintEvent(QPaintEvent *); signals: public slots: void slotFrameArrive(QByteArray); }; #endif // MYWIDGET_H |
MyWidget.cpp |
#include "MyWidget.h" #include <QPainter> MyWidget::MyWidget(QWidget *parent) : QWidget(parent) { } void MyWidget::slotFrameArrive(QByteArray ba) { _buf = ba; update(); } void MyWidget::paintEvent(QPaintEvent *) { QPainter p(this); QPixmap pixmap; //加载数据从buf中 pixmap.loadFromData(_buf, "JPG"); //开始绘图 p.drawPixmap(0, 0, pixmap); } |
main.cpp |
#include <QApplication> #include "MyWidget.h" #include "recver.h" int main(int argc,char* argv[]) { QApplication app(argc,argv); MyWidget w; w.show(); Recver* recver = new Recver; app.connect(recver, SIGNAL(sigFrameArrive(QByteArray)), &w, SLOT(slotFrameArrive(QByteArray))); return app.exec(); } |
运行顺序:先运行show端程序,在运行send端程序 |
178这类的表示发送的数据的大小 |
4.录屏软件录屏端和接收端程序
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。