PhotoHandler.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. // User cancelled photo capture
  39. return;
  40. }
  41. IRandomAccessStream stream = await photo.OpenAsync(FileAccessMode.Read);
  42. BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
  43. SoftwareBitmap softwareBitmap = await decoder.GetSoftwareBitmapAsync();
  44. SoftwareBitmap softwareBitmapBGR8 = SoftwareBitmap.Convert(softwareBitmap,
  45. BitmapPixelFormat.Bgra8,
  46. BitmapAlphaMode.Premultiplied);
  47. SoftwareBitmapSource bitmapSource = new SoftwareBitmapSource();
  48. await bitmapSource.SetBitmapAsync(softwareBitmapBGR8);
  49. UpdatePhotoTaken(bitmapSource);
  50. }
  51. }
  52. }