Hi, I like to make a grayscale image, and I can not do it, I have the program and was wondering if it is correct.
thank you very much.
program.
#include "cliente.h"
#include <iostream>
#include <QBuffer>
#include <QImage>
#include <QFile>
#include <math.h>
#include <QLabel>
#include <QPixmap>
#include <QWidget>
#include <QLayout>
#include <QVBoxLayout>
#include <QVector>
#include <QColor>
#include<QRgb>
#include <QImageWriter>
#include<QTimer>
bool img=false;
int cont=0;
QFile archivo;
cliente::cliente()
{
socket = new QTcpSocket(this);
connect( socket, SIGNAL(connected()), this, SLOT(conectado()));
connect( socket, SIGNAL(readyRead()), this, SLOT(recv()));
connect( socket, SIGNAL(disconnected()), this, SLOT(disconnect()));
connect( socket, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(displayError(QAbstractSocket::SocketError)));
this->estaConectado =false;
}
void cliente::recv()
{
message.clear();
message.append(socket->readAll());
emit newMessage(this, "<command>", QString(message));
cont=cont+1;
if(img==true){
leeimagen(message);
}
}
void cliente::conectarse(QString host, int port)
{
this->socket->abort();
this->socket->connectToHost(host, port);
}
void cliente::conectado()
{
this->sendMessage(" \n\r");
emit connected(this, "<command>");
this->estaConectado=true;
}
void cliente::sendMessage(QString msg)
{
if (socket->isValid())
socket->write(msg.toAscii());
}
void cliente::aTrue(){img=true;}
void cliente::sendcabecera(QString host,int port )
{
static const char mydata[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x80, 0x02, 0x00, 0x00, 0xE0, 0x01, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00
};
QByteArray data = QByteArray::fromRawData(mydata, sizeof(mydata));
this->socket->connectToHost( host, port);
this->socket->write(data);
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(aTrue()));
timer->start(2);
}
void cliente::sendArchivo(QByteArray byteArray)
{
if (socket->isValid())
socket->write(byteArray);
}
void cliente::leeimagen(QByteArray bytesReceived)
{
QBuffer* buffer = new QBuffer(&bytesReceived, this);
QImage im(200, 200, QImage::Format_RGB888) ;
QImage im2(200, 200,QImage::Format_RGB888);
im.loadFromData(bytesReceived)
for(int i=0; i<200; i++)
{
for(int j=0; j<200; j++)
{
int pixl = im.pixel(i,j);
int pix = (int)qGray(pixl);
im2.setPixel(i,j, qGray(pix));
}
}
//return image.convertToFormat(f);
emit imageReceived(im2);
qDebug() <<bytesReceived.data();
return;
}
void cliente::disconnect()
{
this->estaConectado =false;
this->socket->close();
emit disconnected(this, "<command>");
img=false;
cont=0;
}
void cliente::displayError(QAbstractSocket::SocketError socketError)
{
switch (socketError)
{
case QAbstractSocket::RemoteHostClosedError:
break;
case QAbstractSocket::HostNotFoundError:
emit ConnectionError(this, tr("Conection Client"),
tr("The host was not found. Please check the "
"host name and port settings."));
break;
case QAbstractSocket::ConnectionRefusedError:
emit ConnectionError(this, tr("Conection Client"),
tr("The conection was refused by the peer. "
"Make sure the server is running, "
"and check that the host name and port "
"settings are correct."));
break;
default:
emit ConnectionError(this, tr("Conection Client"),
tr("The following error occurred: %1.")
.arg(socket->errorString()));
}
}



