MobileDocument.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System;
  2. using System.IO;
  3. using System.Threading.Tasks;
  4. using Plugin.Media;
  5. using Syncfusion.Drawing;
  6. using Syncfusion.Pdf;
  7. using Syncfusion.Pdf.Graphics;
  8. using Syncfusion.Pdf.Parsing;
  9. namespace InABox.Mobile
  10. {
  11. public abstract class MobileDocumentSource
  12. {
  13. public abstract Task<MobileDocument> GetDocument();
  14. }
  15. public class MobileDocumentCameraSource : MobileDocumentSource
  16. {
  17. public override async Task<MobileDocument> GetDocument()
  18. {
  19. await CrossMedia.Current.Initialize();
  20. if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
  21. {
  22. throw new Exception("No Camera Available");
  23. }
  24. String filename = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}.png";
  25. var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
  26. {
  27. Name = filename,
  28. CompressionQuality = 5,
  29. PhotoSize = Plugin.Media.Abstractions.PhotoSize.Large
  30. });
  31. if (file == null)
  32. return null;
  33. byte[] data;
  34. await using (var stream = file.GetStream())
  35. {
  36. BinaryReader br = new BinaryReader(stream);
  37. data = br.ReadBytes((int)stream.Length);
  38. }
  39. return new MobileDocument(filename, data);
  40. }
  41. }
  42. public class MobileDocumentLibrarySource : MobileDocumentSource
  43. {
  44. public override async Task<MobileDocument> GetDocument()
  45. {
  46. await CrossMedia.Current.Initialize();
  47. if (!CrossMedia.Current.IsPickPhotoSupported)
  48. throw new Exception("No Photo Library available");
  49. var file = await CrossMedia.Current.PickPhotoAsync(new Plugin.Media.Abstractions.PickMediaOptions()
  50. {
  51. CompressionQuality = 5,
  52. PhotoSize = Plugin.Media.Abstractions.PhotoSize.Large
  53. });
  54. if (file == null)
  55. return null;
  56. byte[] data;
  57. await using (var stream = file.GetStream())
  58. {
  59. BinaryReader br = new BinaryReader(stream);
  60. data = br.ReadBytes((int)stream.Length);
  61. }
  62. return new MobileDocument(file.OriginalFilename,data);
  63. }
  64. }
  65. public static class MobileDocumentExtensions
  66. {
  67. public static MobileDocument ToPDF(this MobileDocument document)
  68. {
  69. byte[] result;
  70. using (var img = new MemoryStream(document.Data))
  71. {
  72. var image = new PdfBitmap(img);
  73. var pdfDoc = new PdfDocument();
  74. var section = pdfDoc.Sections.Add();
  75. section.PageSettings.Margins.All = 0;
  76. section.PageSettings.Width = image.Width;
  77. section.PageSettings.Height = image.Height;
  78. var page = section.Pages.Add();
  79. page.Graphics.DrawImage(image, 0, 0, page.Size.Width, page.Size.Height);
  80. using (var ms = new MemoryStream())
  81. {
  82. pdfDoc.Save(ms);
  83. document.Data = ms.GetBuffer();
  84. document.FileName = Path.ChangeExtension(document.FileName, "pdf");
  85. }
  86. }
  87. return document;
  88. }
  89. }
  90. public class MobileDocument
  91. {
  92. public string FileName { get; set; }
  93. public byte[] Data { get; set; }
  94. public MobileDocument(string filename, byte[] data)
  95. {
  96. FileName = filename;
  97. Data = data;
  98. }
  99. public static async Task<MobileDocument> From<T>()
  100. where T : MobileDocumentSource, new()
  101. {
  102. return await new T().GetDocument();
  103. }
  104. }
  105. }