#include "client.h"
#include <QHostAddress>
#include "mainwindow.h"
#include <QtGui>
#include <QAbstractSocket>
#include <QImage>
{
connect(socket, SIGNAL(readyRead()),this, SLOT(tcpReady()) );
}
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))
{
//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;
}
}
int Client::tcpReady()
{
qint32 cmdID, counter, metaSize, CmdNbr, min, max, value, error, width, height;
int sizereceived = 0;
int sizeAvailable = 0;
forever{
if (socket->bytesAvailable() < sizeof(qint32))
{
return 0;
}
input >> cmdID;
// We get the data response with cmdID = 100 when we send a command to get parameter value
if (cmdID == 100)
{
input >> counter >> metaSize >> CmdNbr >> min >> max >> value;
}
// We get the data response with cmdID = 101 when we send a command to set parameter value
else if (cmdID == 101)
{
input >> counter >> metaSize >> CmdNbr >> error;
}
// Here we get a streaming raw image data
else if (cmdID == 1)
{
break;
}
else
{
return 0;
}
}
// Here we read the data and construct image
input >> counter >> metaSize >> width >> height;
int pixel = width * height;
char *temp = new char[pixel];
//char *temp = 0;
int read = input.readRawData(temp, pixel);
buffer.append(temp, pixel);
delete [] temp;
int m_index = 0;
for (int i = 0; i < (pixel-1); ++i, ++m_index)
{
image.bits()[m_index] = buffer[i];
}
int cnt = image.byteCount();
bool gray = image.isGrayscale();
if( !image.isNull() )
{
int result = image.save("E:/temp1.png", "PNG",1);
}
return 1;
}
#include "client.h"
#include <QHostAddress>
#include "mainwindow.h"
#include <QtGui>
#include <QAbstractSocket>
#include <QImage>
Client::Client(QWidget *parent) :
QWidget(parent)
{
socket = new QTcpSocket(this);
connect(socket, SIGNAL(readyRead()),this, SLOT(tcpReady()) );
}
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;
}
}
int Client::tcpReady()
{
QDataStream input(socket);
input.setVersion(QDataStream::Qt_4_0);
input.setByteOrder(QDataStream::LittleEndian);
qint32 cmdID, counter, metaSize, CmdNbr, min, max, value, error, width, height;
QByteArray buffer;
int sizereceived = 0;
int sizeAvailable = 0;
forever{
if (socket->bytesAvailable() < sizeof(qint32))
{
return 0;
}
input >> cmdID;
// We get the data response with cmdID = 100 when we send a command to get parameter value
if (cmdID == 100)
{
input >> counter >> metaSize >> CmdNbr >> min >> max >> value;
}
// We get the data response with cmdID = 101 when we send a command to set parameter value
else if (cmdID == 101)
{
input >> counter >> metaSize >> CmdNbr >> error;
}
// Here we get a streaming raw image data
else if (cmdID == 1)
{
break;
}
else
{
return 0;
}
}
// Here we read the data and construct image
input >> counter >> metaSize >> width >> height;
int pixel = width * height;
char *temp = new char[pixel];
//char *temp = 0;
int read = input.readRawData(temp, pixel);
buffer.append(temp, pixel);
QImage image = QImage(width, height, QImage::Format_Indexed8);
delete [] temp;
int m_index = 0;
for (int i = 0; i < (pixel-1); ++i, ++m_index)
{
image.bits()[m_index] = buffer[i];
}
int cnt = image.byteCount();
bool gray = image.isGrayscale();
if( !image.isNull() )
{
int result = image.save("E:/temp1.png", "PNG",1);
}
return 1;
}
To copy to clipboard, switch view to plain text mode
input >> counter >> metaSize >> width >> height;
int pixel = width * height;
socket->read(image.bits(), pixel);
input >> counter >> metaSize >> width >> height;
int pixel = width * height;
QImage image = QImage(width, height, QImage::Format_Indexed8);
socket->read(image.bits(), pixel);
To copy to clipboard, switch view to plain text mode
Bookmarks