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
Contributor
Posts: 48
Registered: ‎08-07-2013
My Device: z10
My Carrier: blackberry

Base64 to image decoder

hi 

 

   i want to decode a base64 image to png image file.how this can be done.the content of base64is in a text file.i had read it to a Qbytearray.now how can i proceed forward?

 

thank you, 

Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: Base64 to image decoder

Hi,

QByteArray decoded = QByteArray::fromBase64(data);
QImage image;
image.loadFromData(decoded);

Or, if you know that the image is in PNG format:

 

image.loadFromData(decoded, "PNG");

To save to disk:

 

image.save("filename.png")

 Optionally, specify a format (otherwise QImage will try to guess it based on filename):

image.save("filename.png", "PNG");

 QImage & QByteArray have much more options, you might want to browse them.

 


Andrey Fidrya, @zmeyc on twitter
Contributor
Posts: 48
Registered: ‎08-07-2013
My Device: z10
My Carrier: blackberry

Re: Base64 to image decoder

thanks for the replay,

sorry,i had forgot to mention.it has to display on the image view without saving it on the disk.please help me..
Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: Base64 to image decoder

I think this should work (untested):

 

QImage rgb32Image = image.convertToFormat(QImage::Format_RGB32);
QImage finalImage = rgb32Image.rgbSwapped();
const bb::ImageData imageData = bb::ImageData::fromPixels(finalImage.bits(), bb::PixelFormat::RGBX, finalImage.width(), finalImage.height(), finalImage.bytesPerLine()); Image cascadesImage = bb::cascades::Image(imageData);

Code taken from:

https://github.com/blackberry/Cascades-Samples/blob/master/scratchpad/src/app.cpp

 


Andrey Fidrya, @zmeyc on twitter
Retired
Posts: 418
Registered: ‎07-18-2012
My Device: Q10
My Carrier: Bell Canada

Re: Base64 to image decoder

If you decide you do need to save on disk, keep QTemporaryFile in mind - it (can) delete itself when it goes out of scope/object is deleted

 

http://harmattan-dev.nokia.com/docs/library/html/qt4/qtemporaryfile.html

 

 

--
Rob is no longer associated with BlackBerry.