Friday, May 12, 2017

Base64 encode/decode in MATLAB

The method I have written below converts an image in JPG format to base64.

and use it like this - 
imname = imread('pic.jpg');
base64str = base64file(imname);

To decode an image, I could not find a more direct solution in MATLAB than to write it as a JPG file and then read it back.

raw = base64decode(xmlImageBytes);   % convert xml image to raw binary
fid = fopen('decodedImage.jpg', 'wb');
fwrite(fid, raw, 'uint8');            % dump the raw binary to the hard disk
fclose(fid);
img = imread('decodedImage.jpg');

This method does not remove/convert escape characters correctly sometimes, thus decoded image is not correct. Additional code has to be written to convert escape characters before decoding the XML image bytes. 

No comments:

Post a Comment

Base64 QString to QImage to QString in Qt5

I have tested it on Qt5.10 and it works well. Encode - QStringToQImage ( const QString msg ) { QByteArray by = QByteArray :: ...