Frazzle Frazzle - 3 months ago 86
Android Question

Xamarin Android - Image to Text

I have found this code:

// 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
//Close the File Stream
string _base64String = Convert.ToBase64String (ImageData);

I'm not sure how to pass in an image and which imports to use to get it working. I basically want to take an image and convert it to text.



I recommend storing images in a blob file. But if you really want to store them in a string you can achieve it with the following code snippets

To string

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

To byteArray

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