PhotoHandler.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. private void UpdatePhotoTaken(SoftwareBitmapSource photo)
  20. {
  21. Photo = photo;
  22. if (OnPhotoTaken == null) return;
  23. OnPhotoTaken(this, new PhotoTakenEventArgs(photo));
  24. }
  25. public async void Take()
  26. {
  27. CameraCaptureUI captureUI = new CameraCaptureUI();
  28. captureUI.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Png;
  29. captureUI.PhotoSettings.AllowCropping = false;
  30. captureUI.PhotoSettings.MaxResolution = CameraCaptureUIMaxPhotoResolution.Large3M;
  31. StorageFile photo = await captureUI.CaptureFileAsync(CameraCaptureUIMode.Photo);
  32. if (photo == null)
  33. {
  34. // User cancelled photo capture
  35. return;
  36. }
  37. IRandomAccessStream stream = await photo.OpenAsync(FileAccessMode.Read);
  38. BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
  39. SoftwareBitmap softwareBitmap = await decoder.GetSoftwareBitmapAsync();
  40. SoftwareBitmap softwareBitmapBGR8 = SoftwareBitmap.Convert(softwareBitmap,
  41. BitmapPixelFormat.Bgra8,
  42. BitmapAlphaMode.Premultiplied);
  43. SoftwareBitmapSource bitmapSource = new SoftwareBitmapSource();
  44. await bitmapSource.SetBitmapAsync(softwareBitmapBGR8);
  45. UpdatePhotoTaken(bitmapSource);
  46. }
  47. }
  48. }