-
Notifications
You must be signed in to change notification settings - Fork 493
Expand file tree
/
Copy pathCameraViewPage.xaml.cs
More file actions
149 lines (124 loc) · 4.09 KB
/
CameraViewPage.xaml.cs
File metadata and controls
149 lines (124 loc) · 4.09 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
using CommunityToolkit.Maui.Core;
using CommunityToolkit.Maui.Sample.ViewModels.Views;
using CommunityToolkit.Maui.Storage;
namespace CommunityToolkit.Maui.Sample.Pages.Views;
public sealed partial class CameraViewPage : BasePage<CameraViewViewModel>
{
readonly IFileSaver fileSaver;
readonly string imagePath;
Stream videoRecordingStream = Stream.Null;
public CameraViewPage(CameraViewViewModel viewModel, IFileSystem fileSystem, IFileSaver fileSaver) : base(viewModel)
{
InitializeComponent();
this.fileSaver = fileSaver;
imagePath = Path.Combine(fileSystem.CacheDirectory, "camera-view-image.jpg");
Camera.MediaCaptured += OnMediaCaptured;
}
protected override async void OnAppearing()
{
base.OnAppearing();
var packagingModel = AppInfo.Current.PackagingModel;
PermissionStatus microphonePermissionsRequest;
PermissionStatus cameraPermissionsRequest;
if (packagingModel == AppPackagingModel.Packaged || OperatingSystem.IsAndroid() || OperatingSystem.IsIOS() || OperatingSystem.IsMacCatalyst())
{
cameraPermissionsRequest = await Permissions.RequestAsync<Permissions.Camera>();
microphonePermissionsRequest = await Permissions.RequestAsync<Permissions.Microphone>();
}
else
{
// Permissions are automatically granted for unpackaged apps, so we can just set them to Granted.
microphonePermissionsRequest = PermissionStatus.Granted;
cameraPermissionsRequest = PermissionStatus.Granted;
}
if (cameraPermissionsRequest is not PermissionStatus.Granted)
{
await Shell.Current.CurrentPage.DisplayAlertAsync("Camera permission is not granted.", "Please grant the permission to use this feature.", "OK");
return;
}
if (microphonePermissionsRequest is not PermissionStatus.Granted)
{
await Shell.Current.CurrentPage.DisplayAlertAsync("Microphone permission is not granted.", "Please grant the permission to use this feature.", "OK");
return;
}
}
// https://github.com/dotnet/maui/issues/15833
protected override void OnNavigatedFrom(NavigatedFromEventArgs args)
{
base.OnNavigatedFrom(args);
if (!Shell.Current.Navigation.NavigationStack.Contains(this))
{
Cleanup();
}
}
async void OnImageTapped(object? sender, TappedEventArgs args)
{
if (image.Source is null || image.Source.IsEmpty)
{
return;
}
await Navigation.PushAsync(new ImageViewPage(imagePath));
}
void Cleanup()
{
Camera.MediaCaptured -= OnMediaCaptured;
}
void OnMediaCaptured(object? sender, MediaCapturedEventArgs e)
{
using var localFileStream = File.Create(imagePath);
e.Media.CopyTo(localFileStream);
Dispatcher.Dispatch(() =>
{
// workaround for https://github.com/dotnet/maui/issues/13858
#if ANDROID
image.Source = ImageSource.FromStream(() => File.OpenRead(imagePath));
#else
image.Source = ImageSource.FromFile(imagePath);
#endif
debugText.Text = $"Image saved to {imagePath}";
});
}
void ZoomIn(object? sender, EventArgs? e)
{
Camera.ZoomFactor += 1.0f;
}
void ZoomOut(object? sender, EventArgs? e)
{
Camera.ZoomFactor -= 1.0f;
}
async void SetNightMode(object? sender, EventArgs? e)
{
#if ANDROID
await Camera.SetExtensionMode(AndroidX.Camera.Extensions.ExtensionMode.Night);
#else
await Task.CompletedTask;
#endif
}
async void StartCameraRecording(object? sender, EventArgs? e)
{
await Camera.StartVideoRecording(CancellationToken.None);
}
async void StopCameraRecording(object? sender, EventArgs? e)
{
videoRecordingStream = await Camera.StopVideoRecording(CancellationToken.None);
}
async void SaveVideo(object? sender, EventArgs? e)
{
if (videoRecordingStream == Stream.Null)
{
await DisplayAlertAsync("Unable to Save Video", "Stream is null", "OK");
}
else
{
var status = await Permissions.RequestAsync<Permissions.StorageWrite>();
if (status is not PermissionStatus.Granted)
{
await Shell.Current.CurrentPage.DisplayAlertAsync("Storage permission is not granted.", "Please grant the permission to use this feature.", "OK");
return;
}
await fileSaver.SaveAsync("recording.mp4", videoRecordingStream);
await videoRecordingStream.DisposeAsync();
videoRecordingStream = Stream.Null;
}
}
}