MultiImageCapture.xaml.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. }
  39. private async void OpenLibrary()
  40. {
  41. var document = await MobileDocument.From<MobileDocumentPhotoLibrarySource>();
  42. if (document != null)
  43. DataToImage(document.Data);
  44. }
  45. #endregion
  46. private async void AddPhoto(Plugin.Media.Abstractions.MediaFile file)
  47. {
  48. try
  49. {
  50. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Adding Photo"))
  51. {
  52. var memoryStream = new MemoryStream();
  53. if (Device.RuntimePlatform.Equals(Device.Android))
  54. file.GetStream().CopyTo(memoryStream);
  55. else if (Device.RuntimePlatform.Equals(Device.iOS))
  56. file.GetStreamWithImageRotatedForExternalStorage().CopyTo(memoryStream);
  57. var data = memoryStream.ToArray();
  58. DataToImage(data);
  59. }
  60. }
  61. catch
  62. { }
  63. }
  64. public void DataToImage(byte[] data)
  65. {
  66. try
  67. {
  68. ImageSource src = ImageSource.FromStream(() => new MemoryStream(data));
  69. Image img = new Image();
  70. img.HeightRequest = 150;
  71. img.WidthRequest = 150;
  72. img.Aspect = Aspect.AspectFit;
  73. img.VerticalOptions = LayoutOptions.FillAndExpand;
  74. img.HorizontalOptions = LayoutOptions.FillAndExpand;
  75. img.Source = src;
  76. img.GestureRecognizers.Add(new TapGestureRecognizer
  77. {
  78. Command = new Command(OnTap),
  79. CommandParameter = src,
  80. NumberOfTapsRequired = 1
  81. });
  82. if (img != null)
  83. {
  84. Images.Add(img);
  85. Device.BeginInvokeOnMainThread(RefreshView);
  86. }
  87. }
  88. catch
  89. {
  90. }
  91. }
  92. private void RefreshView()
  93. {
  94. Device.BeginInvokeOnMainThread(() =>
  95. {
  96. imagesStackLayout.Children.Clear();
  97. if (Images.Count > 0)
  98. addDeleteLbl.Text = "Tap on photo to view / delete";
  99. else
  100. addDeleteLbl.Text = "Add photo(s) below:";
  101. foreach (Image img in Images)
  102. {
  103. imagesStackLayout.Children.Add(img);
  104. }
  105. });
  106. }
  107. private void OnTap(object obj)
  108. {
  109. ImageViewerEditor imageViewEditor = new ImageViewerEditor(obj as ImageSource, true);
  110. imageViewEditor.OnDeleteSelected += () =>
  111. {
  112. Image img = Images.Find(x => x.Source.Equals(obj as ImageSource));
  113. Images.Remove(img);
  114. RefreshView();
  115. };
  116. imageViewEditor.OnSaveSelected += (byte[] array) =>
  117. {
  118. Image img = Images.Find(x => x.Source.Equals(obj as ImageSource));
  119. Images.Remove(img);
  120. DataToImage(array);
  121. };
  122. Navigation.PushAsync(imageViewEditor);
  123. }
  124. }
  125. }