1

我正在尝试创建一个 UWP 应用程序,它允许您修改相机动态显示的颜色。我当前的代码基于 Nikola Metulev 的Effective example,它使用 IBasicVideoEffect 修改相机视图。但是我需要在相机处于活动状态时修改颜色(例如显示更多红色)。

我已经搜索了几天,我认为我的问题是,由于 IBasicVideoEffect 必须在一个单独的类中,我不知道如何将值(在这种情况下为 Matrix5x4)从 MainPage 传递到 Effect 类。

这是我的代码:

主页类:

using ColorEffects;
using System;
using Windows.Media.Capture;
using Windows.Media.Effects;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

namespace Color
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        private MediaCapture _mediaCapture;

        public MainPage()
        {
            this.InitializeComponent();
        }

        protected async override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            _mediaCapture = new MediaCapture();
            var settings = new MediaCaptureInitializationSettings()
            {
                StreamingCaptureMode = StreamingCaptureMode.Video,
            };
            await _mediaCapture.InitializeAsync(settings);
            captureElement.Source = _mediaCapture;
            await _mediaCapture.StartPreviewAsync();

            var effect = await _mediaCapture.AddVideoEffectAsync(
                new VideoEffectDefinition(typeof(TheEffect).FullName),
                                            MediaStreamType.VideoPreview);
        }
    }
}

颜色效果类:

using Microsoft.Graphics.Canvas;
using Microsoft.Graphics.Canvas.Brushes;
using Microsoft.Graphics.Canvas.Effects;
using Microsoft.Graphics.Canvas.Text;
using System;
using System.Collections.Generic;
using System.Numerics;
using Windows.Foundation.Collections;
using Windows.Graphics.DirectX.Direct3D11;
using Windows.Media.Effects;
using Windows.Media.MediaProperties;
using Windows.UI.Text;

namespace ColorEffects
{
    public sealed class TheEffect : IBasicVideoEffect
    {
        private IPropertySet _configuration;
        private CanvasDevice _canvasDevice;

        public void ProcessFrame(ProcessVideoFrameContext context)
        {
            using (var inputBitmap = CanvasBitmap.CreateFromDirect3D11Surface(_canvasDevice, context.InputFrame.Direct3DSurface))
            using (var drawingSurface = CanvasRenderTarget.CreateFromDirect3D11Surface(_canvasDevice, context.OutputFrame.Direct3DSurface).CreateDrawingSession())
            {
                var matrix = new Matrix5x4() { M11 = 0.5f, M22 = 0.5f, M33 = 1.0f, M44 = 1.0f, M54 = 0.0f };
                var colorEffect = new ColorMatrixEffect()
                {
                    ColorMatrix = matrix,
                    Source = inputBitmap
                };

                drawingSurface.DrawImage(colorEffect);
            }
        }

        public void SetEncodingProperties(VideoEncodingProperties encodingProperties, IDirect3DDevice device)
        {
            _canvasDevice = CanvasDevice.CreateFromDirect3D11Device(device);
        }

        public bool IsReadOnly { get { return false; } }

        public IReadOnlyList<VideoEncodingProperties> SupportedEncodingProperties
        {
            get
            {
                var properties = new List<VideoEncodingProperties>();
                properties.Add(VideoEncodingProperties.CreateUncompressed("ARGB32", 640, 480));
                return properties;
            }
        }

        public MediaMemoryTypes SupportedMemoryTypes { get { return MediaMemoryTypes.Gpu; } }

        public bool TimeIndependent { get { return false; } }

        public void DiscardQueuedFrames() { }

        public void Close(MediaEffectClosedReason reason)
        {
            if (_canvasDevice != null)
                _canvasDevice.Dispose();
        }

        public void SetProperties(IPropertySet configuration)
        {
            _configuration = configuration;
        }
    }
}

任何人都可以帮助我吗?非常感谢!

4

1 回答 1

0

通过对代码稍作调整,您可以在视频运行期间进行颜色更改,直到您共享相同的对象作为有效参数以及更改颜色的源对象,即

public class TheEffectParameter
{
    public Matrix5x4 ColorMatrix { get; set; }
}

public sealed class TheEffect: IBasicVideoEffect
{
    private IPropertySet _configuration;
    private CanvasDevice _canvasDevice;

    public void ProcessFrame(ProcessVideoFrameContext context)
    {
        using (var inputBitmap = CanvasBitmap.CreateFromDirect3D11Surface(_canvasDevice, context.InputFrame.Direct3DSurface))
        using (var drawingSurface = CanvasRenderTarget.CreateFromDirect3D11Surface(_canvasDevice, context.OutputFrame.Direct3DSurface).CreateDrawingSession())
        {
            Matrix5x4 matrix;
            if (ColorParameter == null)
            {
                //Default if parameter not passed
                matrix = new Matrix5x4() { M11 = 0.5f, M22 = 0.5f, M33 = 1.0f, M44 = 1.0f, M54 = 0.0f };
            }
            else
                matrix = ColorParameter.ColorMatrix;

            var colorEffect = new ColorMatrixEffect()
            {
                ColorMatrix = matrix,
                Source = inputBitmap
            };

            drawingSurface.DrawImage(colorEffect);
        }
    }

    public void SetEncodingProperties(VideoEncodingProperties encodingProperties, IDirect3DDevice device)
    {
        _canvasDevice = CanvasDevice.CreateFromDirect3D11Device(device);
    }

    public bool IsReadOnly { get { return false; } }

    public IReadOnlyList<VideoEncodingProperties> SupportedEncodingProperties
    {
        get
        {
            var properties = new List<VideoEncodingProperties>();
            properties.Add(VideoEncodingProperties.CreateUncompressed("ARGB32", 640, 480));
            return properties;
        }
    }

    public MediaMemoryTypes SupportedMemoryTypes { get { return MediaMemoryTypes.Gpu; } }

    public bool TimeIndependent { get { return false; } }

    public void DiscardQueuedFrames()
    {
    }

    public void Close(MediaEffectClosedReason reason)
    {
        if (_canvasDevice != null)
            _canvasDevice.Dispose();
    }

    public void SetProperties(IPropertySet configuration)
    {
        _configuration = configuration;
    }

    public TheEffectParameter ColorParameter
    {
        get
        {
            if (_configuration != null && _configuration.TryGetValue(nameof(ColorParameter), out object val))
                return (TheEffectParameter)val;
            return null;
        }
    }
}

主页上的变化很小:

TheEffectParameter parameter;
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);

    _mediaCapture = new MediaCapture();
    var settings = new MediaCaptureInitializationSettings()
    {
        StreamingCaptureMode = StreamingCaptureMode.Video,
    };
    await _mediaCapture.InitializeAsync(settings);
    captureElement.Source = _mediaCapture;
    await _mediaCapture.StartPreviewAsync();
    parameter = new TheEffectParameter {
        ColorMatrix = new Matrix5x4() { M11 = 0.5f, M22 = 0.5f, M33 = 1.0f, M44 = 1.0f, M54 = 0.0f }
    };
    var propertySet = new PropertySet();
    propertySet.Add(nameof(TheEffect.ColorParameter), parameter);

    var effect = await _mediaCapture.AddVideoEffectAsync(
        new VideoEffectDefinition(typeof(TheEffect).FullName,propertySet),
                                    MediaStreamType.VideoPreview);
}

public void ChangeColor(Matrix5x4 matrix)
{
    if (parameter == null)
        return;
    parameter.ColorMatrix = matrix;
}

这可能有一个错误,请原谅我,因为我使用你的动态编写此代码并且尚未测试,但它应该可以工作

于 2020-12-14T12:22:47.003 回答