Xamarin Android -图像到文本

本文关键字:文本 图像 Android Xamarin | 更新日期: 2023-09-27 18:13:41

我找到了这段代码:

// provide read access to the file
FileStream fs = new FileStream(media.Path, FileMode.Open,FileAccess.Read);
// Create a byte array of file stream length
byte[] ImageData = new byte[fs.Length];
//Read block of bytes from stream into the byte array
fs.Read(ImageData,0,System.Convert.ToInt32(fs.Length));
//Close the File Stream
fs.Close();
string _base64String = Convert.ToBase64String (ImageData);

我不确定如何传递图像以及使用哪些导入来使其工作。我想把一张图片转换成文字。

谢谢

Xamarin Android -图像到文本

我建议将图像存储在blob文件中。但是,如果您真的想将它们存储在字符串中,您可以使用以下代码片段

实现它。

字符串

byte[] ImageData = File.ReadAllBytes(@"Path to the image to load");
        string _base64String = Convert.ToBase64String(ImageData);

中byteArray

byte[] data = Convert.FromBase64String(_base64String);
        File.WriteAllBytes(@"Path to the image to store", data);