【发布时间】: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[]> 来显示流。
【问题讨论】:
-
关于从 byte[] 创建图像有很多 existing questions
标签: arrays xamarin.forms xamarin.android queue uwp-xaml