Linux实验课 使用QT实现Windows端监视Linux端屏幕
注意工程目录路径不允许有中文出现
【Windows端】RemoteController
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QUdpSocket>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
signals:
void signalGetData(const QByteArray& data);
private slots:
void onHandleData(const QByteArray& data);
protected slots:
void readPendingDatagrams();
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
QUdpSocket *udpSocket;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QPixmap>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
udpSocket = new QUdpSocket(this);
udpSocket->bind(QHostAddress::Any, 7755);
connect(udpSocket, SIGNAL(readyRead()),
this, SLOT(readPendingDatagrams()));
connect(this, SIGNAL(signalGetData(const QByteArray&)),
this, SLOT(onHandleData(const QByteArray&)));
}
MainWindow::~MainWindow()
{
delete ui;
delete udpSocket;
}
void MainWindow::onHandleData(const QByteArray& data)
{
QPixmap pixmap;
QByteArray udata = qUncompress(data);
pixmap.loadFromData(udata);
ui->label->setPixmap(
pixmap.scaled(
ui->label->size(),
Qt::KeepAspectRatio,
Qt::SmoothTransformation));
}
void MainWindow::readPendingDatagrams()
{
while (udpSocket->hasPendingDatagrams()) {
QByteArray datagram;
datagram.resize(udpSocket->pendingDatagramSize());
QHostAddress sender;
quint16 senderPort;
udpSocket->readDatagram(datagram.data(), datagram.size(),
&sender, &senderPort);
qDebug()<<"receive data "<<datagram.size();
emit signalGetData(datagram);
}
}
void MainWindow::on_pushButton_clicked()
{
QByteArray datagram;
QHostAddress sender;
quint16 senderPort;
datagram.append("I am windows");
sender.setAddress("192.168.121.133");
senderPort = 8888;
udpSocket->writeDatagram(datagram, sender, senderPort);
}
mainwindow.ui
【Linux端】UdpScreen
main.cpp
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QUdpSocket>
#include <QPixmap>
#include <linux/input.h>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
protected:
void Screenshot();
void SendData();
protected slots:
void readPendingDatagrams();
void Run();
private:
Ui::MainWindow *ui;
QUdpSocket *udpSocket;
QPixmap originalPixmap;
private slots:
void on_pushButton_clicked();
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QtGui>
#include <QTimer>
#include <fcntl.h>
#include <string.h>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
udpSocket = new QUdpSocket(this);
udpSocket->bind(QHostAddress::Any, 8888);
connect(udpSocket, SIGNAL(readyRead()),
this, SLOT(readPendingDatagrams()));
}
MainWindow::~MainWindow()
{
delete ui;
delete udpSocket;
}
void MainWindow::readPendingDatagrams()
{
while (udpSocket->hasPendingDatagrams()) {
QByteArray datagram;
datagram.resize(udpSocket->pendingDatagramSize());
QHostAddress sender;
quint16 senderPort;
udpSocket->readDatagram(datagram.data(), datagram.size(),
&sender, &senderPort);
qDebug()<<"receive data "<<datagram;
}
}
void MainWindow::Screenshot()
{
qDebug()<<"Screenshot";
originalPixmap = QPixmap();
originalPixmap = QPixmap::grabWindow(QApplication::desktop()->winId());
}
void MainWindow::SendData()
{
QSize size(600, 450);
originalPixmap = originalPixmap.scaled(size);
qDebug()<<"screen to bytes";
QByteArray bytes;
QBuffer buffer(&bytes);
buffer.open(QIODevice::ReadWrite);
originalPixmap.save(&buffer, "JPEG"); // writes pixmap into bytes in PNG format
qDebug()<<"all bytes ori "<<bytes.size();
bytes = qCompress(bytes, 9);
QHostAddress sender;
quint16 senderPort;
sender.setAddress("192.168.121.1");
senderPort = 7755;
qDebug()<<"all bytes comp "<<bytes.size();
qint64 res = udpSocket->writeDatagram(
bytes, sender, senderPort);
qDebug()<<"send bytes "<<res;
// int send_len = 1024;
// int len = bytes.size();
// int pos = 0;
// while(len > 0)
// {
// if(len < send_len)
// {
// send_len = len;
// }
// qDebug()<<send_len;
// udpSocket->writeDatagram(bytes.mid(pos, send_len), sender, senderPort);
// len -= send_len;
// pos += send_len;
// }
}
void MainWindow::Run()
{
Screenshot();
SendData();
}
void MainWindow::on_pushButton_clicked()
{
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(Run()));
timer->start(1000);
}
mainwindow.ui
附录
- 工程代码
链接:http://pan.baidu.com/s/1sllVaod 密码:931q