6

I need to pass an image (Image _thresholdedImage) like byte array... I don't know how I can do this. Any idea? Thank you!

_thresholdedImage.Source = ImageSource.FromStream (() => photo.Source);

var tessResult = await _tesseractApi.SetImage(imageBytes);
Jonathan Zúñiga
  • 645
  • 3
  • 13
  • 25
  • Please specifiy the function signature for `SetImage()`. Assuming `SetImage()` takes a `byte[]`, use the answer provided in here: http://stackoverflow.com/a/23731642/5296568 . Adjust the format to the whatever the function expects (Bitmap, maybe?). (I only found the C++ documentation at https://zdenop.github.io/tesseract-doc/group___advanced_a_p_i.html#gaa463622111f3b11d8fca5863709cc699 ). – Maximilian Gerhardt Nov 26 '15 at 21:25

7 Answers7

2

I was unable to convert it in X.Forms, instead I use the following code with a dependency service. Thank you to all.

    public async Task<byte[]> GetBytesFromImage(string filePath)
    {
        ConvertImageToBW(filePath);

        // Create another bitmap that will hold the results of the filter.
        Bitmap thresholdedBitmap = Bitmap.CreateBitmap (BitmapFactory.DecodeFile(filePath));

        thresholdedBitmap = BitmapFactory.DecodeFile (thresholdedImagePath);

        byte[] bitmapData;
        using (var stream = new MemoryStream())
        {
            thresholdedBitmap.Compress(Bitmap.CompressFormat.Png, 0, stream);
            bitmapData = stream.ToArray();
        }

        return bitmapData;
    }
Jonathan Zúñiga
  • 645
  • 3
  • 13
  • 25
1

Have you tried using converters?

 public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        ImageSource retSource = null;
        if (value != null)
        {
            byte[] imageAsBytes = (byte[])value;
            retSource = ImageSource.FromStream(() => new MemoryStream(imageAsBytes));
        }
        return retSource;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
MohamedHamza
  • 205
  • 1
  • 12
1

It turns out that you can convert the MediaFile object to a byte array. So no need to convert the file to ImageSource or Image.

var photo = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions() { });

byte[] imageArray = null;

using (MemoryStream memory = new MemoryStream()) {

    Stream stream = photo.GetStream();
    stream.CopyTo(memory);
    imageArray = memory.ToArray();
}

Source: https://forums.xamarin.com/discussion/156236/how-to-get-the-bytes-from-the-imagesource-in-xamarin-forms
Shoutout to user "ajaxer" in the Xamarin forums whose solution saved me after spending 3 days on this issue.

Alireza Sattari
  • 181
  • 2
  • 12
0

You could be using the following function:

  public async Task<byte[]> Download(string url)
        {
            using (HttpClient client = new HttpClient())
            {
                byte[] fileArray = await client.GetByteArrayAsync(url);
                return fileArray;
            }

        }
dirtydanee
  • 6,081
  • 2
  • 27
  • 43
Abdullah Tahan
  • 1,963
  • 17
  • 28
  • 1
    Welcome to Stack Overflow! While this piece of code may answer the question, it is better to include a description of what the problem was, and how your code will tackle the given problem. For the future, here is some information, [how to crack a awesome answer](http://stackoverflow.com/help/how-to-answer) on Stack Overflow. – dirtydanee Jan 01 '17 at 16:36
0

By Using Java.Io in xamairn forms we can get byte array from imagePath. It will work for all platforms.

private static byte[] GetBytesFromImage(string imagePath)
        {
            var imgFile = new File(imagePath);
            var stream = new FileInputStream(imgFile);
            var bytes = new byte[imgFile.Length()];
            stream.Read(bytes);
            return bytes;
        }
0
byte[] bitmapData;
using (var stm = new MemoryStream())
{
    snapshot.Compress(Bitmap.CompressFormat.Png, 0, stm);
    bitmapData = stm.ToArray();
}
Alex Butenko
  • 3,664
  • 3
  • 35
  • 54
Abdullah Tahan
  • 1,963
  • 17
  • 28
0

Following code can use to convert image into byte array.Here CrossMedia plugin is used to capture image from camera.

public byte[] imageByte;
Stream imageStream = null; 
var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
       { Name = "pic.jpg"});
if (file == null) return;
imageStream = file.GetStream();
BinaryReader br = new BinaryReader(imageStream);
imageByte = br.ReadBytes((int)imageStream.Length);
Sachin
  • 1
  • 1