PhotoHandler.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System;
  2. using System.Threading.Tasks;
  3. using Windows.Graphics.Imaging;
  4. using Windows.Media.Capture;
  5. using Windows.Storage;
  6. using Windows.Storage.Streams;
  7. using Windows.UI.Xaml.Media.Imaging;
  8. using YJMPD_UWP.Helpers.EventArgs;
  9. namespace YJMPD_UWP.Model
  10. {
  11. public class PhotoHandler
  12. {
  13. public delegate void OnPhotoTakenHandler(object sender, PhotoTakenEventArgs e);
  14. public event OnPhotoTakenHandler OnPhotoTaken;
  15. public SoftwareBitmapSource Photo { get; private set; }
  16. public PhotoHandler()
  17. {
  18. }
  19. public void Reset()
  20. {
  21. Photo = null;
  22. }
  23. private void UpdatePhotoTaken(SoftwareBitmapSource photo)
  24. {
  25. Photo = photo;
  26. if (OnPhotoTaken == null) return;
  27. OnPhotoTaken(this, new PhotoTakenEventArgs(photo));
  28. }
  29. public async void Take()
  30. {
  31. CameraCaptureUI captureUI = new CameraCaptureUI();
  32. captureUI.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Png;
  33. captureUI.PhotoSettings.AllowCropping = false;
  34. captureUI.PhotoSettings.MaxResolution = CameraCaptureUIMaxPhotoResolution.Large3M;
  35. StorageFile photo = await captureUI.CaptureFileAsync(CameraCaptureUIMode.Photo);
  36. if (photo == null)
  37. {
  38. return;
  39. }
  40. IRandomAccessStream stream = await photo.OpenAsync(FileAccessMode.Read);
  41. BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
  42. SoftwareBitmap softwareBitmap = await decoder.GetSoftwareBitmapAsync();
  43. SoftwareBitmap softwareBitmapBGR8 = SoftwareBitmap.Convert(softwareBitmap,
  44. BitmapPixelFormat.Bgra8,
  45. BitmapAlphaMode.Premultiplied);
  46. SoftwareBitmapSource bitmapSource = new SoftwareBitmapSource();
  47. await bitmapSource.SetBitmapAsync(softwareBitmapBGR8);
  48. UpdatePhotoTaken(bitmapSource);
  49. }
  50. }
  51. }