gpt4 book ai didi

windows - 如何获取 MediaCapture 的预览缓冲区 - 通用应用程序

转载 作者:可可西里 更新时间:2023-11-01 09:43:11 26 4
gpt4 key购买 nike

在Windows phone silverlight中,我在开始预览视频时使用PhotoCamera获取缓冲区帧,在通用应用程序中我使用MediaCapture代替,但我不知道如何获取预览缓冲区。

谢谢

最佳答案

由于 Windows 运行时没有 Silverlight 的 PhotoCaptureDevice 类,因此非常有用的 GetPreviewBufferARGB()GetPreviewBufferYCbCr() 方法不可用。

您正在寻找的解决方案是使用 MediaCapture.StartPreviewToCustomSinkAsync() 方法,但这需要比我更好的 C++ 技能。似乎没有人解决了这个问题并分享了他们的代码。

现在有一个非常漂亮的解决方案,使用Lumia Imaging SDK ,它不使用 MediaCapture 类,但可能会更好地解决您的问题。

查看 Microsoft 的 example on Github第一的。这很好用,但相当复杂,因为它同时针对 Windows 8.1 和 Windows Phone 8.1。

为了我自己的理解,我写了一些更简单的代码,只针对 Windows Phone。这可能会有所帮助。

从新的 C# Windows Phone 8.1 (Store) 应用程序开始,通过 NuGet PM 安装 Lumia Imaging SDK。此示例使用 MainPage.xaml 中的 x:Name="previewImage" 绘制图像元素,因此请务必添加它。您还需要对 MainPage.xaml.cs 进行相关导入,我认为这是。

using Lumia.Imaging;
using System.Threading.Tasks;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Core;
using System.ComponentModel;

然后您只需在 MainPage.xaml.cs 中的正确位置添加以下内容。

private CameraPreviewImageSource _cameraPreviewImageSource; // Using camera as our image source
private WriteableBitmap _writeableBitmap;
private FilterEffect _effect;
private WriteableBitmapRenderer _writeableBitmapRenderer; // renderer for our images
private bool _isRendering = false; // Used to prevent multiple renderers running at once

public MainPage()
{
this.InitializeComponent();
this.NavigationCacheMode = NavigationCacheMode.Required;
startCameraPreview();
}

private async Task startCameraPreview()
{
// Create a camera preview image source (from the Lumia Imaging SDK)
_cameraPreviewImageSource = new CameraPreviewImageSource();
await _cameraPreviewImageSource.InitializeAsync(string.Empty); // use the first available camera (ask me if you want code to access other camera)
var previewProperties = await _cameraPreviewImageSource.StartPreviewAsync();
_cameraPreviewImageSource.PreviewFrameAvailable += drawPreview; // call the drawPreview method every time a new frame is available

// Create a preview bitmap with the correct aspect ratio using the properties object returned when the preview started.
var width = 640.0;
var height = (width / previewProperties.Width) * previewProperties.Height;
var bitmap = new WriteableBitmap((int)width, (int)height);
_writeableBitmap = bitmap;

// Create a BitmapRenderer to turn the preview Image Source into a bitmap we hold in the PreviewBitmap object
_effect = new FilterEffect(_cameraPreviewImageSource);
_effect.Filters = new IFilter[0]; // null filter for now
_writeableBitmapRenderer = new WriteableBitmapRenderer(_effect, _writeableBitmap);
}

private async void drawPreview(IImageSize args)
{
// Prevent multiple rendering attempts at once
if (_isRendering == false)
{
_isRendering = true;
await _writeableBitmapRenderer.RenderAsync(); // Render the image (with no filter)
// Draw the image onto the previewImage XAML element
await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High,
() =>
{
previewImage.Source = _writeableBitmap; // previewImage is an image element in MainPage.xaml
_writeableBitmap.Invalidate(); // force the PreviewBitmap to redraw
});
_isRendering = false;
}
}

您可能想知道...我如何获取 previewBuffer?你不需要!

_writeableBitmap 对象始终保存来自相机的最新帧,因此您可以随心所欲地使用它。

关于windows - 如何获取 MediaCapture 的预览缓冲区 - 通用应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27394751/

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