The method I have written below converts an image in JPG format to base64.
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.
To encode an image, Download the code from https://www.mathworks.com/matlabcentral/fileexchange/24514-base64-image-encoder
and use it like this -
imname = imread('pic.jpg');
base64str = base64file(imname);
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