Cannot load qimage from byte array in release mode
-
In my app I receive images in
JPEG
format from Android device overQTcpSocket
as a byte array.When I run this app on my dev PC it works like a charm (both in debug and release modes).
However, I'm not able to load QImage from byte array on another PC where QT is not installed.
I included all the *.dll files in QT for my platform (Win_x64, msvc_2015_64) and also installed Visual Studio C++ Redistributable 2015. My app starts fine (doesn't give any errors that some *.dll files are missing). But QImage is empty all the time.
First I thought that byte array wasn't being received at all, so I checked its size and found that it's being received correctly.
I'm using this code to load
QImage
fromuchar
bytes:void handleImageBytes(uchar *bytes, qint64 length) { QImage image; image.loadFromData(bytes, length, "JPEG"); emit frameReady(image); }
And I receive those bytes from
QTcpSocket
:void handleReadyRead() { if(this->incomingFrameLength==0){ QString data = this->client->readAll(); this->incomingFrameLength = data.toInt(); this->client->write("ok"); qDebug()<<QString("Received frame length: %1").arg(this->incomingFrameLength); return; } if(this->incomingFrameLength!=0 && this->client->bytesAvailable()<this->incomingFrameLength) return; uchar* data = (uchar*)malloc(this->incomingFrameLength+256); qint64 len = this->client->read((char*)data, this->incomingFrameLength+256); qDebug() << QString("Received %1 bytes").arg(len); handleImageBytes(data, len); this->incomingFrameLength=0; this->client->write("received"); qDebug()<<QString("Received frame of length %1").arg(len); free(data); }
As you see, first I receive the size of the image as a length of the byte array. Then I wait for all the required bytes are received and ready in the
QTcpSocket
's buffer. Then I create pointer to the byte array with sufficient size, load incoming bytes to that array and callhandleImageBytes
method from above.This all works fine on my dev PC (windows 7 x64, Qt 5.7, msvc_2015_64). However, loading images from byte array doesn't work when I move my release code with all the required *.dll files to other PC where Qt isn't installed.
How to solve this problem?
-
@umriyaev said in Cannot load qimage from byte array in release mode:
However, loading images from byte array doesn't work when I move my release code with all the required *.dll files to other PC where Qt isn't installed.
most probably you forgot to copy also the imageplugin dlls (or coppied them to the wrong sub-directory)?
Try again with using the windeployqt tool -
@umriyaev said in Cannot load qimage from byte array in release mode:
QString data = this->client->readAll(); [...] uchar* data = (uchar*)malloc(this->incomingFrameLength+256); qint64 len = this->client->read((char*)data, this->incomingFrameLength+256);
QString stores data in UTF-16. Better not use it for binary JPEG data, especially not for checking it's length. Use QByteArray instead.
Also, why do you first read the frame length from
QString data
, but then discard that and define and read into newuchar *
data? Looks like you read same data twice for no reason.Anyway, that's off-topic. If the other device (Android) also runs Qt, I would suggest using QDataStream to read and write your QImage - then you don't have to parse the data by hand in any way, it will work automatically.
If that is not the option, then... hm. Do you get any warnings or error messages on the console when you run the application on the other (non-dev) machine? Have you deployed all dependencies (inluding image format plugins)? Try using the deployment tool to ease the process: http://doc.qt.io/qt-5/windows-deployment.html#the-windows-deployment-tool
-
@umriyaev
hi,
this looks like a big/little enianness problem.Have you checked that its the same for your DevPc and your android device?
AFAIK Windows is l-Endian, and most android arm devices are as well so this is a small chance.
But something you should keep in mind any way, because IIRC network byte order is big-endian, it can be helpful to convert any format you plan on using for data interchange to network byte order.
-
Have you included qjpeg.dll?