-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathCameraEffectExample.xaml.cs
More file actions
231 lines (188 loc) · 7.93 KB
/
Copy pathCameraEffectExample.xaml.cs
File metadata and controls
231 lines (188 loc) · 7.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
using ExampleGallery.Effects;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Windows.Foundation.Collections;
using Windows.Graphics.Display;
using Windows.Media.Capture;
using Windows.Media.Devices;
using Windows.Media.Effects;
using Windows.Media.MediaProperties;
using Windows.Storage.Streams;
using Windows.UI.Core;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
namespace ExampleGallery
{
public sealed partial class CameraEffectExample : UserControl, ICustomThumbnailSource
{
private MediaCapture mediaCapture;
private TaskCompletionSource<object> hasLoaded = new TaskCompletionSource<object>();
private Task changeEffectTask = null;
private IPropertySet effectPropertySet = null; //defined in class scope so that Slider_ValueChanged can modify values
const string noEffect = "No effect";
const string displacementEffect = "Displacement effect";
const string rotatingTilesEffect = "Rotating tiles effect";
const string gaussianBlurEffect = "Gaussian Blur effect";
public List<string> PossibleEffects
{
get
{
return new List<string> { noEffect, displacementEffect, rotatingTilesEffect, gaussianBlurEffect };
}
}
public CameraEffectExample()
{
this.InitializeComponent();
DisplayInformation.GetForCurrentView().OrientationChanged += OnOrientationChanged;
}
#region Rotate the preview to match the device orientation
//
// Adapted from https://github.com/Microsoft/Windows-universal-samples/blob/master/Samples/CameraStarterKit/cs/MainPage.xaml.cs
//
private async void OnOrientationChanged(DisplayInformation sender, object args)
{
await SetPreviewRotation();
}
private static readonly Guid RotationKey = new Guid("C380465D-2271-428C-9B83-ECEA3B4A85C1");
private async Task SetPreviewRotation()
{
if (mediaCapture == null)
return;
var orientation = DisplayInformation.GetForCurrentView().CurrentOrientation;
var rotationDegrees = ConvertDisplayOrientationToDegrees(orientation);
var props = mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview);
props.Properties.Add(RotationKey, rotationDegrees);
await mediaCapture.SetEncodingPropertiesAsync(MediaStreamType.VideoPreview, props, null);
}
private static int ConvertDisplayOrientationToDegrees(DisplayOrientations orientation)
{
switch (orientation)
{
case DisplayOrientations.Portrait:
return 90;
case DisplayOrientations.LandscapeFlipped:
return 180;
case DisplayOrientations.PortraitFlipped:
return 270;
case DisplayOrientations.Landscape:
default:
return 0;
}
}
#endregion
private void OnUnloaded(object sender, RoutedEventArgs e)
{
var action = mediaCapture.StopPreviewAsync();
mediaCapture.Failed -= mediaCapture_Failed;
DisplayInformation.GetForCurrentView().OrientationChanged -= OnOrientationChanged;
}
private void mediaCapture_Failed(MediaCapture sender, MediaCaptureFailedEventArgs errorEventArgs)
{
var action = Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
progressText.Text = "MediaCapture failed: " + errorEventArgs.Message;
});
}
private async void OnLoaded(object sender, RoutedEventArgs e)
{
this.effectCombo.SelectedItem = noEffect;
this.captureElement.Visibility = Visibility.Collapsed;
this.progressRing.IsActive = true;
await CreateMediaCapture();
if (ThumbnailGenerator.IsDrawingThumbnail && captureElement.Source != null)
{
await SetEffectWorker(rotatingTilesEffect);
customThumbnail = new InMemoryRandomAccessStream();
await mediaCapture.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpeg(), customThumbnail);
}
this.captureElement.Visibility = Visibility.Visible;
this.progressRing.IsActive = false;
this.progressRing.Visibility = Visibility.Collapsed;
hasLoaded.SetResult(null);
}
private async Task CreateMediaCapture()
{
mediaCapture = new MediaCapture();
mediaCapture.Failed += mediaCapture_Failed;
var settings = new MediaCaptureInitializationSettings()
{
StreamingCaptureMode = StreamingCaptureMode.Video
};
try
{
await mediaCapture.InitializeAsync(settings);
}
catch (Exception)
{
this.progressText.Text = "No camera is available.";
return;
}
captureElement.Source = mediaCapture;
await mediaCapture.StartPreviewAsync();
await SetPreviewRotation();
}
private async Task ResetEffectsAsync()
{
if (mediaCapture.CameraStreamState == CameraStreamState.Streaming)
{
await mediaCapture.ClearEffectsAsync(MediaStreamType.VideoPreview);
}
}
private void EffectCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string effect = noEffect;
if (e.AddedItems.Count != 0)
effect = (string)e.AddedItems[0];
var task = SetEffect(effect);
}
private void BlurAmountSlider_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
{
if (effectPropertySet == null) return;
effectPropertySet["BlurAmount"] = e.NewValue;
}
private async Task SetEffect(string effect)
{
// wait for OnLoaded to complete
await hasLoaded.Task;
// wait for any previous SetEffect to finish
if (changeEffectTask != null)
{
await changeEffectTask;
}
await (changeEffectTask = SetEffectWorker(effect));
}
private async Task SetEffectWorker(string effect)
{
await ResetEffectsAsync();
blurAmountSlider.Visibility = Visibility.Collapsed;
effectPropertySet = new PropertySet();
string typeName = null;
switch (effect)
{
case displacementEffect:
typeName = typeof(DisplacementEffect).FullName;
break;
case rotatingTilesEffect:
typeName = typeof(RotatedTilesEffect).FullName;
break;
case gaussianBlurEffect:
typeName = typeof(DynamicBlurVideoEffect).FullName;
effectPropertySet["BlurAmount"] = blurAmountSlider.Value;
blurAmountSlider.Visibility = Visibility.Visible;
break;
}
if (typeName == null)
return;
await mediaCapture.AddVideoEffectAsync(new VideoEffectDefinition(typeName, effectPropertySet),
MediaStreamType.VideoPreview);
}
// This example generates a custom thumbnail image (not just a rendering capture like most examples).
IRandomAccessStream ICustomThumbnailSource.Thumbnail { get { return customThumbnail; } }
IRandomAccessStream customThumbnail;
}
}