Results 1 to 6 of 6

Thread: QImage from QByteArray

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanked 86 Times in 81 Posts

    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?

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

    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...

  3. #3
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanked 86 Times in 81 Posts

    Default Re: QImage from QByteArray

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

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

    alexlpn (13th September 2019)

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,348
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    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.

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

    alexlpn (13th September 2019)

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

    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, 13:14
  2. Obtain const QImage from QByteArray
    By mcosta in forum Qt Programming
    Replies: 8
    Last Post: 27th April 2011, 10:46
  3. Streaming QImage (QByteArray, QDataStream, QBuffer)
    By knarz in forum Qt Programming
    Replies: 5
    Last Post: 17th January 2009, 23:05
  4. Constructing QImage from QBytearray
    By dbrmik in forum Newbie
    Replies: 6
    Last Post: 16th December 2008, 16:00
  5. QImage to QByteArray
    By navi1084 in forum Qt Programming
    Replies: 5
    Last Post: 15th October 2008, 10: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
  •  
Qt is a trademark of The Qt Company.