【问题标题】:Modify IBasicVideoEffect values on the fly即时修改 IBasicVideoEffect 值
【发布时间】:2017-10-25 07:41:06
【问题描述】:

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

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

这是我的代码:

MainPage 类:

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);
        }
    }
}

ColorEffect 类:

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;
        }
    }
}

谁能帮帮我?非常感谢!

【问题讨论】:

    标签: c# visual-studio uwp video-effects


    【解决方案1】:

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

    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;
    }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-08
      • 2018-06-09
      • 2011-04-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多