Problem getting an image from a web server
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:
Code:
#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(){
dataReceived->clear();
http
= new QHttp("localhost",
80,
this);
header.setValue("Host", "localhost");
http->setHost("localhost");
http->request(header);
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();
}
//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 ?
Re: Problem getting an image from a web server
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?
Re: Problem getting an image from a web server
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.
Re: Problem getting an image from a web server
Quote:
Originally Posted by
probine
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:
Code:
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 defined. It does take some work to wrap everything in QLatin1String and friends though...)
I see you have different trials in your code commented out.
What happend when you had these lines in:
Code:
//pixmap.loadFromData(dataReceived->data());
//label->setPixmap(pixmap);
Have a nice day :-)