简体   繁体   中英

Convert byte array to picture

Hello I try to convert a byte array to an Image but each time i get a null image, somebody could help me please ?

QImage image("p.jpg");
qDebug()<<image;
  QImage image2;
QByteArray paquet2;
QDataStream out2(&paquet2, QIODevice::WriteOnly);

out2 << image;

qDebug()<<image2.fromData(paquet2,"jpg");

qDebug 1 result: QImage(Qsize(500,500))
qDebug 2 result: QImage(Qsize(0,0))

fromData() is a static method.

Try image2.loadFromData(paquet2) or QImage::fromData(paquet2) instead.

I noticed something very strange, this code works as expected:

  QImage img("...");
  QByteArray data;
  QBuffer buff(&data);
  QDataStream out(&buff);
  out << img;
  qDebug() << QImage::fromData(data);

But it gives a warning about the IODevice not being open.

If I manually buff.open() , fromData() produces a null image again.

Without explicitly opening, the openMode() is automatically set to Unbuffered | WriteOnly Unbuffered | WriteOnly and it works, if I open() it explicitly to Unbuffered | WriteOnly Unbuffered | WriteOnly it doesn't work. Go figure...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM