簡體   English   中英

在 Xamarin.Forms 中將圖像轉換為字節數組

[英]Convert Image into byte array in Xamarin.Forms

我需要傳遞一個像字節數組這樣的圖像( Image _thresholdedImage )......我不知道我怎么能做到這一點。 任何的想法? 謝謝!

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

var tessResult = await _tesseractApi.SetImage(imageBytes);

我無法在 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;
    }

您是否嘗試過使用轉換器?

 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();
    }

您可能正在使用以下功能:

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

        }

通過在 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;
        }
byte[] bitmapData;
using (var stm = new MemoryStream())
{
    snapshot.Compress(Bitmap.CompressFormat.Png, 0, stm);
    bitmapData = stm.ToArray();
}

以下代碼可用於將圖像轉換為字節數組。這里使用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);

事實證明,您可以將 MediaFile 對象轉換為字節數組。 因此無需將文件轉換為ImageSourceImage

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 天后拯救了我。 😁

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM