如何获取 MediaCapture 的预览缓冲区 - 通用应用 [英] How to get preview buffer of MediaCapture - Universal app

查看:21
本文介绍了如何获取 MediaCapture 的预览缓冲区 - 通用应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In Windows phone silverlight, I use PhotoCamera to get buffer frame when start preview video, in universal app I use MediaCapture instead, but I don't know how to get preview buffer.

谢谢

推荐答案

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

Since Windows Runtime doesn't have Silverlight's PhotoCaptureDevice class, the extremely useful GetPreviewBufferARGB() and GetPreviewBufferYCbCr() methods are not available.

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

The solution you're looking for is to use the MediaCapture.StartPreviewToCustomSinkAsync() method, but this requires C++ skills better than mine. No-one seems to have solved the problem and shared their code.

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

Now there's a really beautiful solution, using the Lumia Imaging SDK, that doesn't use the MediaCapture class, but will probably solve your problem even better.

首先查看 Microsoft Github 上的示例.这运行良好,但相当复杂,因为它同时针对 Windows 8.1 和 Windows Phone 8.1.

Check out Microsoft's example on Github first. This works well but is quite complicated because it targets both Windows 8.1 and Windows Phone 8.1.

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

I've written some simpler code, just targeting Windows Phone, for my own understanding. It might help.

从一个新的 C# Windows Phone 8.1(商店)应用开始,并通过 NuGet PM 安装 Lumia Imaging SDK.此示例在 MainPage.xaml 中使用 x:Name="previewImage" 绘制图像元素,因此请确保添加该元素.您还需要对 MainPage.xaml.cs 进行相关导入,我认为是.

Start with a new C# Windows Phone 8.1 (Store) app with the Lumia Imaging SDK installed via NuGet PM. This example draws to an image element with x:Name="previewImage" in MainPage.xaml so make sure you add that. You'll also need to make the relevant imports to MainPage.xaml.cs which I think are.

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

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

Then you just add the following in the right place in 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?你不需要!

You might be wondering... how do I grab the previewBuffer? You don't need to!

_writeableBitmap 对象始终保存来自相机的最新帧,因此您可以用它做任何您喜欢的事情.

The _writeableBitmap object always holds the latest frame from the camera so you can do whatever you like with it.

这篇关于如何获取 MediaCapture 的预览缓冲区 - 通用应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆