MobileDocument.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. using System;
  2. using System.IO;
  3. using System.Threading.Tasks;
  4. using Plugin.Media;
  5. using Plugin.Media.Abstractions;
  6. using Syncfusion.Pdf;
  7. using Syncfusion.Pdf.Graphics;
  8. using Xamarin.Essentials;
  9. namespace InABox.Mobile
  10. {
  11. public enum MobileDocumentSize
  12. {
  13. Small,
  14. Medium,
  15. Large,
  16. Full
  17. }
  18. public enum MobileDocumentQuality
  19. {
  20. Low,
  21. Medium,
  22. High,
  23. }
  24. public interface IMobileDocumentOptions
  25. {
  26. }
  27. public class MobileDocumentCameraOptions : IMobileDocumentOptions
  28. {
  29. public MobileDocumentSize Size { get; set; }
  30. public int Compression { get; set; }
  31. public MobileDocumentCameraOptions()
  32. {
  33. Size = MobileDocumentSize.Medium;
  34. Compression = 10;
  35. }
  36. }
  37. public class MobileDocumentPhotoLibraryOptions : IMobileDocumentOptions
  38. {
  39. public MobileDocumentSize Size { get; set; }
  40. public int Compression { get; set; }
  41. public MobileDocumentPhotoLibraryOptions()
  42. {
  43. Size = MobileDocumentSize.Medium;
  44. Compression = 10;
  45. }
  46. }
  47. public class MobileDocumentVideoOptions : IMobileDocumentOptions
  48. {
  49. public MobileDocumentSize Size { get; set; }
  50. public int Compression { get; set; }
  51. public MobileDocumentQuality Quality { get; set; }
  52. public MobileDocumentVideoOptions()
  53. {
  54. Size = MobileDocumentSize.Medium;
  55. Compression = 10;
  56. Quality = MobileDocumentQuality.Medium;
  57. }
  58. }
  59. public class MobileDocumentVideoLibraryOptions : IMobileDocumentOptions
  60. {
  61. // Picking a video actually doesnt have any options
  62. }
  63. public abstract class MobileDocumentSource
  64. {
  65. public abstract Task<MobileDocument> From();
  66. }
  67. public abstract class MobileDocumentSource<TOptions> : MobileDocumentSource where TOptions : class, IMobileDocumentOptions, new()
  68. {
  69. protected TOptions Options { get; }
  70. protected MobileDocumentSource(TOptions options = null)
  71. {
  72. Options = options ?? new TOptions();
  73. }
  74. protected abstract bool IsEnabled { get; }
  75. protected abstract Task<MediaFile> Capture();
  76. public override async Task<MobileDocument> From()
  77. {
  78. await CrossMedia.Current.Initialize();
  79. var result = new MobileDocument();
  80. if (IsEnabled)
  81. {
  82. try
  83. {
  84. var file = await Capture();
  85. if (file != null)
  86. {
  87. result.FileName = Path.GetFileName(file.OriginalFilename ?? file.Path);
  88. await using (var stream = file.GetStream())
  89. {
  90. BinaryReader br = new BinaryReader(stream);
  91. result.Data = br.ReadBytes((int)stream.Length);
  92. }
  93. }
  94. }
  95. catch (FeatureNotSupportedException fnsEx)
  96. {
  97. MobileLogging.Log(fnsEx, "Capture(FNS)");
  98. }
  99. catch (PermissionException pEx)
  100. {
  101. MobileLogging.Log(pEx, "Capture(PERM)");
  102. }
  103. catch (Exception ex)
  104. {
  105. MobileLogging.Log(ex, "Capture(ERR)");
  106. }
  107. }
  108. return result;
  109. }
  110. }
  111. public class MobileDocumentCameraSource : MobileDocumentSource<MobileDocumentCameraOptions>
  112. {
  113. public MobileDocumentCameraSource(MobileDocumentCameraOptions options = null) : base(options)
  114. {
  115. }
  116. protected override bool IsEnabled =>
  117. CrossMedia.Current.IsCameraAvailable && CrossMedia.Current.IsTakePhotoSupported;
  118. protected override async Task<MediaFile> Capture()
  119. {
  120. return await CrossMedia.Current.TakePhotoAsync(
  121. new StoreCameraMediaOptions()
  122. {
  123. Name = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}.png",
  124. CompressionQuality = Options.Compression,
  125. PhotoSize = Options.Size switch
  126. {
  127. MobileDocumentSize.Small => PhotoSize.Small,
  128. MobileDocumentSize.Large => PhotoSize.Large,
  129. MobileDocumentSize.Full => PhotoSize.Full,
  130. _ => PhotoSize.Medium
  131. },
  132. }
  133. );
  134. }
  135. }
  136. public class MobileDocumentPhotoLibrarySource : MobileDocumentSource<MobileDocumentPhotoLibraryOptions>
  137. {
  138. public MobileDocumentPhotoLibrarySource(MobileDocumentPhotoLibraryOptions options = null) : base(options)
  139. {
  140. }
  141. protected override bool IsEnabled => CrossMedia.Current.IsPickPhotoSupported;
  142. protected override async Task<MediaFile> Capture()
  143. {
  144. return await CrossMedia.Current.PickPhotoAsync(
  145. new PickMediaOptions()
  146. {
  147. CompressionQuality = Options.Compression,
  148. PhotoSize = Options.Size switch
  149. {
  150. MobileDocumentSize.Small => PhotoSize.Small,
  151. MobileDocumentSize.Large => PhotoSize.Large,
  152. MobileDocumentSize.Full => PhotoSize.Full,
  153. _ => PhotoSize.Medium
  154. },
  155. }
  156. );
  157. }
  158. }
  159. public class MobileDocumentVideoSource : MobileDocumentSource<MobileDocumentVideoOptions>
  160. {
  161. public MobileDocumentVideoSource(MobileDocumentVideoOptions options = null) : base(options)
  162. {
  163. }
  164. protected override bool IsEnabled => CrossMedia.Current.IsCameraAvailable && CrossMedia.Current.IsTakeVideoSupported;
  165. protected override async Task<MediaFile> Capture()
  166. {
  167. return await CrossMedia.Current.TakeVideoAsync(
  168. new StoreVideoOptions()
  169. {
  170. Name = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}.mp4",
  171. CompressionQuality = Options.Compression,
  172. PhotoSize = Options.Size switch
  173. {
  174. MobileDocumentSize.Small => PhotoSize.Small,
  175. MobileDocumentSize.Large => PhotoSize.Large,
  176. MobileDocumentSize.Full => PhotoSize.Full,
  177. _ => PhotoSize.Medium
  178. },
  179. Quality = Options.Quality switch
  180. {
  181. MobileDocumentQuality.Low => VideoQuality.Low,
  182. MobileDocumentQuality.High => VideoQuality.High,
  183. _ => VideoQuality.Medium
  184. }
  185. }
  186. );
  187. }
  188. }
  189. public class MobileDocumentVideoLibrarySource : MobileDocumentSource<MobileDocumentVideoLibraryOptions>
  190. {
  191. public MobileDocumentVideoLibrarySource(MobileDocumentVideoLibraryOptions options = null) : base(options)
  192. {
  193. }
  194. protected override bool IsEnabled => CrossMedia.Current.IsPickVideoSupported;
  195. protected override async Task<MediaFile> Capture()
  196. {
  197. return await CrossMedia.Current.PickVideoAsync();
  198. }
  199. }
  200. public static class MobileDocumentExtensions
  201. {
  202. }
  203. public class MobileDocument
  204. {
  205. public string FileName { get; set; }
  206. public byte[] Data { get; set; }
  207. public MobileDocument()
  208. {
  209. FileName = "";
  210. Data = new byte[] { };
  211. }
  212. public MobileDocument(string filename, byte[] data)
  213. {
  214. FileName = filename;
  215. Data = data;
  216. }
  217. public void ConvertToPDF()
  218. {
  219. using (var img = new MemoryStream(Data))
  220. {
  221. var image = new PdfBitmap(img);
  222. var pdfDoc = new PdfDocument();
  223. var section = pdfDoc.Sections.Add();
  224. section.PageSettings.Margins.All = 0;
  225. section.PageSettings.Width = image.Width;
  226. section.PageSettings.Height = image.Height;
  227. var page = section.Pages.Add();
  228. page.Graphics.DrawImage(image, 0, 0, page.Size.Width, page.Size.Height);
  229. using (var ms = new MemoryStream())
  230. {
  231. pdfDoc.Save(ms);
  232. Data = ms.GetBuffer();
  233. FileName = Path.ChangeExtension(FileName, "pdf");
  234. }
  235. }
  236. }
  237. public bool IsPDF() => FileName.ToUpper().EndsWith(".PDF");
  238. public static async Task<MobileDocument> From<T>(T source = null) where T : MobileDocumentSource
  239. {
  240. source ??= (T)Activator.CreateInstance(typeof(T), new object[] { null });
  241. return await source.From();
  242. }
  243. }
  244. }