Results 1 to 6 of 6

Thread: QImage from QByteArray

  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.

  2. #2
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    445
    Thanked 61 Times in 57 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QImage from QByteArray

    When data.txt contains base64 encoded content, why do you encode it a second time here:
    QByteArray base64Data = bytes.toBase64();
    And when it's a jpeg - why do you tell QImage::loadFromData() that it's a PNG?

  3. #3
    Join Date
    Sep 2019
    Posts
    3
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QImage from QByteArray

    Quote Originally Posted by ChristianEhrlicher View Post
    When data.txt contains base64 encoded content, why do you encode it a second time here:

    And when it's a jpeg - why do you tell QImage::loadFromData() that it's a PNG?
    Thanks for pointing this out. I've fixed that but still the result is false...

  4. #4
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    445
    Thanked 61 Times in 57 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QImage from QByteArray

    QImage::loadFromData() needs the raw data, not base64 encoded text.

  5. The following user says thank you to ChristianEhrlicher for this useful post:

    alexlpn (13th September 2019)

  6. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,118
    Thanks
    235
    Thanked 656 Times in 646 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: QImage from QByteArray

    QImage::loadFromData() needs the raw data, not base64 encoded text.
    And so you need to give it the output from QByteArray::fromBase64(), not QByteArray::toBase64().
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  7. The following user says thank you to d_stranz for this useful post:

    alexlpn (13th September 2019)

  8. #6
    Join Date
    Sep 2019
    Posts
    3
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QImage from QByteArray

    Quote Originally Posted by d_stranz View Post
    And so you need to give it the output from QByteArray::fromBase64(), not QByteArray::toBase64().
    Quote Originally Posted by ChristianEhrlicher View Post
    QImage::loadFromData() needs the raw data, not base64 encoded text.
    Thank you mates!
    I see I've mixed several things in wrong order) Now it is clear.

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

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.