From f68af2d8c73e8be9a9b351a630fcb4cc8e900774 Mon Sep 17 00:00:00 2001 From: Tobias Wohlleben Date: Mon, 20 Jan 2025 13:54:38 +0100 Subject: [PATCH] wip --- .../CamBooth.App/Core/Camera/CameraService.cs | 46 +++++-------------- .../LiveView/LiveViewPage.xaml.cs | 9 +++- src/CamBooth/CamBooth.App/MainWindow.xaml.cs | 11 +++-- ...PanelPage.xaml => PictureGalleryPage.xaml} | 4 +- ...age.xaml.cs => PictureGalleryPage.xaml.cs} | 34 ++++++++------ .../PictureGallery/PictureGalleryService.cs | 43 +++++++++-------- src/CamBooth/CamBooth.sln.DotSettings.user | 2 + 7 files changed, 72 insertions(+), 77 deletions(-) rename src/CamBooth/CamBooth.App/PictureGallery/{PicturePanelPage.xaml => PictureGalleryPage.xaml} (84%) rename src/CamBooth/CamBooth.App/PictureGallery/{PicturePanelPage.xaml.cs => PictureGalleryPage.xaml.cs} (59%) diff --git a/src/CamBooth/CamBooth.App/Core/Camera/CameraService.cs b/src/CamBooth/CamBooth.App/Core/Camera/CameraService.cs index c2b05df..910ec29 100644 --- a/src/CamBooth/CamBooth.App/Core/Camera/CameraService.cs +++ b/src/CamBooth/CamBooth.App/Core/Camera/CameraService.cs @@ -55,11 +55,11 @@ public class CameraService : IDisposable } catch (DllNotFoundException) { - ReportError("Canon DLLs not found!", true); + this.ReportError("Canon DLLs not found!"); } catch (Exception ex) { - ReportError(ex.Message, true); + this.ReportError(ex.Message); } } @@ -141,33 +141,9 @@ public class CameraService : IDisposable } - private void ReportError(string message, bool lockdown) + private void ReportError(string message) { - int errc; - lock (ErrLock) - { - errc = ++ErrCount; - } - - if (lockdown) - { - //this.EnableUI(false); - } - - switch (errc) - { - case < 4: - this._logger.Info(message); - break; - case 4: - this._logger.Info("Many errors happened!"); - break; - } - - lock (ErrLock) - { - ErrCount--; - } + this._logger.Info(message); } @@ -188,7 +164,7 @@ public class CameraService : IDisposable } catch (Exception ex) { - ReportError(ex.Message, false); + this.ReportError(ex.Message); } } @@ -220,7 +196,7 @@ public class CameraService : IDisposable } catch (Exception ex) { - ReportError(ex.Message, false); + this.ReportError(ex.Message); } } @@ -273,20 +249,20 @@ public class CameraService : IDisposable } catch (Exception ex) { - ReportError(ex.Message, false); + this.ReportError(ex.Message); } } private void ErrorHandler_NonSevereErrorHappened(object sender, ErrorCode ex) { - ReportError($"SDK Error code: {ex} ({((int)ex).ToString("X")})", false); + this.ReportError($"SDK Error code: {ex} ({((int)ex).ToString("X")})"); } private void ErrorHandler_SevereErrorHappened(object sender, Exception ex) { - ReportError(ex.Message, true); + this.ReportError(ex.Message); } #endregion @@ -296,11 +272,11 @@ public class CameraService : IDisposable { try { - MainCamera.TakePhotoAsync(); + MainCamera.TakePhoto(); } catch (Exception ex) { - ReportError(ex.Message, false); + this.ReportError(ex.Message); } } diff --git a/src/CamBooth/CamBooth.App/LiveView/LiveViewPage.xaml.cs b/src/CamBooth/CamBooth.App/LiveView/LiveViewPage.xaml.cs index d6c4ea9..d62f667 100644 --- a/src/CamBooth/CamBooth.App/LiveView/LiveViewPage.xaml.cs +++ b/src/CamBooth/CamBooth.App/LiveView/LiveViewPage.xaml.cs @@ -65,7 +65,14 @@ public partial class LiveViewPage : Page public void TakePhoto() { - this._cameraService.TakePhoto(); + try + { + this._cameraService.TakePhoto(); + } + catch (Exception e) + { + this._logger.Error(e.Message); + } } diff --git a/src/CamBooth/CamBooth.App/MainWindow.xaml.cs b/src/CamBooth/CamBooth.App/MainWindow.xaml.cs index ae7d47c..7d4b478 100644 --- a/src/CamBooth/CamBooth.App/MainWindow.xaml.cs +++ b/src/CamBooth/CamBooth.App/MainWindow.xaml.cs @@ -49,11 +49,12 @@ public partial class MainWindow : Window } - private void LoadPicturePanelAsync() + private async void LoadPicturePanelAsync() { - //this.PicturePanelPage.Visibility = Visibility.Hidden; - //this.PicturePanelPage.Navigate(new PicturePanelPage(this._appSettings, this._logger)); - this._pictureGalleryService.LoadThumbnailsToCache(); + //this.PictureGalleryPage.Visibility = Visibility.Hidden; + //this.PictureGalleryPage.Navigate(new PictureGalleryPage(this._appSettings, this._logger)); + // this._pictureGalleryService.LoadThumbnailsToCache(); + await this._pictureGalleryService.LoadThumbnailsToCache(); //this.DebugFrame.Navigate(new DebugConsolePage(this._logger)); } @@ -62,7 +63,7 @@ public partial class MainWindow : Window { if (visibility) { - this.PicturePanel.Navigate(new PicturePanelPage(this._appSettings, this._logger, this._pictureGalleryService)); + this.PicturePanel.Navigate(new PictureGalleryPage(this._appSettings, this._logger, this._pictureGalleryService)); } else { diff --git a/src/CamBooth/CamBooth.App/PictureGallery/PicturePanelPage.xaml b/src/CamBooth/CamBooth.App/PictureGallery/PictureGalleryPage.xaml similarity index 84% rename from src/CamBooth/CamBooth.App/PictureGallery/PicturePanelPage.xaml rename to src/CamBooth/CamBooth.App/PictureGallery/PictureGalleryPage.xaml index e8e0daf..659ed8c 100644 --- a/src/CamBooth/CamBooth.App/PictureGallery/PicturePanelPage.xaml +++ b/src/CamBooth/CamBooth.App/PictureGallery/PictureGalleryPage.xaml @@ -1,11 +1,11 @@ - diff --git a/src/CamBooth/CamBooth.App/PictureGallery/PicturePanelPage.xaml.cs b/src/CamBooth/CamBooth.App/PictureGallery/PictureGalleryPage.xaml.cs similarity index 59% rename from src/CamBooth/CamBooth.App/PictureGallery/PicturePanelPage.xaml.cs rename to src/CamBooth/CamBooth.App/PictureGallery/PictureGalleryPage.xaml.cs index cc41c5d..7d51ecb 100644 --- a/src/CamBooth/CamBooth.App/PictureGallery/PicturePanelPage.xaml.cs +++ b/src/CamBooth/CamBooth.App/PictureGallery/PictureGalleryPage.xaml.cs @@ -10,7 +10,7 @@ using Image = Wpf.Ui.Controls.Image; namespace CamBooth.App.PictureGallery; -public partial class PicturePanelPage : Page, IDisposable +public partial class PictureGalleryPage : Page, IDisposable { private readonly AppSettingsService _appSettingsService; @@ -19,7 +19,7 @@ public partial class PicturePanelPage : Page, IDisposable private readonly PictureGalleryService _pictureGalleryService; - public PicturePanelPage(AppSettingsService appSettingsService, Logger logger, PictureGalleryService pictureGalleryService) + public PictureGalleryPage(AppSettingsService appSettingsService, Logger logger, PictureGalleryService pictureGalleryService) { this._appSettingsService = appSettingsService; this._logger = logger; @@ -46,21 +46,25 @@ public partial class PicturePanelPage : Page, IDisposable { int loop = 0; - do - { - BitmapImage thumbnail = this._pictureGalleryService.ThumbnailsOrderedByNewestDescending[loop]; - - var imageControl = new Wpf.Ui.Controls.Image() + this.Dispatcher.Invoke( + () => { - Source = thumbnail, - Width = 388, - Margin = new Thickness(3) - }; + do + { + BitmapImage thumbnail = this._pictureGalleryService.ThumbnailsOrderedByNewestDescending[loop]; - this.PicturesPanel.Children.Add(imageControl); - loop++; - } - while ((loop < howManyPictures || howManyPictures == 0) && loop < this._pictureGalleryService.ThumbnailsOrderedByNewestDescending.Count); + var imageControl = new Wpf.Ui.Controls.Image() + { + Source = thumbnail, + Width = 388, + Margin = new Thickness(3) + }; + + this.PicturesPanel.Children.Add(imageControl); + loop++; + } + while ((loop < howManyPictures || howManyPictures == 0) && loop < this._pictureGalleryService.ThumbnailsOrderedByNewestDescending.Count); + }); } diff --git a/src/CamBooth/CamBooth.App/PictureGallery/PictureGalleryService.cs b/src/CamBooth/CamBooth.App/PictureGallery/PictureGalleryService.cs index 9add697..137ea7f 100644 --- a/src/CamBooth/CamBooth.App/PictureGallery/PictureGalleryService.cs +++ b/src/CamBooth/CamBooth.App/PictureGallery/PictureGalleryService.cs @@ -15,10 +15,10 @@ public class PictureGalleryService private readonly Logger _logger; - public List ThumbnailsOrderedByNewestDescending => this.thumbnails. - OrderByDescending(map => map.Key) - .Select(ordered => ordered.Value) - .ToList(); + public List ThumbnailsOrderedByNewestDescending => + this.thumbnails.OrderByDescending(map => map.Key) + .Select(ordered => ordered.Value) + .ToList(); private Dictionary thumbnails = new(); @@ -30,7 +30,7 @@ public class PictureGalleryService } - public void LoadThumbnailsToCache(int cacheSize = 0) + public async Task LoadThumbnailsToCache(int cacheSize = 0) { this._logger.Info("Start load thumbnails into cache"); string? pictureLocation = this._appSettings.PictureLocation; @@ -38,26 +38,30 @@ public class PictureGalleryService List picturePaths = Directory.EnumerateFiles(pictureLocation).ToList(); - do - { - string picturePath = picturePaths[loop]; - - DateTime creationTime = File.GetCreationTime(picturePath); - - // add if not exists - if (!this.thumbnails.ContainsKey(creationTime)) + await Task.Run( + () => { - this.thumbnails.Add(creationTime, CreateThumbnail(picturePath, 244, 200)); - this._logger.Info($"Thumbnail '{picturePath}' successfully created"); - } + do + { + string picturePath = picturePaths[loop]; - loop++; - } - while ((loop < cacheSize || cacheSize == 0) && loop < picturePaths.Count); + DateTime creationTime = File.GetCreationTime(picturePath); + // add if not exists + if (!this.thumbnails.ContainsKey(creationTime)) + { + this.thumbnails.Add(creationTime, CreateThumbnail(picturePath, 244, 200)); + this._logger.Info($"Thumbnail '{picturePath}' successfully created"); + } + + loop++; + } + while ((loop < cacheSize || cacheSize == 0) && loop < picturePaths.Count); + }); this._logger.Info("Loading thumbnails into cache completed"); } + public static BitmapImage CreateThumbnail(string filePath, int maxWidth, int maxHeight) { var bitmap = new BitmapImage(); @@ -66,6 +70,7 @@ public class PictureGalleryService bitmap.DecodePixelWidth = maxWidth; // Größe des ThumbnailsOrderedByNewestDescending direkt beim Dekodieren festlegen bitmap.CacheOption = BitmapCacheOption.OnLoad; bitmap.EndInit(); + bitmap.Freeze(); // Threadsicher machen return bitmap; } } \ No newline at end of file diff --git a/src/CamBooth/CamBooth.sln.DotSettings.user b/src/CamBooth/CamBooth.sln.DotSettings.user index d63f4c5..587c839 100644 --- a/src/CamBooth/CamBooth.sln.DotSettings.user +++ b/src/CamBooth/CamBooth.sln.DotSettings.user @@ -7,6 +7,8 @@ True True ForceIncluded + ForceIncluded + ForceIncluded ForceIncluded ForceIncluded ForceIncluded