MultiImageCapture.xaml.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Threading.Tasks;
  5. using InABox.Mobile;
  6. using Plugin.Media;
  7. using Xamarin.Forms;
  8. using Xamarin.Forms.Xaml;
  9. using XF.Material.Forms;
  10. using XF.Material.Forms.UI.Dialogs;
  11. namespace PRS.Mobile
  12. {
  13. [XamlCompilation(XamlCompilationOptions.Compile)]
  14. public partial class MultiImageCapture : ContentView
  15. {
  16. public List<Image> Images { get; set; }
  17. public MultiImageCapture(bool disablelibrary = false)
  18. {
  19. InitializeComponent();
  20. if (disablelibrary)
  21. ChooseImage.IsEnabled = false;
  22. Images = new List<Image>();
  23. }
  24. #region Buttons Pressed
  25. private async void TakePhoto_Clicked(object sender, MobileButtonClickEventArgs e)
  26. {
  27. Task.Run(OpenCamera);
  28. }
  29. private async void ChooseImage_Clicked(object sender, MobileButtonClickEventArgs e)
  30. {
  31. Task.Run(OpenLibrary);
  32. }
  33. private async void OpenCamera()
  34. {
  35. var document = await MobileDocument.From<MobileDocumentCameraSource>();
  36. if (document != null)
  37. DataToImage(document.Data);
  38. // try
  39. // {
  40. // await CrossMedia.Current.Initialize();
  41. // if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
  42. // {
  43. // await MaterialDialog.Instance.AlertAsync("Unable to Take Photo!");
  44. // return;
  45. // }
  46. //
  47. // String filename = String.Format("{0:yyyy-MM-dd HH:mm:ss.fff}.png", DateTime.Now);
  48. // var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
  49. // {
  50. // Name = filename,
  51. // CompressionQuality = 15,
  52. // PhotoSize = Plugin.Media.Abstractions.PhotoSize.Full,
  53. // SaveMetaData = false
  54. // });
  55. // ;
  56. // if (file != null)
  57. // AddPhoto(file);
  58. // }
  59. // catch (Exception e)
  60. // {
  61. // MobileLogging.Log(e,"MultiImage::OpenCamera");
  62. // }
  63. }
  64. private async void OpenLibrary()
  65. {
  66. var document = await MobileDocument.From<MobileDocumentLibrarySource>();
  67. if (document != null)
  68. DataToImage(document.Data);
  69. // try
  70. // {
  71. // await CrossMedia.Current.Initialize();
  72. // if (!CrossMedia.Current.IsPickPhotoSupported)
  73. // {
  74. // await MaterialDialog.Instance.AlertAsync("Unable to Open Library!");
  75. // return;
  76. // }
  77. // var file = await CrossMedia.Current.PickPhotoAsync(new Plugin.Media.Abstractions.PickMediaOptions()
  78. // {
  79. // CompressionQuality = 15,
  80. // PhotoSize = Plugin.Media.Abstractions.PhotoSize.Full,
  81. // });
  82. // if (file != null)
  83. // AddPhoto(file);
  84. // }
  85. // catch (Exception e)
  86. // {
  87. // MobileLogging.Log(e,"MultiImage::OpenLibrary");
  88. // return;
  89. // }
  90. }
  91. #endregion
  92. private async void AddPhoto(Plugin.Media.Abstractions.MediaFile file)
  93. {
  94. try
  95. {
  96. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Adding Photo"))
  97. {
  98. var memoryStream = new MemoryStream();
  99. if (Device.RuntimePlatform.Equals(Device.Android))
  100. file.GetStream().CopyTo(memoryStream);
  101. else if (Device.RuntimePlatform.Equals(Device.iOS))
  102. file.GetStreamWithImageRotatedForExternalStorage().CopyTo(memoryStream);
  103. var data = memoryStream.ToArray();
  104. DataToImage(data);
  105. }
  106. }
  107. catch
  108. { }
  109. }
  110. public void DataToImage(byte[] data)
  111. {
  112. try
  113. {
  114. ImageSource src = ImageSource.FromStream(() => new MemoryStream(data));
  115. Image img = new Image();
  116. img.HeightRequest = 150;
  117. img.WidthRequest = 150;
  118. img.Aspect = Aspect.AspectFit;
  119. img.VerticalOptions = LayoutOptions.FillAndExpand;
  120. img.HorizontalOptions = LayoutOptions.FillAndExpand;
  121. img.Source = src;
  122. img.GestureRecognizers.Add(new TapGestureRecognizer
  123. {
  124. Command = new Command(OnTap),
  125. CommandParameter = src,
  126. NumberOfTapsRequired = 1
  127. });
  128. if (img != null)
  129. {
  130. Images.Add(img);
  131. Device.BeginInvokeOnMainThread(RefreshView);
  132. }
  133. }
  134. catch
  135. {
  136. }
  137. }
  138. private void RefreshView()
  139. {
  140. Device.BeginInvokeOnMainThread(() =>
  141. {
  142. imagesStackLayout.Children.Clear();
  143. if (Images.Count > 0)
  144. addDeleteLbl.Text = "Tap on photo to view / delete";
  145. else
  146. addDeleteLbl.Text = "Add photo(s) below:";
  147. foreach (Image img in Images)
  148. {
  149. imagesStackLayout.Children.Add(img);
  150. }
  151. });
  152. }
  153. private void OnTap(object obj)
  154. {
  155. ImageViewerEditor imageViewEditor = new ImageViewerEditor(obj as ImageSource, true);
  156. imageViewEditor.OnDeleteSelected += () =>
  157. {
  158. Image img = Images.Find(x => x.Source.Equals(obj as ImageSource));
  159. Images.Remove(img);
  160. RefreshView();
  161. };
  162. imageViewEditor.OnSaveSelected += (byte[] array) =>
  163. {
  164. Image img = Images.Find(x => x.Source.Equals(obj as ImageSource));
  165. Images.Remove(img);
  166. DataToImage(array);
  167. };
  168. Navigation.PushAsync(imageViewEditor);
  169. }
  170. }
  171. }