WImage from bytearray
Added by Marcelo Antunes about 5 years ago
In my wt app, images are stored on postgresdb as bytea, and i'm using a "midleware" written on qt to access DB.
Here is how i convert the image from wfileupload to QBytearray to store on DB:
QImage *image = new QImage(QString(fotoFu->spoolFileName().c_str()));
QByteArray arr = QByteArray::fromRawData((const char*)image->bits(), image->sizeInBytes());
Here is how i try to "convert" the qbytearray to an image that i can show on a wimage:
const unsigned char* begin = reinterpret_cast<unsigned char*>(arr.data());
const unsigned char* end = begin + arr.length();
std::vector<unsigned char> bufferToCompress( begin, end );
Wt::WMemoryResource *imgDisp= new Wt::WMemoryResource("image/pjpeg", bufferToCompress);
Wt::WPainter::Image imag(imgDisp->url(), 120, 120);
log("notice")<<"imgDisp: "<<imag->url();
The internal path of the page where is WFileUpload is:
http://192.168.0.160:8080/?_=/prod/new&wtd=5Oq7tyyB08gO7IcQ
But if i access this link (the link get from imag->url()):
http://192.168.0.160:8080/?_=/produtosservicos/novo&wtd=5Oq7tyyB08gO7IcQ&request=resource&resource=oizwb54&rand=2
The browser gives me the attatched error
regards
Marcelo
Replies (3)
RE: WImage from bytearray - Added by Aziz Gokhan Narin almost 3 years ago
Hello Marcelo,
Did you achieve getting image data as byte array from WImage?
RE: WImage from bytearray - Added by Marcelo Antunes almost 3 years ago
I'm also using QT with wt and did like this:
QByteArray Image::toQByteArray(std::string path){
std::ifstream file;
file.open(path,std::ios_base::binary);
if(!file.is_open())
{
return QByteArray();
}
file.seekg(0,std::ios::end);
long fileSize = file.tellg();
file.seekg(0,std::ios::beg);
std::vector< char> data((ulong)fileSize,0);
file.read((&data[0]), fileSize);
file.close();
std::vector< char> temp = std::vector<char>(data.begin(), data.end());
QByteArray imag=QByteArray();
for(uint i=0;i<temp.size();i++){
imag.append(temp.at(i));
}
return imag;
}
RE: WImage from bytearray - Added by Aziz Gokhan Narin almost 3 years ago
Hello again,
Thank you Marcelo for your quick respond. However I do not use Qt in my project. I need native Wt solution.
Thank you again
Gokhan