Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
cmr1990
Posts: 61
Registered: ‎10-16-2013
My Device: Z10
Accepted Solution

Convert QImage to QByteArray

Hi All,

 

Is there any way to convert QImage to QByteArray??

Please reply!

 

Cheers!

Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Convert QImage to QByteArray

use the save method.

example from http://qt-project.org/doc/qt-4.8/qimage.html#save-2

QImage image;
QByteArray ba;
QBuffer buffer(&ba);
buffer.open(QIODevice::WriteOnly);
image.save(&buffer, "PNG"); // writes image into ba in PNG format

 

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
BBSJdev
Posts: 6,118
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30

Re: Convert QImage to QByteArray

There are a few methods this link describes one of them.

 

http://www.qtforum.org/article/9484/how-to-convert-qimage-to-qbytearray-and-uchar-and-then-make-a-qm...

 

What are you planning to do though as QImage also has a pixel() function allowing you to access the colour directly?


If you've been helped click on Like Button, if you've been saved buy the app. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.
Developer
Zmey
Posts: 1,512
Registered: ‎12-18-2012
My Device: PlayBook, Z10, DAC

Re: Convert QImage to QByteArray

Quoting docs:

QImage image;
QByteArray ba;
QBuffer buffer(&ba);
buffer.open(QIODevice::WriteOnly);
image.save(&buffer, "PNG"); // writes image into ba in PNG format

Andrey Fidrya, @zmeyc on twitter
Developer
cmr1990
Posts: 61
Registered: ‎10-16-2013
My Device: Z10

Re: Convert QImage to QByteArray

 i want to store it into the database.

Developer
BBSJdev
Posts: 6,118
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30

Re: Convert QImage to QByteArray

LOL. First time I've seen three of the same answers entered at the same time.

 


If you've been helped click on Like Button, if you've been saved buy the app. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.
Developer
BBSJdev
Posts: 6,118
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30

Re: Convert QImage to QByteArray

[ Edited ]

Ok then well you could use the pixel() function to write the bytes in to the DB via an array/buffer then use the setPixel() function to write them back.

 

However I'm not sure this would be any more efficient (depending on your implentation of course).

 

So go with the first suggestion by Simon, myself and Zmey, as it's less effort.

 


If you've been helped click on Like Button, if you've been saved buy the app. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.
Developer
BBSJdev
Posts: 6,118
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30

Re: Convert QImage to QByteArray

[ Edited ]

I'm also going to predict your next questions and assume you want to go from the DB back to a QImage maybe even to an ImageView.

 

So there is a good thread on that here...

 

http://supportforums.blackberry.com/t5/Native-Development/ImageView-and-Qimage-integration/td-p/1777...

 

and here...

 

http://supportforums.blackberry.com/t5/Native-Development/How-to-convert-the-Qbytearray-to-image-in-...


If you've been helped click on Like Button, if you've been saved buy the app. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.
Developer
cmr1990
Posts: 61
Registered: ‎10-16-2013
My Device: Z10

Re: Convert QImage to QByteArray

LOL!! thanks a tonnn!!!! :smileyvery-happy: :smileyvery-happy: