PhotoHandler.cs 4.1 KB

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