我在Matlab环境下工作的一个项目,我必须解码来自数据库服务器,它以base64编码格式的XML获得了RGB图像。 我成功地转换图像为base64并通过将其转换成XML张贴到数据库中。 我用base64encode /解码图像,以BASE64编码和我已附加下面的程序。 问题是,当我使用base64decode功能,并尝试从的base64重新转换图像。 它根本不起作用。
这是我的转换图像为base64和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我用下面再改与Base64格式回图像的图像创建的XML,但遗憾的是它不工作,并抛出一些奇怪的字符,我不能回去转换成图像。 我还没有线索,如何将字符串转换回映像。
filename = '...\IMAG0386.xml';
tree = xml_read(filename);
image = tree.imagebase64;
K = base64decode(tree.imagebase64)) %test image retrieval --> only the string
我尝试了像使用其他选项Java代码在MATLAB中,但我不知道,如何使用代码在Matlab。 这在C#,Java的许多选择,但我不知道,如何在MATLAB中使用它们。 请帮我在这方面。