This repository was archived by the owner on Sep 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainPage.xaml.cs
236 lines (177 loc) · 8.18 KB
/
MainPage.xaml.cs
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
232
233
234
235
236
/*
* Copyright © 2013 Nokia Corporation. All rights reserved.
* Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation.
* Other product and company names mentioned herein may be trademarks
* or trade names of their respective owners.
* See LICENSE.TXT for license information.
*/
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using RealtimeFilterDemo.Resources;
using System;
using System.Linq;
using System.Threading;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Navigation;
using Windows.Foundation;
using Windows.Phone.Media.Capture;
namespace RealtimeFilterDemo
{
public partial class MainPage : PhoneApplicationPage
{
private MediaElement _mediaElement = null;
private PhotoCaptureDevice _photoCaptureDevice = null;
private NokiaImagingSDKEffects _cameraEffect = null;
private CameraStreamSource _cameraStreamSource = null;
private Semaphore _cameraSemaphore = new Semaphore(1, 1);
public MainPage()
{
InitializeComponent();
ApplicationBar = new ApplicationBar();
var previousButton = new ApplicationBarIconButton(new Uri("/Assets/Icons/previous.png", UriKind.Relative));
previousButton.Text = AppResources.PreviousEffectButtonText;
previousButton.Click += PreviousButton_Click;
ApplicationBar.Buttons.Add(previousButton);
var nextButton = new ApplicationBarIconButton(new Uri("/Assets/Icons/next.png", UriKind.Relative));
nextButton.Text = AppResources.NextEffectButtonText;
nextButton.Click += NextButton_Click;
ApplicationBar.Buttons.Add(nextButton);
var aboutMenuItem = new ApplicationBarMenuItem();
aboutMenuItem.Text = AppResources.AboutPageButtonText;
aboutMenuItem.Click += AboutMenuItem_Click;
ApplicationBar.MenuItems.Add(aboutMenuItem);
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
Initialize();
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
base.OnNavigatedFrom(e);
while (!_cameraSemaphore.WaitOne(100));
Uninitialize();
_cameraSemaphore.Release();
}
protected override void OnOrientationChanged(OrientationChangedEventArgs e)
{
base.OnOrientationChanged(e);
AdjustOrientation();
}
private void AdjustOrientation()
{
if (_photoCaptureDevice != null)
{
double canvasAngle;
if (Orientation.HasFlag(PageOrientation.LandscapeLeft))
{
canvasAngle = _photoCaptureDevice.SensorRotationInDegrees - 90;
}
else if (Orientation.HasFlag(PageOrientation.LandscapeRight))
{
canvasAngle = _photoCaptureDevice.SensorRotationInDegrees + 90;
}
else // PageOrientation.PortraitUp
{
canvasAngle = _photoCaptureDevice.SensorRotationInDegrees;
}
var tmptransform = new RotateTransform() { Angle = canvasAngle };
var previewSize = tmptransform.TransformBounds(
new System.Windows.Rect(
new System.Windows.Point(),
new System.Windows.Size(_photoCaptureDevice.PreviewResolution.Width, _photoCaptureDevice.PreviewResolution.Height)
)
);
double s1 = viewfinderCanvas.ActualWidth / previewSize.Width;
double s2 = viewfinderCanvas.ActualHeight / previewSize.Height;
//video center match Viewfinder canvas center center
BackgroundVideoBrush.AlignmentX = AlignmentX.Center;
BackgroundVideoBrush.AlignmentY = AlignmentY.Center;
//Don't use a strech strategie.
BackgroundVideoBrush.Stretch = Stretch.None;
double scale = Math.Max(s1, s2); //UniformFill
// double scale = Math.Min(s1, s2); // Uniform
if (_photoCaptureDevice.SensorLocation == CameraSensorLocation.Back)
{
BackgroundVideoBrush.Transform = new CompositeTransform() { Rotation = canvasAngle, CenterX = viewfinderCanvas.ActualWidth / 2, CenterY = viewfinderCanvas.ActualHeight / 2, ScaleX = scale, ScaleY = scale };
}
else
{
//Front viewfinder need to be flipped
BackgroundVideoBrush.Transform = new CompositeTransform() { Rotation = canvasAngle, CenterX = viewfinderCanvas.ActualWidth / 2, CenterY = viewfinderCanvas.ActualHeight / 2, ScaleX = -scale, ScaleY = scale };
}
_photoCaptureDevice.SetProperty(KnownCameraGeneralProperties.EncodeWithOrientation, canvasAngle);
}
}
private async void Initialize()
{
StatusTextBlock.Text = AppResources.MainPage_StatusTextBlock_StartingCamera;
var camera = CameraSensorLocation.Back;
// var camera = CameraSensorLocation.Front;
var resolution = PhotoCaptureDevice.GetAvailablePreviewResolutions(camera).Last();
_photoCaptureDevice = await PhotoCaptureDevice.OpenAsync(camera, resolution);
await _photoCaptureDevice.SetPreviewResolutionAsync(resolution);
_cameraEffect = new NokiaImagingSDKEffects();
_cameraEffect.PhotoCaptureDevice = _photoCaptureDevice;
_cameraStreamSource = new CameraStreamSource(_cameraEffect, resolution);
_cameraStreamSource.FrameRateChanged += CameraStreamSource_FPSChanged;
_mediaElement = new MediaElement();
_mediaElement.BufferingTime = new TimeSpan(0);
_mediaElement.SetSource(_cameraStreamSource);
// Using VideoBrush in XAML instead of MediaElement, because otherwise
// CameraStreamSource.CloseMedia() does not seem to be called by the framework:/
BackgroundVideoBrush.SetSource(_mediaElement);
_cameraEffect.PreviousEffect();
StatusTextBlock.Text = _cameraEffect.EffectName;
AdjustOrientation();
}
private void Uninitialize()
{
StatusTextBlock.Text = "";
if (_mediaElement != null)
{
_mediaElement.Source = null;
_mediaElement = null;
}
if (_cameraStreamSource != null)
{
_cameraStreamSource.FrameRateChanged -= CameraStreamSource_FPSChanged;
_cameraStreamSource = null;
}
_cameraEffect = null;
if (_photoCaptureDevice != null)
{
_photoCaptureDevice.Dispose();
_photoCaptureDevice = null;
}
}
private void AboutMenuItem_Click(object sender, EventArgs e)
{
NavigationService.Navigate(new Uri("/AboutPage.xaml", UriKind.Relative));
}
private void NextButton_Click(object sender, EventArgs e)
{
_cameraEffect.NextEffect();
StatusTextBlock.Text = _cameraEffect.EffectName;
}
private void PreviousButton_Click(object sender, EventArgs e)
{
_cameraEffect.PreviousEffect();
StatusTextBlock.Text = _cameraEffect.EffectName;
}
private void CameraStreamSource_FPSChanged(object sender, int e)
{
FrameRateTextBlock.Text = String.Format(AppResources.MainPage_FrameRateTextBlock_Format, e);
//StatusTextBlock.Text = string.Concat("QuantizeColorEffect with Cache count: ", App.AssignedColorCache.Count);
}
private async void LayoutRoot_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
if (_cameraSemaphore.WaitOne(100))
{
await _photoCaptureDevice.FocusAsync();
_cameraSemaphore.Release();
}
}
}
}