PDA

View Full Version : Problem getting an image from a web server



probine
12th January 2007, 22:17
I want to be able to get an image from a web server. Any image, any size, any web server.

The following code compiles with no warning or problems at all:




#include <QHttp>
#include <QHttpRequestHeader>
#include <QHttpResponseHeader>
#include <QByteArray>
#include <QLabel>
#include <QImage>
#include <QString>
#include <QTextBrowser>
#include <iostream>

#include "image.h"
using namespace std;

Image::Image(){
tb = new QTextBrowser(this);
dataReceived = new QByteArray();
dataReceived->clear();
label = new QLabel(this);
http = new QHttp("localhost", 80, this);
QHttpRequestHeader header("GET", "/images/test.jpg");
header.setValue("Host", "localhost");
http->setHost("localhost");
http->request(header);

connect(http, SIGNAL(readyRead(const QHttpResponseHeader &)), this, SLOT(getLogo(const QHttpResponseHeader &)));
connect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(requestFinished(int, bool)));
//connect(http, SIGNAL(done(bool)), this, SLOT(done(bool)));

setFixedSize(300, 150);
setCentralWidget(label);
show();
}

void Image::getLogo(const QHttpResponseHeader & resp){
//QByteArray imageData(http->readAll());
//QImage image(imageData.data());
//QImage image("logo.png");
//label->setPixmap(QPixmap::fromImage(image));

cout << "The response:\n";
dataReceived->append(http->readAll());
cout << "dataReceived lenght: " << dataReceived->size() << endl;
//QString temp1 = dataReceived->data();
//cout << temp1.toStdString();
}

void Image::requestFinished(int n, bool error){
if(error){
cout << "requestFinished error\n";
}
else{
if(dataReceived->size()==0){
return;
}
cout << "requestFinished ok\n";
cout << "Image lenght: " << dataReceived->size() << endl;
site.append("<html><tr><td>a");
site.append(dataReceived->data());
site.append("</td></tr></html>");
//tb->append(dataReceived->data());
tb->append(site);
//pixmap.loadFromData(dataReceived->data());
//label->setPixmap(pixmap);

}

}

void Image::done(bool error){
cout << "DONE function, bool reported:\n";
if(!error)
cout << "no error\n";
pixmap.loadFromData(dataReceived->data());
label->setPixmap(pixmap);
tb->append(dataReceived->data());
}



For some reason I always get a corrupted image. The console also prints a message liske this:

The response:
dataReceived lenght: 1334
requestFinished ok
Image lenght: 1334
Corrupt JPEG data: 1 extraneous bytes before marker 0xd9
JPEG datastream contains no image

What is wrong ?

gfunk
12th January 2007, 22:27
What's inside of dataReceived? Is it valid html or binary data? What's the size of test.jpg? Can you access test.jpg with a regular browser?

probine
12th January 2007, 22:34
Yes, I can access the image from a regular browser.

The size of the image in my local directory is exactly the same size of the one reported in the console.

Inside the QByteArray I keep all the data I received from the QHttp response, so my guess is that it is binary data.

camel
12th January 2007, 23:05
What is wrong ?

That is a very good question ;-)


I sadly do not see what type "site" is. But it could be that you are bitten by an implicit conversion of QByteArray to QString (including the complete messing up of any binary data in it) in the lines:


site.append("<html><tr><td>a");
site.append(dataReceived->data());
site.append("</td></tr></html>");


What parameter type does site.append() accept?
(Since I do hate these kind of errors, I have always QT_NO_CAST_FROM_ASCII (http://doc.trolltech.com/4.2/qstring.html#QT_NO_CAST_FROM_ASCII) defined. It does take some work to wrap everything in QLatin1String (http://doc.trolltech.com/4.2/qlatin1string.html) and friends though...)

I see you have different trials in your code commented out.
What happend when you had these lines in:


//pixmap.loadFromData(dataReceived->data());
//label->setPixmap(pixmap);



Have a nice day :-)