Decode base64 to RGB image in matlab

2019-07-28 23:18发布

问题:

I am working in Matlab environment for a project, and I have to decode a RGB image received in xml from the database server, which is encoded in base64 format. I was successful in converting image to base64 and post it to the database by converting it into xml. I used the base64encode/decode to encode the image to base64 and I have attached the program below. The problem is when I use the base64decode function and try to reconvert the image from base64. It simply does not work.

This is my program for converting image to base64 and encode it in xml.

function image2xml(test_directory)
% Images in directory ---> byte array format in XML
% This function encodes all the images available in the test directory into
% byte array/base 64 format and saves them in xml with the following
% properties
%     Packs the image(byte array) and its name as timestamp to xml
% Uses functions from the following source
% http://www.mathworks.de/matlabcentral/fileexchange/12907-xmliotools
% Following functions from the above source are to be added to path,while
% running this function
%     xml_write.m
%     xml_read.m
%% ========================================================================


files=dir(test_directory)
% delete('test_image_xml\*.xml');
% If not database_mat is included in the function arguments

    for i = 1:size(files,1)
        k=0;
        if files(i).isdir()==0
            %extracts name with which it savesa as xml
            [~, name_to_save,~ ] = fileparts(files(i).name)
            filename = fullfile([test_directory,'\',files(i).name])
            fid = fopen(filename);
            raw_data = uint8(fread(fid));% read image file as a raw binary
            fclose(fid);
            %Definition of xml tags
            image_imagedetails = [];
            % name of the file is assumed to be the timestamp
            image_imagedetails.timestamp =name_to_save;
            %imagescan.imagebyte64.ATTRIBUTE.EncodingMIMEType = 'base64';
            image_imagedetails.imagebase64 = base64encode(raw_data);% perform base64 encoding of the binary data
            %saves all the xml files into the predefined directory
            mkdir('images_and_timestamp_xml');
            filename = ['images_and_timestamp_xml\' name_to_save,'.xml' ];
            post_data = xml_write(filename, image_imagedetails);

        end
    end

Finaly I use the following to reconvert the xml created with image in base64 format back to image, but unfortunately it does not work, and throws some strange characters, which I am not able to convert back into a image. I further have no clue as of how to convert the string back to image as well.

filename = '...\IMAG0386.xml';
tree = xml_read(filename);
image = tree.imagebase64;
K = base64decode(tree.imagebase64)) %test image retrieval --> only the string

And I tried out other option like using the Java code in matlab, but I do not know, how to use the code in matlab. There are many options in C#, Java, but I have no idea, as how to use them in matlab. Please help me in this regards.

回答1:

Simple Base64 Handling

Using the Apache library

base64 = org.apache.commons.codec.binary.Base64

Then you can call encode or decode.

base64.encode()
base64.decode()

It expects byte[], so you can get this in a couple of ways. Let's encode a string and then decode it.

hello = 'Hello, world!';
encoded = char(base64.encode(unicode2native(hello))).';
result = native2unicode(base64.decode(uint8(output)).');


回答2:

I ran your code under Matlab R2012a and it seems to work as expected.

Maybe what is missing here is a few lines to get the image file back from the binary data encoded in base64. You just need to write the binary data to a file to get your image file back.

I am merely quoting the HTML help file from the Matlab FileExchange submission xmliotools that you are using in your code:

Read XML file with embedded binary data encoded as Base64 (using java version)

tree = xml_read('test.xml', Pref);         % read xml file
raw  = base64decode(tree.MyImage.CONTENT, '', 'java');   % convert xml image to raw binary
fid = fopen('MyFootball.jpg', 'wb');
fwrite(fid, raw, 'uint8');                 % dumb the raw binary to the hard disk
fclose(fid);
I = imread('MyFootball.jpg');              % read it as an image
imshow(I);