码迷,mamicode.com
首页 > 其他好文 > 详细

4.录屏软件录屏端和接收端程序

时间:2014-12-18 00:17:30      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   ar   io   color   os   sp   



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 longcurrentMSecsSinceEpoch获得时间额毫秒值
    qint64 timestamp = QDateTime::currentMSecsSinceEpoch();
    for(int i = 0; i < blockCount; i++)
    {
        QByteArray block;
        //追加byte数组到byte数组中
        block.append((char*)&timestamp, 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端程序

bubuko.com,布布扣

178这类的表示发送的数据的大小

 

 

 

4.录屏软件录屏端和接收端程序

标签:des   style   blog   http   ar   io   color   os   sp   

原文地址:http://blog.csdn.net/tototuzuoquan/article/details/41992261

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!