Results 1 to 9 of 9

Thread: convert QPixmap to std::vector<unsigned char>

  1. #1
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default convert QPixmap to std::vector<unsigned char>

    I get the following when I grab a screen shot:
    Qt Code:
    1. QPixmap originalPixmap = QPixmap::grabWidget(this);
    To copy to clipboard, switch view to plain text mode 

    I need to convert originalPixmap to std::vector<unsigned char>.
    The problem I face is when I convert this grabbed pixmap to qbytearray or qbuffer it's value becomes NULL! as an example:
    Qt Code:
    1. QVariant myPixmap(QPixmap(QPixmap::grabWidget(this)));
    2.  
    3. QByteArray myByteArray = myPixmap.toByteArray() ;
    4.  
    5. qDebug() << myByteArray.size(); // 0 size
    To copy to clipboard, switch view to plain text mode 

    or
    Qt Code:
    1. QPixmap originalPixmap = QPixmap::grabWidget(this);
    2.  
    3. QImage *image = new QImage(originalPixmap.toImage());
    4.  
    5. QByteArray Array;
    6.  
    7. QBuffer buffer(&Array);
    8.  
    9. buffer.open(QIODevice::WriteOnly);
    10.  
    11. image->save(&buffer);
    To copy to clipboard, switch view to plain text mode 

    again returns null string. am I missing something in the code?

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: convert QPixmap to std::vector<unsigned char>

    Your entire question presupposes that there is some natural meaning to "I need to convert originalPixmap to std::vector<unsigned char>." You can convert an image into a list of bytes in a near-infinite number of ways. Most of these will not be a "string" in the human readable sense . What exactly are you expecting to see in your array of bytes?

    A QVariant of type QVariant::Pixmap is not a QVariant of type QVariant::ByteArray and cannot be magically converted.

    In your second example there is no string, so what do you mean by "returns null string"? Line 11 is probably failing because you have not specified the format in which to save() (and there is no file name from which to infer one) but you do not check the return value so we cannot say.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: convert QPixmap to std::vector<unsigned char>

    In addition to what Chris already said, you are doing a lot of totally unneeded conversions and it is not the first time I see them in your code. You are converting a pixmap to an image and then saving it to a byte array. QPixmap has a save() method which can do it directly. Not speaking about memory you spill all over your code.

    Instead of inventing weird code for weird data transformations, when asking a question it is better to just state what your goal is and what you already tried to obtain that goal. I doubt your goal is to "convert QPixmap into std::vector<unsigned char>".
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: convert QPixmap to std::vector<unsigned char>

    I am using lodePNG library to encode and decode png files. The library take std:vector<unsigned char> image as an input:
    Qt Code:
    1. unsigned encode(std::vector<unsigned char>& out,
    2. const std::vector<unsigned char>& in, unsigned w, unsigned h,
    3. LodePNGColorType colortype, unsigned bitdepth)
    To copy to clipboard, switch view to plain text mode 

    I need to print screen and encode it. Therefore, I asked you how to convert QPixmap to std::vector<unsigned char>.
    Furthermore, I have written the following code so far (the problem of getting null was corrected once I specified the image type, thanks Chris)
    Qt Code:
    1. int width = 400;
    2. int height = 300;
    3.  
    4. QPixmap originalPixmap = QPixmap::grabWidget(this);
    5. QImage *image = new QImage(originalPixmap.toImage());
    6. QBuffer qbuffer;
    7. qbuffer.setBuffer(&ba);
    8. qbuffer.open(QIODevice::WriteOnly);
    9. image->save(&qbuffer, "PNG");
    10.  
    11. QByteArray rawPixels = qbuffer.buffer();
    12.  
    13. int size = rawPixels.size();
    14. char* temp = (char*) rawPixels.constData();
    15.  
    16. std::vector<unsigned char> imageVector;
    17. imageVector.resize(size);
    18.  
    19. for(int index = 0; index < size; index++)
    20. {
    21. imageVector.push_back(temp[index]);
    22. }
    23.  
    24. /* compress PNG */
    25. std::vector<unsigned char> png;
    26. png.resize(size);
    27. unsigned error = lodepng::encode(png, imageVector, width, height);
    28. if(error) qDebug() << "encoder error " << error << ": "<< lodepng_error_text(error);
    To copy to clipboard, switch view to plain text mode 

    that's all.

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: convert QPixmap to std::vector<unsigned char>

    If your goal is to have the image data encoded as PNG, you are done after line 10.

    Even shorter because you can call save on orginalPixmap, no need to explicitly convert to QImage.

    You also don't need to set a QByteArray on the buffer because it will do that when being opened for WriteOnly.

    Cheers,
    _

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

    saman_artorious (16th September 2013)

  7. #6
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: convert QPixmap to std::vector<unsigned char>

    If your goal is to have the image data encoded as PNG, you are done after line 10.
    Gee, I didn't know that by the end of line 10 the image is compressed! That's why in line 25 I tried to compress the image again. Could you explain why this compressing/encoding is automatically done by the end of line 10 please?

    Furthermore, this encoding of line 10 can be proved as the byte array associated with the buffer is only of size 1216! which is much less than the real size, 300(width) * 400(height) * 4(RGB).

    Now, is there a qt function to decode png again after line 10? or I need to use the decode function associated with line 25.

    Thank you

  8. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: convert QPixmap to std::vector<unsigned char>

    Quote Originally Posted by saman_artorious View Post
    Could you explain why this compressing/encoding is automatically done by the end of line 10 please?
    libpng does that as part of file saving.

    Now, is there a qt function to decode png again after line 10?
    Load the file to QImage
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. The following user says thank you to wysota for this useful post:

    saman_artorious (16th September 2013)

  10. #8
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: convert QPixmap to std::vector<unsigned char>

    Bingo
    QImage image((const uchar* )bytes.constData(), 400, 300, QImage::Format_RGB32);
    qDebug() << image.byteCount(); // size = 480000
    Thank You All

  11. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: convert QPixmap to std::vector<unsigned char>

    Quote Originally Posted by saman_artorious View Post
    Could you explain why this compressing/encoding is automatically done by the end of line 10 please?
    wysota already gave explaination on how it is done, but just conceptually: you are asking the class (QPixmap or QImage) to "save as PNG".
    PNG is a compressed format, so in order to create PNG data it needs to be compressed by the image saving code.
    Delegated to a plugin which uses libpng, but that is merely a technical detail

    Cheers,
    _

  12. The following user says thank you to anda_skoa for this useful post:

    saman_artorious (16th September 2013)

Similar Threads

  1. How to convert unsigned char[] to char *?
    By Gokulnathvc in forum Newbie
    Replies: 2
    Last Post: 29th April 2011, 08:58
  2. convert from unsigned char* to QString
    By bhaskar in forum Qt Programming
    Replies: 2
    Last Post: 12th February 2010, 06:36
  3. convert unsigned char * to QString
    By sepehr in forum Qt Programming
    Replies: 4
    Last Post: 9th December 2008, 20:31
  4. Conversion from unsigned char* to unsigned char
    By santosh.kumar in forum General Programming
    Replies: 1
    Last Post: 6th August 2007, 13:12
  5. Replies: 5
    Last Post: 9th April 2007, 14:26

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.