PhotoHandler.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Net.Http;
  5. using System.Threading.Tasks;
  6. using Windows.Graphics.Imaging;
  7. using Windows.Media.Capture;
  8. using Windows.Storage;
  9. using Windows.Storage.Streams;
  10. using Windows.UI.Xaml.Media.Imaging;
  11. using YJMPD_UWP.Helpers.EventArgs;
  12. namespace YJMPD_UWP.Model
  13. {
  14. public class PhotoHandler
  15. {
  16. public delegate void OnPhotoTakenHandler(object sender, PhotoTakenEventArgs e);
  17. public event OnPhotoTakenHandler OnPhotoTaken;
  18. public SoftwareBitmapSource Photo { get; private set; }
  19. public PhotoHandler()
  20. {
  21. }
  22. public void Reset()
  23. {
  24. Photo = null;
  25. }
  26. private void UpdatePhotoTaken(SoftwareBitmapSource photo)
  27. {
  28. Photo = photo;
  29. if (OnPhotoTaken == null) return;
  30. OnPhotoTaken(this, new PhotoTakenEventArgs(photo));
  31. }
  32. public async void Take()
  33. {
  34. CameraCaptureUI captureUI = new CameraCaptureUI();
  35. captureUI.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Png;
  36. captureUI.PhotoSettings.AllowCropping = false;
  37. captureUI.PhotoSettings.MaxResolution = CameraCaptureUIMaxPhotoResolution.Large3M;
  38. StorageFile photo = await captureUI.CaptureFileAsync(CameraCaptureUIMode.Photo);
  39. if (photo == null)
  40. {
  41. return;
  42. }
  43. IRandomAccessStream stream = await photo.OpenAsync(FileAccessMode.Read);
  44. BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
  45. SoftwareBitmap softwareBitmap = await decoder.GetSoftwareBitmapAsync();
  46. SoftwareBitmap softwareBitmapBGR8 = SoftwareBitmap.Convert(softwareBitmap,
  47. BitmapPixelFormat.Bgra8,
  48. BitmapAlphaMode.Premultiplied);
  49. SoftwareBitmapSource bitmapSource = new SoftwareBitmapSource();
  50. await bitmapSource.SetBitmapAsync(softwareBitmapBGR8);
  51. await UploadImage(stream.AsStream());
  52. UpdatePhotoTaken(bitmapSource);
  53. }
  54. public async Task<string> UploadImage(Stream file)
  55. {
  56. string url = "http://jancokock.me/f/?plain";
  57. MultipartFormDataContent postdata = new MultipartFormDataContent();
  58. postdata.Add(new ByteArrayContent(ReadFully(file)), "file", "capture.png");
  59. using (HttpClient hc = new HttpClient())
  60. {
  61. try
  62. {
  63. var response = await hc.PostAsync(url, postdata);
  64. response.EnsureSuccessStatusCode();
  65. Debug.WriteLine(await response.Content.ReadAsStringAsync());
  66. return await response.Content.ReadAsStringAsync();
  67. }
  68. catch
  69. {
  70. throw new IOException("Network error");
  71. }
  72. }
  73. }
  74. public byte[] ReadFully(Stream input)
  75. {
  76. byte[] buffer = new byte[16 * 1024];
  77. using (MemoryStream ms = new MemoryStream())
  78. {
  79. int read;
  80. while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
  81. {
  82. ms.Write(buffer, 0, read);
  83. }
  84. return ms.ToArray();
  85. }
  86. }
  87. }
  88. }