ImageTools.Desktop.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System.Drawing;
  2. using Windows.Media.Capture;
  3. using Windows.Media.MediaProperties;
  4. using Windows.Storage;
  5. using Avalonia.Controls;
  6. using Avalonia.Platform.Storage;
  7. using InABox.Core;
  8. using Microsoft.Maui.Media;
  9. namespace InABox.Avalonia.Platform.Desktop;
  10. public class Desktop_ImageTools : IImageTools
  11. {
  12. public Logger? Logger { get; set; }
  13. // private static MediaCapture? _captureManager;
  14. //
  15. // private async Task<MediaCapture> InitMediaCapture()
  16. // {
  17. // if (_captureManager == null)
  18. // {
  19. // _captureManager = new MediaCapture();
  20. // MediaCaptureInitializationSettings _settings = new();
  21. // await _captureManager.InitializeAsync(_settings);
  22. // }
  23. // return _captureManager;
  24. // }
  25. public byte[] CreateVideoThumbnail(byte[] video, int maxwidth, int maxheight)
  26. {
  27. Logger?.Error("CreateVideoThumbnail() is not implemented on this platform");
  28. return video;
  29. }
  30. public byte[] CreateThumbnail(byte[] image, int maxwidth, int maxheight)
  31. {
  32. Logger?.Error("CreateThumbnail() is not implemented on this platform");
  33. return image;
  34. }
  35. public async Task<ImageFile?> CapturePhotoAsync(TopLevel window, int? compression, Size? constraints)
  36. {
  37. // _captureManager = await InitMediaCapture();
  38. // var filename = $"{Path.GetFileNameWithoutExtension(Path.GetTempFileName())}.jpeg";
  39. // var location = await StorageFolder.GetFolderFromPathAsync(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures));
  40. // var storage = await location.CreateFileAsync(filename, CreationCollisionOption.GenerateUniqueName);
  41. // await _captureManager.CapturePhotoToStorageFileAsync(ImageEncodingProperties.CreateJpeg(), storage);
  42. // var data = await File.ReadAllBytesAsync(storage.Path);
  43. // var result = new ImageFile(storage.Name, data);
  44. // return result;
  45. // For the purposes of the desktop (demo) app, ignore the camera (above)
  46. // and always select a file
  47. return await PickPhotoAsync(window,compression,constraints);
  48. }
  49. public async Task<ImageFile?> PickPhotoAsync(TopLevel window, int? compression, Size? constraints)
  50. => await PickMediaFile(window, "Select Image", WellKnownFolder.Pictures, [FilePickerFileTypes.ImageAll]);
  51. public async Task<ImageFile?> CaptureVideoAsync(TopLevel window)
  52. {
  53. return await PickVideoAsync(window);
  54. }
  55. private FilePickerFileType VideoFiles = new("Video File")
  56. {
  57. Patterns = ["*.mpeg", "*.avi", "*.mov", "*.mkv", "*mp4"],
  58. AppleUniformTypeIdentifiers = ["public.movie"],
  59. MimeTypes = ["video/mp4", "video/quicktime", "video/webm", "video/avi", "video/mpeg"]
  60. };
  61. public async Task<ImageFile?> PickVideoAsync(TopLevel window)
  62. => await PickMediaFile(window, "Select Video", WellKnownFolder.Videos, [VideoFiles]);
  63. private static async Task<ImageFile?> PickMediaFile(TopLevel window, string caption, WellKnownFolder folder, FilePickerFileType[] filters)
  64. {
  65. var startlocation = await window.StorageProvider.TryGetWellKnownFolderAsync(folder);
  66. var files = await window.StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions()
  67. {
  68. Title = caption,
  69. FileTypeFilter = filters,
  70. AllowMultiple = false,
  71. SuggestedStartLocation = startlocation
  72. });
  73. var first = files.FirstOrDefault();
  74. if (first == null)
  75. return null;
  76. using var ms = new MemoryStream();
  77. await using var stream = await first.OpenReadAsync();
  78. await stream.CopyToAsync(ms);
  79. return new ImageFile(first.Name, ms.GetBuffer());
  80. }
  81. private async Task<ImageFile> ProcessFile(string filename, byte[]? data, int? compression, Size? constraints)
  82. {
  83. return new ImageFile(filename, data);
  84. }
  85. public byte[] RotateImage(byte[] image, float angle, int quality = 100)
  86. {
  87. Logger?.Error("RotateImage() is not implemented on this platform");
  88. return image;
  89. }
  90. public byte[] ScaleImage(byte[] image, Size? constraints, int quality = 100)
  91. {
  92. Logger?.Error("ScaleImage() is not implemented on this platform");
  93. return image;
  94. }
  95. }