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

前端之家收集整理的这篇文章主要介绍了windows – 如何获取MediaCapture的预览缓冲区 – 通用应用程序前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在Windows手机silverlight中,我在启动预览视频时使用PhotoCamera获取缓冲帧,在通用应用中我使用MediaCapture,但我不知道如何获取预览缓冲区.

谢谢

由于Windows Runtime没有Silverlight的PhotoCaptureDevice类,因此无法使用非常有用的GetPreviewBufferARGB()和GetPreviewBufferYCbCr()方法.

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

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

首先查看Microsoft的example on Github.这很好用但很复杂,因为它同时针对Windows 8.1和Windows Phone 8.1.

为了我自己的理解,我已经编写了一些更简单的代码,仅针对Windows Phone.它可能有所帮助.

首先使用通过NuGet PM安装Lumia Imaging SDK的新C#Windows Phone 8.1(Store)应用程序.此示例在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相关文章