加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

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

发布时间:2020-12-13 20:34:50 所属栏目:Windows 来源:网络整理
导读:在Windows手机silverlight中,我在启动预览视频时使用PhotoCamera获取缓冲帧,在通用应用中我使用MediaCapture,但我不知道如何获取预览缓冲区. 谢谢 由于Windows Runtime没有Silverlight的PhotoCaptureDevice类,因此无法使用非常有用的GetPreviewBufferARGB()
在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对象始终保存来自摄像头的最新帧,因此您可以随意执行任何操作.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读