我正在尝试创建一个UWP应用程序,该应用程序允许您修改相机动态显示的颜色。我当前的代码基于 Nikola Metulev 的有效示例,它通过使用 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;
}
}
}
谁能帮我解决?谢谢!
只需对代码进行一些调整,您就可以在视频运行期间进行颜色更改,直到您将同一对象作为实际参数共享,以及共享要更改颜色的源对象即
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;
}
这可能有一个错误原谅我,因为我使用你的代码在飞行中编写了这段代码并且没有测试过,但它应该可以工作