123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421 |
- 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();
- }
- }
- }
|