MultiImageCapture.xaml.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Threading.Tasks;
  4. using InABox.Mobile;
  5. using Xamarin.Forms;
  6. using Xamarin.Forms.Xaml;
  7. using XF.Material.Forms.UI.Dialogs;
  8. namespace PRS.Mobile
  9. {
  10. [XamlCompilation(XamlCompilationOptions.Compile)]
  11. public partial class MultiImageCapture : ContentView
  12. {
  13. public List<Image> Images { get; set; }
  14. public MultiImageCapture(bool disablelibrary = false)
  15. {
  16. InitializeComponent();
  17. if (disablelibrary)
  18. ChooseImage.IsEnabled = false;
  19. Images = new List<Image>();
  20. }
  21. #region Buttons Pressed
  22. private async void TakePhoto_Clicked(object sender, MobileButtonClickEventArgs e)
  23. {
  24. Task.Run(OpenCamera);
  25. }
  26. private async void ChooseImage_Clicked(object sender, MobileButtonClickEventArgs e)
  27. {
  28. Task.Run(OpenLibrary);
  29. }
  30. private async void OpenCamera()
  31. {
  32. var document = await MobileDocument.From<MobileDocumentCameraSource>(PhotoUtils.CreateCameraOptions());
  33. if (document != null)
  34. DataToImage(document.Data);
  35. }
  36. private async void OpenLibrary()
  37. {
  38. var document = await MobileDocument.From<MobileDocumentPhotoLibrarySource>(PhotoUtils.CreatePhotoLibraryOptions());
  39. if (document != null)
  40. DataToImage(document.Data);
  41. }
  42. #endregion
  43. // private async void AddPhoto(Plugin.Media.Abstractions.MediaFile file)
  44. // {
  45. // try
  46. // {
  47. // using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Adding Photo"))
  48. // {
  49. // var memoryStream = new MemoryStream();
  50. //
  51. // if (Device.RuntimePlatform.Equals(Device.Android))
  52. // file.GetStream().CopyTo(memoryStream);
  53. // else if (Device.RuntimePlatform.Equals(Device.iOS))
  54. // file.GetStreamWithImageRotatedForExternalStorage().CopyTo(memoryStream);
  55. //
  56. // var data = memoryStream.ToArray();
  57. //
  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(
  110. obj as ImageSource,
  111. (data) =>
  112. {
  113. Image img = Images.Find(x => x.Source.Equals(obj as ImageSource));
  114. Images.Remove(img);
  115. DataToImage(data);
  116. },
  117. () =>
  118. {
  119. Image img = Images.Find(x => x.Source.Equals(obj as ImageSource));
  120. Images.Remove(img);
  121. RefreshView();
  122. }
  123. );
  124. Navigation.PushAsync(imageViewEditor);
  125. }
  126. }
  127. }