Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ObservableImageField.cs
1using System;
2using System.IO;
3using System.Threading.Tasks;
4using Microsoft.Maui.Storage;
8using Microsoft.Maui.Controls;
9using CommunityToolkit.Mvvm.Input;
11using CommunityToolkit.Mvvm.ComponentModel;
12
14{
16 {
17 public override string? StringValue
18 {
19 get => this.RawValue as string;
20 set {
21 this.RawValue = value;
22 this.ImageSource = ImageSource.FromStream(() => new MemoryStream(Convert.FromBase64String(this.RawValue as string ?? string.Empty)));
23 }
24 }
25
26 public int? TargetWidth
27 {
28 get
29 {
30 object? Value;
31 return this.Metadata.TryGetValue("TargetWidth", out Value) ? Value as int? : null;
32 }
33 }
34 public int? TargetHeight
35 {
36 get
37 {
38 object? Value;
39 return this.Metadata.TryGetValue("TargetHeight", out Value) ? Value as int? : null;
40 }
41 }
42 public double? AspectRatio
43 {
44 get
45 {
46 object? Value;
47 return this.Metadata.TryGetValue("AspectRatio", out Value) ? Value as double? : null;
48 }
49 }
50 public bool? ShouldCrop
51 {
52 get
53 {
54 object? Value;
55 return this.Metadata.TryGetValue("ShouldCrop", out Value) ? Value as bool? : null;
56 }
57 }
58
59 [ObservableProperty]
60 private ImageSource? imageSource;
61
62 [ObservableProperty]
63 private bool allowUpload = true;
64
65 [RelayCommand]
66 private async Task PickPhoto()
67 {
68 try
69 {
70 FileResult? FileResult = await MediaPicker.Default.PickPhotoAsync(new MediaPickerOptions()
71 {
73 });
74
75 if (FileResult is null)
76 return;
77
78 Stream FileStream = await FileResult.OpenReadAsync();
79 byte[] InputBin = await ToByteArrayAsync(FileStream) ?? throw new Exception("Failed to read photo stream");
80
81 TaskCompletionSource<byte[]?> Tcs = new();
82 await ServiceRef.NavigationService.GoToAsync(
83 nameof(ImageCroppingPage),
84 new ImageCroppingNavigationArgs(ImageSource.FromStream(() => new MemoryStream(InputBin)), Tcs)
85 );
86
87 byte[] OutputBin = await Tcs.Task ?? throw new Exception("Failed to crop photo");
88 this.RawValue = Convert.ToBase64String(OutputBin);
89
90 this.ImageSource = ImageSource.FromStream(() => new MemoryStream(Convert.FromBase64String(this.RawValue as string ?? string.Empty)));
91 }
92 catch (Exception Ex)
93 {
94 ServiceRef.LogService.LogException(Ex);
95 await ServiceRef.UiService.DisplayException(Ex);
96 }
97 }
98
99 [RelayCommand]
100 private async Task TakePhoto()
101 {
102 bool Permitted = await ServiceRef.PermissionService.CheckCameraPermissionAsync();
103
104 if (!Permitted)
105 return;
106
107 try
108 {
109 FileResult? FileResult = await MediaPicker.Default.CapturePhotoAsync(new MediaPickerOptions()
110 {
112 });
113
114 if (FileResult is null)
115 return;
116
117 Stream FileStream = await FileResult.OpenReadAsync();
118
119 byte[] InputBin = await ToByteArrayAsync(FileStream) ?? throw new Exception("Failed to read photo stream");
120
121 TaskCompletionSource<byte[]?> Tcs = new();
122 await ServiceRef.NavigationService.GoToAsync(nameof(ImageCroppingPage), new ImageCroppingNavigationArgs(ImageSource.FromStream(() => new MemoryStream(InputBin)), Tcs));
123
124 byte[] OutputBin = await Tcs.Task ?? throw new Exception("Failed to crop photo");
125
126 this.RawValue = Convert.ToBase64String(OutputBin);
127
128 this.ImageSource = ImageSource.FromStream(() => new MemoryStream(Convert.FromBase64String(this.RawValue as string ?? string.Empty)));
129 }
130 catch (Exception Ex)
131 {
132 ServiceRef.LogService.LogException(Ex);
133 await ServiceRef.UiService.DisplayAlert(
136 }
137 }
138
139 private static async Task<byte[]?> ToByteArrayAsync(System.IO.Stream stream)
140 {
141 if (stream is null)
142 return null;
143 using (System.IO.MemoryStream MemoryStream = new System.IO.MemoryStream())
144 {
145 await stream.CopyToAsync(MemoryStream);
146 return MemoryStream.ToArray();
147 }
148 }
149 }
150}
A strongly-typed resource class, for looking up localized strings, etc.
static string TakePhotoOfYourself
Looks up a localized string similar to Take a Photo of yourself.
static string FailedToLoadPhoto
Looks up a localized string similar to Failed to load photo.
static string ErrorTitle
Looks up a localized string similar to An error has occurred.
static string PickPhotoOfYourself
Looks up a localized string similar to Pick a photo of yourself.
Dictionary< string, object?> Metadata
Arbitrary metadata for field-specific extensions. Use e.g. Metadata["TargetWidth"] or Metadata["MaxFi...
Base class that references services in the app.
Definition: ServiceRef.cs:43
static ILogService LogService
Log service.
Definition: ServiceRef.cs:214
static IUiService UiService
Service serializing and managing UI-related tasks.
Definition: ServiceRef.cs:130
static INavigationService NavigationService
The navigation service for navigating between pages.
Definition: ServiceRef.cs:178
static IReportingStringLocalizer Localizer
Localization service
Definition: ServiceRef.cs:370
static IPermissionService PermissionService
Permission Service
Definition: ServiceRef.cs:358
Holds navigation parameters for opening the Image Cropping page.