123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- using System;
- using System.IO;
- using System.Threading.Tasks;
- using Plugin.Media;
- using Syncfusion.Drawing;
- using Syncfusion.Pdf;
- using Syncfusion.Pdf.Graphics;
- using Syncfusion.Pdf.Parsing;
- namespace InABox.Mobile
- {
-
- public abstract class MobileDocumentSource
- {
- public abstract Task<MobileDocument> GetDocument();
-
- }
- public class MobileDocumentCameraSource : MobileDocumentSource
- {
- public override async Task<MobileDocument> GetDocument()
- {
- await CrossMedia.Current.Initialize();
- if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
- {
- throw new Exception("No Camera Available");
- }
- String filename = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}.png";
- var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
- {
- Name = filename,
- CompressionQuality = 5,
- PhotoSize = Plugin.Media.Abstractions.PhotoSize.Large
- });
- if (file == null)
- return null;
- byte[] data;
- await using (var stream = file.GetStream())
- {
- BinaryReader br = new BinaryReader(stream);
- data = br.ReadBytes((int)stream.Length);
- }
- return new MobileDocument(filename, data);
- }
- }
- public class MobileDocumentLibrarySource : MobileDocumentSource
- {
- public override async Task<MobileDocument> GetDocument()
- {
- await CrossMedia.Current.Initialize();
-
- if (!CrossMedia.Current.IsPickPhotoSupported)
- throw new Exception("No Photo Library available");
- var file = await CrossMedia.Current.PickPhotoAsync(new Plugin.Media.Abstractions.PickMediaOptions()
- {
- CompressionQuality = 5,
- PhotoSize = Plugin.Media.Abstractions.PhotoSize.Large
- });
-
- if (file == null)
- return null;
-
- byte[] data;
- await using (var stream = file.GetStream())
- {
- BinaryReader br = new BinaryReader(stream);
- data = br.ReadBytes((int)stream.Length);
- }
- return new MobileDocument(file.OriginalFilename,data);
-
- }
- }
-
- public static class MobileDocumentExtensions
- {
- public static MobileDocument ToPDF(this MobileDocument document)
- {
- byte[] result;
- using (var img = new MemoryStream(document.Data))
- {
- var image = new PdfBitmap(img);
- var pdfDoc = new PdfDocument();
- var section = pdfDoc.Sections.Add();
- section.PageSettings.Margins.All = 0;
- section.PageSettings.Width = image.Width;
- section.PageSettings.Height = image.Height;
- var page = section.Pages.Add();
- page.Graphics.DrawImage(image, 0, 0, page.Size.Width, page.Size.Height);
- using (var ms = new MemoryStream())
- {
- pdfDoc.Save(ms);
- document.Data = ms.GetBuffer();
- document.FileName = Path.ChangeExtension(document.FileName, "pdf");
- }
- }
- return document;
- }
- }
-
- public class MobileDocument
- {
- public string FileName { get; set; }
- public byte[] Data { get; set; }
- public MobileDocument(string filename, byte[] data)
- {
- FileName = filename;
- Data = data;
- }
- public static async Task<MobileDocument> From<T>()
- where T : MobileDocumentSource, new()
- {
- return await new T().GetDocument();
- }
- }
- }
|