gpt4 book ai didi

c# - 如何在 WPF 桌面应用程序中使用 WinRT MediaCapture 录制时使用 CapturedFrame 呈现网络摄像头视频预览

转载 作者:太空宇宙 更新时间:2023-11-03 13:08:01 27 4
gpt4 key购买 nike

许多开发人员对 MediaCapture 类可在 WPF 中使用这一事实感到沮丧,但是用于预览视频录制的 CaptureElement 却不可用。

我理解这个限制,但是,我想使用 MediaCapture 类来录制视频。如果可能的话,我想找到一种在 WPF 中提供预览的方法。

在网上进行了大量挖掘之后,我遇到了 CapturedFrame 类。 CapturedFrame 似乎是一种抽象,表示从 MediaCapture 对象记录的单个视频帧。

CapturedFrame MSDN:

https://msdn.microsoft.com/en-us/library/windows/desktop/windows.media.capture.capturedframe.aspx

CapturedFrame 类有一个属性 SoftwareBitmap ..

我的问题:

任何人都可以提供一些代码或链接来演示如何将视频从 MediaCapture 录制到文件中..同时处理每一帧以某种方式获取 Bitamp 以使用图像控件在 WPF 中显示位图?

我知道这不是解决问题的最有效方法,但是,如果可能并且确实有效,对于我们所有不能使用 CaptureElement 的 WPF 桌面开发人员来说,这应该是一个可行的解决方法。

我想说我知道 WPFMediaKit 和其他开源项目,但是,如果可能的话,这是我希望继续进行的方式。

提前感谢任何回复。

最佳答案

Microsoft github 页面上有一个相关的示例,尽管它们针对的是 Windows 10 商店应用程序。如果我要开始一个新项目,我会选择通用 Windows 平台而不是 WPF,因此您可能有兴趣迁移您的项目以获得此功能和其他功能。

GetPreviewFrame :此示例会将预览帧捕获到 SoftwareBitmap 并将它们显示在 Image 控件中。这是相关部分:

private async Task GetPreviewFrameAsSoftwareBitmapAsync()
{
// Get information about the preview
var previewProperties = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview) as VideoEncodingProperties;

// Create the video frame to request a SoftwareBitmap preview frame
var videoFrame = new VideoFrame(BitmapPixelFormat.Bgra8, (int)previewProperties.Width, (int)previewProperties.Height);

// Capture the preview frame
using (var currentFrame = await _mediaCapture.GetPreviewFrameAsync(videoFrame))
{
// Collect the resulting frame
SoftwareBitmap previewFrame = currentFrame.SoftwareBitmap;

// Copy the SoftwareBitmap to a WriteableBitmap to display it to the user
var wb = new WriteableBitmap(previewFrame.PixelWidth, previewFrame.PixelHeight);
previewFrame.CopyToBuffer(wb.PixelBuffer);

// Display it in the Image control
PreviewFrameImage.Source = wb;
}
}

仔细查看示例,了解如何获取所有详细信息。或者,要进行演练,您可以观看 camera session来自最近的//build/ session ,其中包括一些相机示例的演练。

现在,如果您确实要迁移您的项目,您也可以使用 CaptureElement,但我认为您可能有兴趣看到它。

关于c# - 如何在 WPF 桌面应用程序中使用 WinRT MediaCapture 录制时使用 CapturedFrame 呈现网络摄像头视频预览,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30105079/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com