Reputation: 51
I am working on a GUI to display image from sensor. The GUI acts as a client while the sensor acts as a server. Once I send a command to the sensor, the sensor will send streaming raw image data (binary data) continuously to the GUI through TCP. The data will be an exact copy of the image memory with size width (1240) x height (1028) coded in U8 (unsigned 8 bit). The size of one incoming data is (1240 x 1028 x 1 byte) + 20 byte (meta header) = 1.3MByte.
My biggest obstacles are: 1.) how can I receive and store the streaming data from sensor continuously for display purpose? 2.) how can I display the raw data?
I have tried the code as below, but it is not working. Please advise:
"client.h"
#ifndef CLIENT_H
#define CLIENT_H
#include <QObject>
#include <QWidget>
#include <QTcpSocket>
class Client : public QWidget
{
Q_OBJECT
public:
explicit Client(QWidget *parent = 0);
int capture (int mode, int NBRLine);
signals:
public slots:
private:
QTcpSocket* socket;
};
#endif // CLIENT_H
"client.cpp"
#include "client.h"
#include <QHostAddress>
#include "mainwindow.h"
#include <QtGui>
#include <QAbstractSocket>
#include <QImage>
Client::Client(QWidget *parent) :
QWidget(parent)
{
socket = new QTcpSocket(this);
}
int Client::capture(int mode, int NBRLine)
{
if (socket->state() != QTcpSocket::ConnectedState)
{
socket->connectToHost("192.168.0.65", 1096);
}
/* send command to retrieve raw image data from sensor */
if(socket->waitForConnected(5000))
{
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out.setByteOrder(QDataStream::LittleEndian);
out << qint32(0) << qint32(0) << qint32(0) << qint32(1);
out << qint32(9) << qint32(1) << qint32(0) << mode << qint32(10) << qint32(2) << qint32(0) << NBRLine ;
socket->write(block);
socket->flush();
}
else
{
return false;
}
/**********************************************************/
/* to get data size of each scan through width and height in the meta header */
QDataStream input(socket);
input.setVersion(QDataStream::Qt_4_0);
input.setByteOrder(QDataStream::LittleEndian);
qint32 buffer, cmdID, counter, metaSize, width, height;
do
{
if (socket->waitForReadyRead(1000))
{
input >> cmdID;
if (cmdID == 101)
{
input >> buffer >> buffer >> buffer >> buffer;
}
}
else
{
socket->disconnectFromHost();
break;
}
} while (cmdID != 1);
input >> counter >> metaSize;
if (metaSize != 8) return false;
input >> width >> height;
quint32 datasize = width * height;
/**********************************************************/
/* Receiving streaming data which I have problem here !!!! */
while (socket->bytesAvailable() < datasize + 80) {
if (!socket->waitForReadyRead(1000)) {
socket->disconnectFromHost();
break;
}
}
QImage img;
input >> img;
if (img.isNull())
{
return 0;
}
img.save("E:/temp1");
return 1;
}
Upvotes: 3
Views: 3748
Reputation: 32635
Before you can send the contents of the buffer, you need to find a way to tell the client how much data to expect. So first of all you should send data size and after that the data.
On the server side you should send image data like :
QTcpSocket socket;
QBuffer buffer;
QImageWriter writer(&buffer, "PNG");
writer.write(QImage("myImage.png") );
QByteArray data;
QDataStream stream( &data, QIODevice::WriteOnly );
stream.setVersion( QDataStream::Qt_4_8 );
stream << (quint32)buffer.data().size();
data.append( buffer.data() );
socket.write( data );
On the client side you should connect the readyRead
signal of the socket to a slot to read data when new data receives :
connect( socket, SIGNAL(readyRead()),this, SLOT(tcpReady()) );
The image data can be read like :
void Client::tcpReady()
{
if( dataSize == 0 )
{
QDataStream stream( &socket );
stream.setVersion( QDataStream::Qt_4_8 );
if( socket.bytesAvailable() < sizeof(quint32) )
return;
stream >> dataSize;
}
if( dataSize > socket.bytesAvailable() )
return;
QByteArray array = socket.read( dataSize );
QBuffer buffer(&array);
buffer.open( QIODevice::ReadOnly );
QImageReader reader(&buffer, "PNG");
QImage image = reader.read();
if( !image.isNull() )
{
image.save("E:/temp1");
}
else
{
QDebug()<<"Invalid image received!";
}
}
Upvotes: 2