Convert Base64 string to an image in matlab?

9 ビュー (過去 30 日間)
Siobhán Harty
Siobhán Harty 2018 年 11 月 2 日
回答済み: JiaChun Lin 2020 年 4 月 30 日
I am wondering if any one could direct me to a matlab function/code that would allow me to convert a Base64 string to an image? You can do so via the below website (by simply pasting your string into a text box). But I would like to integrate this into a script and save out the image file.
Any help would be greatly appreciated.
Thanks,
Siobhán

採用された回答

Walter Roberson
Walter Roberson 2018 年 11 月 3 日
The site does not really convert base 64 to image, not really.
The background here is that the most common way in http to request that an image be displayed is to use http "multipart", with the part being a header that indicates the content type is image and that the encoding is base 64. Then after the headers there is sent a base 64 string which is the base 64 encoding of the complete binary file that held the image. The site does not send any kind of commands like "change to color 73,pen down, draw 9 pixels at angle 25 degrees" : the site just sends whatever it found in the image file. The operation is pretty much an inline file transfer protocol with no interpretation of what is inside the file.
Then, your browser having received the binary image file, your browser is responsible for taking the content and displaying it.
Thus what is being returned from the base 64 decoder that James suggested is the binary content of an image file. To save it off as an image, fopen, fwrite, fclose, without having tried to display it. The tricky part is deciding the file extension, which would normally have been conveyed as part of the multipart http process.
Now, MATLAB does not offer any direct way of taking a byte array that encodes an image file and displaying it. The routines probably exist internally as doing so is a fundamental operation, but no official interface is offered for it. There are available Java interfaces for some image types.
But those interfaces to display do not matter since you are after saving to file anyhow, which you would do directly. If you want to display as well you can imread or imshow the saved file.
  1 件のコメント
Siobhán Harty
Siobhán Harty 2018 年 11 月 3 日
Thank you, Walter. Really appreciate you taking the time to provide all of that information. Very helpful!

サインインしてコメントする。

その他の回答 (2 件)

James Tursa
James Tursa 2018 年 11 月 2 日
Maybe some variation of this:
encoder = org.apache.commons.codec.binary.Base64;
bytes = encoder.decode(int8(base64string'));
  2 件のコメント
Siobhán Harty
Siobhán Harty 2018 年 11 月 3 日
Thanks so much for your suggestion, James.
However, that returns bytes as opposed to the image.
More of a concern now is that maltab seems to be struggling big time with the size of the base64 string to begin with.
Thanks again!
Siobhan
NiKa
NiKa 2020 年 4 月 28 日

サインインしてコメントする。


JiaChun Lin
JiaChun Lin 2020 年 4 月 30 日
can use raw = matlab.net.base64decode(% input the base64 encode....);
then as what Walter said, use 'fopen, fwirte, fclose, without having tried to display it'. it really works, thanks very much.

カテゴリ

Help Center および File ExchangeConvert Image Type についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by