using System; using System.IO; using InABox.Core; using Plugin.Media; using Xamarin.CommunityToolkit.Core; using Xamarin.CommunityToolkit.UI.Views; using Xamarin.Forms; using XF.Material.Forms.UI.Dialogs; namespace comal.timesheets { class EmbeddedImageCapture : Grid { public Button CameraButton = new Button(); public Button LibraryButton = new Button(); public Label CameraLabel = new Label(); public Label LibraryLabel = new Label(); public Document Document; public Image Image { get; set; } bool firstLoad; bool disableLibrary; bool isVideo; public byte[] bytes; public Plugin.Media.Abstractions.VideoQuality VideoQuality {get; set;} public TimeSpan VideoLength { get; set; } public EmbeddedImageCapture(bool disablelibrary = false, bool isvideo = false) { VerticalOptions = LayoutOptions.Center; firstLoad = true; Document = new Document(); Image = new Image(); disableLibrary = disablelibrary; isVideo = isvideo; AddColumnsAndRows(); AddButtons(); } private void AddColumnsAndRows() { new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }; new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }; RowDefinition row = new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) }; RowDefinition row1 = new RowDefinition() { Height = new GridLength(5, GridUnitType.Star) }; RowDefinition row2 = new RowDefinition() { Height = new GridLength(2, GridUnitType.Star) }; RowDefinitions.Add(row); RowDefinitions.Add(row1); RowDefinitions.Add(row2); if (isVideo) RowDefinitions.Add(new RowDefinition() { Height = new GridLength(50, GridUnitType.Absolute) }); } private void AddButtons() { if (firstLoad) { CameraButton.Clicked += CameraButton_Clicked; CameraButton.ImageSource = "cameraicon.png"; CameraButton.Margin = 5; CameraButton.Padding = 2; SetColumn(CameraButton, 0); SetRow(CameraButton, 1); CameraButton.BackgroundColor = Color.FromHex("#15C7C1"); CameraButton.HeightRequest = 90; CameraButton.WidthRequest = 90; CameraButton.CornerRadius = 15; CameraButton.HorizontalOptions = LayoutOptions.Center; CameraButton.VerticalOptions = LayoutOptions.Center; //CameraButton.BorderWidth = 1; //CameraButton.BorderColor = Color.Orange; LibraryButton.Clicked += LibraryButton_Clicked; LibraryButton.ImageSource = "photolibraryicon.png"; if (Device.RuntimePlatform.Equals(Device.iOS)) { LibraryButton.ImageSource = "photo.png"; } LibraryButton.Margin = 5; LibraryButton.Padding = 2; SetRow(LibraryButton, 1); SetColumn(LibraryButton, 1); LibraryButton.BackgroundColor = Color.FromHex("#15C7C1"); LibraryButton.HeightRequest = 90; LibraryButton.WidthRequest = 90; LibraryButton.CornerRadius = 15; LibraryButton.HorizontalOptions = LayoutOptions.Center; LibraryButton.VerticalOptions = LayoutOptions.Center; if (disableLibrary) LibraryButton.IsEnabled = false; //LibraryButton.BorderWidth = 1; //LibraryButton.BorderColor = Color.Orange; CameraLabel.Text = "Camera"; SetRow(CameraLabel, 2); SetColumn(CameraLabel, 0); CameraLabel.HorizontalOptions = LayoutOptions.Center; CameraLabel.HorizontalTextAlignment = TextAlignment.Center; CameraLabel.FontAttributes = FontAttributes.Bold; CameraLabel.FontSize = Device.GetNamedSize(NamedSize.Medium, CameraLabel); LibraryLabel.Text = "Library"; SetRow(LibraryLabel, 2); SetColumn(LibraryLabel, 1); LibraryLabel.HorizontalOptions = LayoutOptions.Center; LibraryLabel.HorizontalTextAlignment = TextAlignment.Center; LibraryLabel.FontAttributes = FontAttributes.Bold; LibraryLabel.FontSize = Device.GetNamedSize(NamedSize.Medium, LibraryLabel); } Children.Add(CameraButton); Children.Add(LibraryButton); Children.Add(CameraLabel); Children.Add(LibraryLabel); if (isVideo) { var lbl = new Label { Text = "Take Video", FontAttributes = FontAttributes.Bold, FontSize = 24 }; lbl.HorizontalOptions = LayoutOptions.Center; lbl.VerticalOptions = LayoutOptions.Center; SetRow(lbl, 3); SetColumn(lbl, 0); SetColumnSpan(lbl, 2); Children.Add(lbl); } firstLoad = false; } private void CameraButton_Clicked(object sender, EventArgs e) { if (isVideo) OpenVideoCamera(); else OpenCamera(); } private void LibraryButton_Clicked(object sender, EventArgs e) { if (isVideo) OpenVideoLibrary(); else OpenLibrary(); } private async void OpenVideoLibrary() { try { await CrossMedia.Current.Initialize(); if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakeVideoSupported) { return; } var file = await CrossMedia.Current.PickVideoAsync(); if (file == null) return; AddVideo(file); } catch { } } private async void OpenVideoCamera() { try { await CrossMedia.Current.Initialize(); if (CrossMedia.Current.IsCameraAvailable && CrossMedia.Current.IsTakeVideoSupported) { var file = await CrossMedia.Current.TakeVideoAsync(new Plugin.Media.Abstractions.StoreVideoOptions() { CompressionQuality = 15, PhotoSize = Plugin.Media.Abstractions.PhotoSize.MaxWidthHeight, Quality = Plugin.Media.Abstractions.VideoQuality.Low, DesiredLength = new TimeSpan(0, 0, 20), RotateImage = false }); if (file == null) return; AddVideo(file); } } catch { return; } } private async void OpenLibrary() { try { await CrossMedia.Current.Initialize(); if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported) { return; } var file = await CrossMedia.Current.PickPhotoAsync(new Plugin.Media.Abstractions.PickMediaOptions() { CompressionQuality = 15, PhotoSize = Plugin.Media.Abstractions.PhotoSize.Full, }); if (file == null) return; AddPhoto(file); } catch { return; } } private async void OpenCamera() { try { await CrossMedia.Current.Initialize(); if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported) { 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) return; AddPhoto(file); } catch(Exception e) { } } private async void AddVideo(Plugin.Media.Abstractions.MediaFile file) { try { using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Adding Video")) { 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(); bytes = data; ImageSource src = ImageSource.FromStream(() => new MemoryStream(data)); MediaElement element = new MediaElement(); element.HorizontalOptions = LayoutOptions.Center; element.VerticalOptions = LayoutOptions.Center; element.ShowsPlaybackControls = true; element.Aspect = Aspect.Fill; element.HeightRequest = 800; element.WidthRequest = 400; element.AutoPlay = false; element.Margin = 5; element.Source = file.Path; Device.BeginInvokeOnMainThread(() => { try { Children.Clear(); RowDefinitions.Clear(); ColumnDefinitions.Clear(); RowDefinitions.Add(new RowDefinition() { Height = new GridLength(35, GridUnitType.Absolute) }); RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) }); ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Auto) }); Image img = new Image { Source = "closee.png" }; img.GestureRecognizers.Add(new TapGestureRecognizer { Command = new Command(OnVideoTap), CommandParameter = data, NumberOfTapsRequired = 1 }); img.HorizontalOptions = LayoutOptions.Center; img.VerticalOptions = LayoutOptions.Center; img.HeightRequest = 35; img.WidthRequest = 35; img.Margin = new Thickness(5, 5, 5, 0); SetRow(img, 0); SetColumn(img, 0); SetRow(element, 1); SetColumn(element, 0); Padding = new Thickness(0); Children.Add(img); Children.Add(element); HorizontalOptions = LayoutOptions.CenterAndExpand; ForceLayout(); } catch { } }); } } catch { } } private void OnVideoTap(object obj) { bytes = new byte[] { }; RowDefinitions.Clear(); ColumnDefinitions.Clear(); Viewer_OnDeleteSelected(); } 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 = new Image(); Image.HeightRequest = 200; Image.WidthRequest = 200; Image.Aspect = Aspect.AspectFit; Image.VerticalOptions = LayoutOptions.FillAndExpand; Image.HorizontalOptions = LayoutOptions.FillAndExpand; Image.Source = src; Image.GestureRecognizers.Add(new TapGestureRecognizer { Command = new Command(OnTap), CommandParameter = src, NumberOfTapsRequired = 1 }); if (Image != null) { Device.BeginInvokeOnMainThread(() => { Children.Clear(); RowDefinitions.Clear(); ColumnDefinitions.Clear(); Children.Add(Image); }); } } catch { } } private void OnTap(object obj) { ImageViewerEditor imageViewEditor = new ImageViewerEditor(obj as ImageSource, true); imageViewEditor.OnDeleteSelected += Viewer_OnDeleteSelected; imageViewEditor.OnSaveSelected += ImageViewEditor_OnSaveSelected; Navigation.PushAsync(imageViewEditor); } private void ImageViewEditor_OnSaveSelected(byte[] array) { DataToImage(array); } private void Viewer_OnDeleteSelected() { Document = new Document(); Image = new Image(); Image.Source = null; Children.Clear(); AddColumnsAndRows(); AddButtons(); } public void ClearItems() { Image = new Image(); Image.Source = null; Children.Clear(); } } }