我需要像字节数组一样传递图像(Image _thresholdedImage
)...我不知道怎么做到这一点。任何的想法?谢谢!
_thresholdedImage.Source = ImageSource.FromStream (() => photo.Source);
var tessResult = await _tesseractApi.SetImage(imageBytes);
答案 0 :(得分:2)
我无法在X.Forms中转换它,而是使用以下代码和依赖服务。谢谢大家。
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;
}
答案 1 :(得分:1)
您是否尝试过使用转换器?
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();
}
答案 2 :(得分:1)
您可以使用以下功能:
public async Task<byte[]> Download(string url)
{
using (HttpClient client = new HttpClient())
{
byte[] fileArray = await client.GetByteArrayAsync(url);
return fileArray;
}
}
答案 3 :(得分:0)
通过在xamairn表单中使用Java.Io,我们可以从imagePath获取字节数组。 它适用于所有平台。
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;
}
答案 4 :(得分:0)
以下代码可用于将图像转换为字节数组。这里使用 CrossMedia 插件从相机中捕获图像。
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);
答案 5 :(得分:0)
事实证明,您可以将MediaFile对象转换为字节数组。因此,无需将文件转换为ImageSource
或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();
}
来源:https://forums.xamarin.com/discussion/156236/how-to-get-the-bytes-from-the-imagesource-in-xamarin-forms
在Xamarin论坛中向用户“ ajaxer”大喊大叫,该用户的解决方案在此问题上花了3天后才救了我。 ?