I m able to send and receive as well as display image .
My latest server side code is
IplImage *image=cvQueryFrame(camera);
CapturedImage = cvwidget->putImage(image);
cvwidget
->imagelabel
->setPixmap
(QPixmap::fromImage(CapturedImage
));
//For viewing captured image
CapturedImage.save(&buffer, "PNG");
buffer.close();
/* Pad it with spaces, so that it has a constant width. */
len = len.leftJustified(10, ' ', false);
/* Put the number at the start of our data array. */
data.prepend(len);
/*send QbyteArray over socket */
nWrite = gtcpSocket->write(data);
IplImage *image=cvQueryFrame(camera);
CapturedImage = cvwidget->putImage(image);
cvwidget->imagelabel->setPixmap(QPixmap::fromImage(CapturedImage)); //For viewing captured image
QByteArray data;
QBuffer buffer(&data);
buffer.open(QIODevice::WriteOnly);
CapturedImage.save(&buffer, "PNG");
buffer.close();
QByteArray len = QByteArray::number(data.length(), 10);
/* Pad it with spaces, so that it has a constant width. */
len = len.leftJustified(10, ' ', false);
/* Put the number at the start of our data array. */
data.prepend(len);
/*send QbyteArray over socket */
nWrite = gtcpSocket->write(data);
To copy to clipboard, switch view to plain text mode
My client side code for receiving and displaying image is
void MainWindow::readDatafromServer() //Slot conneted to readyRead() signal
{
qint64 nRead;
while (data.length() < 10)
{
/* Read all available data. */
// QMessageBox::critical(this,tr("first"),QString::number(received.length()));
/* Add received data to the end of our byte array. */
data += received;
/* Wait until the next packet of data arrives. */
// tcpSocket->waitForReadyRead();
}
/* Get the first ten bytes, making up our number. */
/* Remove the first ten bytes from our data array. */
data.remove(0, 10);
bool ok;
int length = str.toInt(&ok,10);
// QMessageBox::critical(this,str,QString::number(length));
// QMessageBox::critical(this,"ABytes",QString::number((int)tcpSocket->bytesAvailable()));
while (data.length() < length)
{
/* Read all available data. */
// QMessageBox::critical(this,tr("second"),QString::number(receivedB.length()));
/* Add received data to the end of our byte array. */
data += receivedB;
/* Wait until the next packet of data arrives. */
//tcpSocket->waitForReadyRead();
}
buf.setBuffer(&data);
qir.setDevice(&buf);
qir.read(&ImagePNG);
ViewObj
->ui.
label->setPixmap
(QPixmap::fromImage(ImagePNG
));
}
void MainWindow::readDatafromServer() //Slot conneted to readyRead() signal
{
qint64 nRead;
QDataStream in(tcpSocket);
in.setVersion(QDataStream::Qt_4_6);
QByteArray data;
while (data.length() < 10)
{
/* Read all available data. */
QByteArray received = tcpSocket->readAll();
// QMessageBox::critical(this,tr("first"),QString::number(received.length()));
/* Add received data to the end of our byte array. */
data += received;
/* Wait until the next packet of data arrives. */
// tcpSocket->waitForReadyRead();
}
/* Get the first ten bytes, making up our number. */
QByteArray len = data.left(10);
/* Remove the first ten bytes from our data array. */
data.remove(0, 10);
QString str( len);
bool ok;
int length = str.toInt(&ok,10);
// QMessageBox::critical(this,str,QString::number(length));
// QMessageBox::critical(this,"ABytes",QString::number((int)tcpSocket->bytesAvailable()));
while (data.length() < length)
{
/* Read all available data. */
QByteArray receivedB = tcpSocket->readAll();
// QMessageBox::critical(this,tr("second"),QString::number(receivedB.length()));
/* Add received data to the end of our byte array. */
data += receivedB;
/* Wait until the next packet of data arrives. */
//tcpSocket->waitForReadyRead();
}
QBuffer buf;
buf.setBuffer(&data);
buf.open(QIODevice::ReadOnly);
QImageReader qir;
qir.setDevice(&buf);
QImage ImagePNG;
qir.read(&ImagePNG);
ViewObj->ui.label->setPixmap(QPixmap::fromImage(ImagePNG));
}
To copy to clipboard, switch view to plain text mode
At server captures image from webcam and writes over socket every o.1 sec,Now how to use waitForReadyRead() at client side to read this continuous data from server .I tried but it freezes the GUI,If not used client receives image only one time.
Bookmarks