using System; using System.Collections.Generic; using System.IO; using System.Threading.Tasks; using InABox.Mobile; using Plugin.Media; using Xamarin.Forms; using Xamarin.Forms.Xaml; using XF.Material.Forms; using XF.Material.Forms.UI.Dialogs; namespace PRS.Mobile { [XamlCompilation(XamlCompilationOptions.Compile)] public partial class MultiImageCapture : ContentView { public List Images { get; set; } public MultiImageCapture(bool disablelibrary = false) { InitializeComponent(); if (disablelibrary) ChooseImage.IsEnabled = false; Images = new List(); } #region Buttons Pressed private async void TakePhoto_Clicked(object sender, MobileButtonClickEventArgs e) { Task.Run(OpenCamera); } private async void ChooseImage_Clicked(object sender, MobileButtonClickEventArgs e) { Task.Run(OpenLibrary); } private async void OpenCamera() { var document = await MobileDocument.From(); if (document != null) DataToImage(document.Data); // try // { // await CrossMedia.Current.Initialize(); // if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported) // { // await MaterialDialog.Instance.AlertAsync("Unable to Take Photo!"); // return; // } // // String filename = String.Format("{0:yyyy-MM-dd HH:mm:ss.fff}.png", DateTime.Now); // var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions // { // Name = filename, // CompressionQuality = 15, // PhotoSize = Plugin.Media.Abstractions.PhotoSize.Full, // SaveMetaData = false // }); // ; // if (file != null) // AddPhoto(file); // } // catch (Exception e) // { // MobileLogging.Log(e,"MultiImage::OpenCamera"); // } } private async void OpenLibrary() { var document = await MobileDocument.From(); if (document != null) DataToImage(document.Data); // try // { // await CrossMedia.Current.Initialize(); // if (!CrossMedia.Current.IsPickPhotoSupported) // { // await MaterialDialog.Instance.AlertAsync("Unable to Open Library!"); // return; // } // var file = await CrossMedia.Current.PickPhotoAsync(new Plugin.Media.Abstractions.PickMediaOptions() // { // CompressionQuality = 15, // PhotoSize = Plugin.Media.Abstractions.PhotoSize.Full, // }); // if (file != null) // AddPhoto(file); // } // catch (Exception e) // { // MobileLogging.Log(e,"MultiImage::OpenLibrary"); // return; // } } #endregion private async void AddPhoto(Plugin.Media.Abstractions.MediaFile file) { try { using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Adding Photo")) { var memoryStream = new MemoryStream(); if (Device.RuntimePlatform.Equals(Device.Android)) file.GetStream().CopyTo(memoryStream); else if (Device.RuntimePlatform.Equals(Device.iOS)) file.GetStreamWithImageRotatedForExternalStorage().CopyTo(memoryStream); var data = memoryStream.ToArray(); DataToImage(data); } } catch { } } public void DataToImage(byte[] data) { try { ImageSource src = ImageSource.FromStream(() => new MemoryStream(data)); Image img = new Image(); img.HeightRequest = 150; img.WidthRequest = 150; img.Aspect = Aspect.AspectFit; img.VerticalOptions = LayoutOptions.FillAndExpand; img.HorizontalOptions = LayoutOptions.FillAndExpand; img.Source = src; img.GestureRecognizers.Add(new TapGestureRecognizer { Command = new Command(OnTap), CommandParameter = src, NumberOfTapsRequired = 1 }); if (img != null) { Images.Add(img); Device.BeginInvokeOnMainThread(RefreshView); } } catch { } } private void RefreshView() { Device.BeginInvokeOnMainThread(() => { imagesStackLayout.Children.Clear(); if (Images.Count > 0) addDeleteLbl.Text = "Tap on photo to view / delete"; else addDeleteLbl.Text = "Add photo(s) below:"; foreach (Image img in Images) { imagesStackLayout.Children.Add(img); } }); } private void OnTap(object obj) { ImageViewerEditor imageViewEditor = new ImageViewerEditor(obj as ImageSource, true); imageViewEditor.OnDeleteSelected += () => { Image img = Images.Find(x => x.Source.Equals(obj as ImageSource)); Images.Remove(img); RefreshView(); }; imageViewEditor.OnSaveSelected += (byte[] array) => { Image img = Images.Find(x => x.Source.Equals(obj as ImageSource)); Images.Remove(img); DataToImage(array); }; Navigation.PushAsync(imageViewEditor); } } }