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

时间:2015-05-07 15:10:29

标签: c# wpf xaml video windows-runtime

有很多开发人员对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中显示位图? / p>

我知道这不是处理问题的最有效方法,但是,如果它可行并且确实有效,那么对于我们所有不能使用CaptureElement的WPF桌面开发者而言,这应该是一个可行的解决方案。

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

预先感谢任何回复。

1 个答案:

答案 0 :(得分:1)

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,其中包括一些相机示例的演练。

现在,如果您确实迁移了项目,那么您也可以使用CaptureElement,但我认为您可能对此感兴趣。