【问题标题】:Xamarin image renderer from byte[]来自 byte[] 的 Xamarin 图像渲染器
【发布时间】:2022-11-15 02:17:34
【问题描述】:

我正在构建一个应用程序,我在其中使用 singalR 实现流。 我使用此代码示例从一个联系人https://github.com/Guille1878/VideoChat 的摄像头获取图像(感谢他提供了这样的代码)。

我通过向集线器发送一个 json 来更新协议:

string arrayJson = JsonConvert.SerializeObject(array);

当图像到达客户端接收器时,我将使用以下代码将其作为 byte[] 返回:

hubConnection.On<string>("DownloadStream", (streamJson) =>
{
    byte[] stream = JsonConvert.DeserializeObject<byte[]>(streamJson);
    MyClass.StreamVideoArray.Enqueue(stream);
});

为了使用该图像,我现在使用一个按钮创建了一个依赖界面启动:

private async void StartStopStream_Clicked(object sender, EventArgs e)
{
    if (onStreaming)
    {
        onStreaming = false;
        OnStreamStatus.Text = "Stream terminated";
    }
    else
    {
        onStreaming = true;
        OnStreamStatus.Text = "On stream";
    }
    int count = 0;
    while (onStreaming)
    {
        await DependencyService.Get<IStreamImgConvert>().BuildImageFrames(count++);
        Stream1 = App.StreamImage;
    }
}

在注入中,我使用了以下代码:

public async Task BuildImageFrames(int count)
{
    await Task.Delay(5);

    MyClass.StreamVideoArray.TryDequeue(out byte[] buffer);

    if (!(buffer?.Any() ?? false))
        return;

    try
    {
        var imageMemoryStream = new MemoryStream(buffer);
        \\Create a new filepath using the int count
        string filePath = Path.Combine(FileSystem.AppDataDirectory, $"File{count}.bmp");

        \\Create the image to ensure the byte arrays from the buffer is usable
        File.WriteAllBytes(filePath, buffer); 
        ImageSource imgFromStream = ImageSource.FromStream(() => imageMemoryStream);

        \\Update the static image which should be update in the xaml page
        MyClass.StreamImage.Source = imgFromStream; 
    }
    catch (Exception ex)
    {
        System.Diagnostics.Debug.WriteLine(ex.Message);
    }
}

这整数计数BuildImageFrames() 内部用于创建图像。这些图像是完美创建的。不幸的是,byte[] 不用于创建静态图像,不会在 xaml 页面内显示任何图像。

我想知道是否有可能创造出类似的东西。有人可以帮我弄清楚吗?

可能会提供另一种解决方案来使用 Queue<byte[]> 来显示流。

【问题讨论】:

标签: arrays xamarin.forms xamarin.android queue uwp-xaml


【解决方案1】:

感谢@jason 提供的链接。 看了一下,让我想起了在xaml里面操作一个对象,应该是在xaml里面,MVVM的xaml.cs绑定到页面里面。

我更正了我的代码并能够显示图像。在依赖注入中,我使用了以下代码:


public async Task BuildImageFrames(int count)
{
    await Task.Delay(5);

    MyClass.App.StreamVideoArray.TryDequeue(out byte[] buffer);

    if (!(buffer?.Any() ?? false))
        return;

    try
    {
        MyClass.App.VideoArray = buffer;
    }
    catch (Exception ex)
    {
        System.Diagnostics.Debug.WriteLine(ex.Message);
    }
}

在 xaml 页面中,我使用了以下代码:

int count = 0;
while (onStreaming)
{
    await DependencyService.Get<IStreamImgConvert>().BuildImageFrames(count++);
    if (App.VideoArray != null)
    {
        ImageSource imgFromStream = ImageSource.FromStream(() => new MemoryStream(App.VideoArray));
        Stream1.Source = imgFromStream;
    }
}

【讨论】:

    猜你喜欢
    • 2017-01-22
    • 2011-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-14
    • 1970-01-01
    • 2021-11-15
    相关资源
    最近更新 更多