// server
#include <QDialog>
#include <QWidget>
#include <QString>
#include <QThread>
#include <QtGui>
#include <unistd.h>
#include <Qt/QtNetwork>
{
Q_OBJECT
public:
ImgServer();
quint64 value;
private slots:
void streamImage();
};
ImgServer::ImgServer()
{
image
= new QImage("/tmp/someimage.jpg");
timer->setInterval(0);
timer->start();
QTimer::singleShot(0,
this,
SLOT(streamImage
()));
}
void ImgServer::streamImage()
{
for (int y=0; y < image->height(); y++)
{
sndStream << (quint16)image->height() << (quint16)image->width();
quint16 y_in = y;
sndStream << y_in;
for(int x=0; x < image->width(); x++)
{
QRgb rgb = image->pixel(x, y);
sndStream << (quint8)qRed(rgb) << (quint8)qGreen(rgb) << (quint8)qBlue(rgb);
usleep(45);
}
clientIp = "127.0.0.1";
socket->writeDatagram(sndBuffer, clientIp, 9988 );
}
}
int main(int argc, char *argv[])
{
ImgServer imageServer;
return app.exec();
}
#include "moc_ImgServer.cpp"
// server
#include <QDialog>
#include <QWidget>
#include <QString>
#include <QThread>
#include <QtGui>
#include <unistd.h>
#include <Qt/QtNetwork>
class ImgServer : public QObject
{
Q_OBJECT
public:
ImgServer();
QUdpSocket* socket;
QImage* image;
quint64 value;
private slots:
void streamImage();
};
ImgServer::ImgServer()
{
socket = new QUdpSocket(this);
image = new QImage("/tmp/someimage.jpg");
QTimer *timer = new QTimer(this);
timer->setInterval(0);
timer->start();
QTimer::singleShot(0, this, SLOT(streamImage()));
}
void ImgServer::streamImage()
{
for (int y=0; y < image->height(); y++)
{
QByteArray sndBuffer(image->width(), 0);
QDataStream sndStream(&sndBuffer, QIODevice::WriteOnly);
sndStream.setVersion(QDataStream::Qt_4_4);
sndStream << (quint16)image->height() << (quint16)image->width();
quint16 y_in = y;
sndStream << y_in;
for(int x=0; x < image->width(); x++)
{
QRgb rgb = image->pixel(x, y);
sndStream << (quint8)qRed(rgb) << (quint8)qGreen(rgb) << (quint8)qBlue(rgb);
usleep(45);
}
QHostAddress clientIp;
clientIp = "127.0.0.1";
socket->writeDatagram(sndBuffer, clientIp, 9988 );
}
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
ImgServer imageServer;
return app.exec();
}
#include "moc_ImgServer.cpp"
To copy to clipboard, switch view to plain text mode
/******************* client code *********************/
#include <QApplication>
#include <QDialog>
#include <QWidget>
#include <QString>
#include <QThread>
#include <QtGui>
#include <unistd.h>
#include <Qt/QtNetwork>
#include <QObject>
class ImgReciever
: public QLabel{
Q_OBJECT
public:
private slots:
void dataPending();
};
{
image = 0;
socket->bind(9988);
connect(socket, SIGNAL(readyRead()), this, SLOT(dataPending() ) );
}
void ImgReciever::dataPending()
{
qDebug() << "ScpImageRcvr::dataPending()";
while( socket->hasPendingDatagrams() )
{
QByteArray buffer
(socket
->pendingDatagramSize
(),
0);
socket->readDatagram(buffer.data(), buffer.size());
quint16 width, height, y;
stream >> height >> width >> y;
if ( !image )
else if( image->width() !=width || image->height() != height )
{
if(image->height()+1 == height )
qDebug() << "Image complete!";
for ( int imgHeight = 0; imgHeight < image->height()+1; imgHeight++ )
{
for ( int incomingImgWidth = 0; incomingImgWidth < width; incomingImgWidth++ )
{
if ( imgHeight == 0 )
{
quint8 red, green, blue;
stream >> red >> green >> blue;
new_image->setPixel( incomingImgWidth, imgHeight, qRgb(red, green, blue) );
}
else
{
new_image->setPixel(incomingImgWidth,
imgHeight,
image->pixel( incomingImgWidth, imgHeight-1) );
}
}
}
delete image;
image = new_image;
mirroredImage = image->mirrored(false, true);
}
myLabel.
setPixmap(QPixmap::fromImage(mirroredImage
));
myLabel.resize(900, 900);
myLabel.show();
}
}
int main(int argc, char *argv[])
{
ImgReciever rcvImgData;
return app.exec();
}
#include "moc_ImgReciever.cpp"
/******************* client code *********************/
#include <QApplication>
#include <QDialog>
#include <QWidget>
#include <QString>
#include <QThread>
#include <QtGui>
#include <unistd.h>
#include <Qt/QtNetwork>
#include <QObject>
class ImgReciever : public QLabel
{
Q_OBJECT
public:
ImgReciever(QWidget* parent=0);
QUdpSocket* socket;
QImage* image;
QGraphicsScene* scene;
QLabel myLabel;
QImage mirroredImage;
private slots:
void dataPending();
};
ImgReciever::ImgReciever(QWidget* parent) : QLabel( parent )
{
scene = new QGraphicsScene();
image = 0;
socket = new QUdpSocket(this);
socket->bind(9988);
connect(socket, SIGNAL(readyRead()), this, SLOT(dataPending() ) );
}
void ImgReciever::dataPending()
{
qDebug() << "ScpImageRcvr::dataPending()";
while( socket->hasPendingDatagrams() )
{
QByteArray buffer(socket->pendingDatagramSize(), 0);
socket->readDatagram(buffer.data(), buffer.size());
QDataStream stream(buffer);
stream.setVersion(QDataStream::Qt_4_4);
quint16 width, height, y;
stream >> height >> width >> y;
QImage new_line(width, 1, QImage::Format_RGB32);
if ( !image )
image = new QImage(new_line);
else if( image->width() !=width || image->height() != height )
{
QImage* new_image = new QImage( width, image->height()+1, QImage::Format_RGB32 );
if(image->height()+1 == height )
qDebug() << "Image complete!";
for ( int imgHeight = 0; imgHeight < image->height()+1; imgHeight++ )
{
for ( int incomingImgWidth = 0; incomingImgWidth < width; incomingImgWidth++ )
{
if ( imgHeight == 0 )
{
quint8 red, green, blue;
stream >> red >> green >> blue;
new_image->setPixel( incomingImgWidth, imgHeight, qRgb(red, green, blue) );
}
else
{
new_image->setPixel(incomingImgWidth,
imgHeight,
image->pixel( incomingImgWidth, imgHeight-1) );
}
}
}
delete image;
image = new_image;
mirroredImage = image->mirrored(false, true);
}
myLabel.setPixmap(QPixmap::fromImage(mirroredImage));
myLabel.resize(900, 900);
myLabel.show();
}
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
ImgReciever rcvImgData;
return app.exec();
}
#include "moc_ImgReciever.cpp"
To copy to clipboard, switch view to plain text mode
Thats it, sorry i took so long.
Bookmarks