我正在尝试创建一个 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;
}
}
}
任何人都可以帮助我吗?非常感谢!