你可以在Java的帮助下做到这一点。例子:
% get a stream of bytes representing an endcoded JPEG image
% (in your case you have this by decoding the base64 string)
fid = fopen('test.jpg', 'rb');
b = fread(fid, Inf, '*uint8');
fclose(fid);
% decode image stream using Java
jImg = javax.imageio.ImageIO.read(java.io.ByteArrayInputStream(b));
h = jImg.getHeight;
w = jImg.getWidth;
% convert Java Image to MATLAB image
p = reshape(typecast(jImg.getData.getDataStorage, 'uint8'), [3,w,h]);
img = cat(3, ...
transpose(reshape(p(3,:,:), [w,h])), ...
transpose(reshape(p(2,:,:), [w,h])), ...
transpose(reshape(p(1,:,:), [w,h])));
% check results against directly reading the image using IMREAD
img2 = imread('test.jpg');
assert(isequal(img,img2))
解码JPEG字节流的第一部分是基于这个答案:
数组中给出数据时的JPEG解码
将 Java 图像转换为 MATLAB 的最后一部分基于此解决方案页面:
如何将“Java Image”对象转换为 MATLAB 图像矩阵?
最后一部分也可以重写为:
p = typecast(jImg.getData.getDataStorage, 'uint8');
img = permute(reshape(p, [3 w h]), [3 2 1]);
img = img(:,:,[3 2 1]);
imshow(img)