PhotoHandler.cs 3.5 KB

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