PhotoHandler.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. Status = PhotoStatus.NOPHOTO;
  26. }
  27. public void Reset()
  28. {
  29. Photo = null;
  30. Status = PhotoStatus.NOPHOTO;
  31. }
  32. public void SetPhoto(string photo)
  33. {
  34. Photo = photo;
  35. UpdateStatus(PhotoStatus.DONE);
  36. }
  37. private void UpdateStatus(PhotoStatus status)
  38. {
  39. Status = status;
  40. if (OnStatusUpdate == null) return;
  41. OnStatusUpdate(this, new PhotoStatusUpdatedEventArgs(status));
  42. }
  43. public async void Take()
  44. {
  45. UpdateStatus(PhotoStatus.TAKING);
  46. CameraCaptureUI captureUI = new CameraCaptureUI();
  47. captureUI.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Png;
  48. captureUI.PhotoSettings.AllowCropping = false;
  49. captureUI.PhotoSettings.MaxResolution = CameraCaptureUIMaxPhotoResolution.Large3M;
  50. StorageFile photo = await captureUI.CaptureFileAsync(CameraCaptureUIMode.Photo);
  51. if (photo == null)
  52. {
  53. UpdateStatus(PhotoStatus.NOPHOTO);
  54. return;
  55. }
  56. IRandomAccessStream stream = await photo.OpenAsync(FileAccessMode.Read);
  57. BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
  58. SoftwareBitmap softwareBitmap = await decoder.GetSoftwareBitmapAsync();
  59. SoftwareBitmap softwareBitmapBGR8 = SoftwareBitmap.Convert(softwareBitmap,
  60. BitmapPixelFormat.Bgra8,
  61. BitmapAlphaMode.Premultiplied);
  62. SoftwareBitmapSource bitmapSource = new SoftwareBitmapSource();
  63. await bitmapSource.SetBitmapAsync(softwareBitmapBGR8);
  64. UpdateStatus(PhotoStatus.UPLOADING);
  65. string photoURL = await UploadImage(stream.AsStream());
  66. Photo = photoURL;
  67. UpdateStatus(PhotoStatus.DONE);
  68. }
  69. public async Task<string> UploadImage(Stream file)
  70. {
  71. string url = "http://jancokock.me/f/?plain";
  72. MultipartFormDataContent postdata = new MultipartFormDataContent();
  73. postdata.Add(new ByteArrayContent(ReadFully(file)), "file", "capture.png");
  74. using (HttpClient hc = new HttpClient())
  75. {
  76. try
  77. {
  78. var response = await hc.PostAsync(url, postdata);
  79. response.EnsureSuccessStatusCode();
  80. string imageurl = await response.Content.ReadAsStringAsync();
  81. string imageid = new List<string>(imageurl.Split('/')).Last();
  82. return imageurl + "/" + imageid;
  83. }
  84. catch
  85. {
  86. throw new IOException("Network error");
  87. }
  88. }
  89. }
  90. public byte[] ReadFully(Stream input)
  91. {
  92. byte[] buffer = new byte[16 * 1024];
  93. using (MemoryStream ms = new MemoryStream())
  94. {
  95. int read;
  96. while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
  97. {
  98. ms.Write(buffer, 0, read);
  99. }
  100. return ms.ToArray();
  101. }
  102. }
  103. }
  104. }