Results 1 to 6 of 6

Thread: QImage from QByteArray

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2019
    Posts
    3
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default QImage from QByteArray

    Hello, I have some problem with loading image from raw bytes.
    I've tried several options from this forum and other sources as well, but still stuck. Something really tiny shrinks from my look. Can someone please check it?

    So the code:
    Qt Code:
    1. QFile f("data.txt");
    2. if (f.open(QIODevice::ReadOnly)){
    3. qDebug()<<"File was opened";
    4. QByteArray bytes = f.readAll();
    5.  
    6. QImage image;
    7. if (image.loadFromData(bytes , "JPG")){
    8. qDebug()<<"Image was loaded";
    9. }else{
    10. qDebug()<<"Image was not loaded";
    11. }
    12. QString filename = "output.jpg";
    13. if (image.save(filename, "JPG")){
    14. qDebug()<<"Image was saved";
    15. }else{
    16. qDebug()<<"Image was not saved";
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 

    I've tried both bytes and base64Data for QImage::LoadFromData method but still can't load image properly.
    This service can be used to check bytes, so one can see that it is a real photo.

    Thanks in advice!



    After an hour I've also tried this:
    Qt Code:
    1. QFile pic("pic.jpg");
    2. if (pic.open(QIODevice::ReadOnly)){
    3. qDebug()<<"Picture was opened";
    4. QByteArray raw = pic.readAll().toBase64();
    5. QImage image;
    6. if (!image.loadFromData(QByteArray::fromBase64(raw), "JPG"))
    7. qDebug()<<"Not loaded";
    8. if (!image.save("output_pic.jpg", "JPG"))
    9. qDebug()<<"Not saved";
    10. }
    To copy to clipboard, switch view to plain text mode 

    Here I am taking real jpg file, converting it to raw base64 bytes and trying to decode it back to jpg, but loadFromData gives me false and I can't check why...

    ps. file data.txt is in the attachment (put it in zip, so it should be unpacked first)
    data.zip
    Last edited by alexlpn; 12th September 2019 at 18:56.

Similar Threads

  1. Replies: 6
    Last Post: 14th May 2014, 12:14
  2. Obtain const QImage from QByteArray
    By mcosta in forum Qt Programming
    Replies: 8
    Last Post: 27th April 2011, 09:46
  3. Streaming QImage (QByteArray, QDataStream, QBuffer)
    By knarz in forum Qt Programming
    Replies: 5
    Last Post: 17th January 2009, 22:05
  4. Constructing QImage from QBytearray
    By dbrmik in forum Newbie
    Replies: 6
    Last Post: 16th December 2008, 15:00
  5. QImage to QByteArray
    By navi1084 in forum Qt Programming
    Replies: 5
    Last Post: 15th October 2008, 09:36

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.