| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485 |
- using System;
- using System.IO;
- using InABox.Core;
- using InABox.Mobile;
- using Xamarin.CommunityToolkit.UI.Views;
- using Xamarin.Essentials;
- using Xamarin.Forms;
- using XF.Material.Forms.UI.Dialogs;
- namespace PRS.Mobile
- {
- class EmbeddedImageCapture : Grid
- {
- public MobileButton CameraButton = new MobileButton();
- public MobileButton LibraryButton = new MobileButton();
- 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 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()
- {
- Padding = new Thickness(10);
- ColumnSpacing = 0;
- RowSpacing = 0;
- ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
- ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
- RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) });
- RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) });
- }
- private void AddButtons()
- {
- if (firstLoad)
- {
- CameraButton.Clicked += CameraButton_Clicked;
- CameraButton.Image = "camera";
- CameraButton.ImageSize = new Size(50, 50);
- CameraButton.Margin = 5;
- CameraButton.Padding = 2;
- SetColumn(CameraButton, 0);
- SetRow(CameraButton, 0);
- CameraButton.BackgroundColor = Color.FromHex("#15C7C1");
- CameraButton.HeightRequest = 70;
- CameraButton.WidthRequest = 70;
- CameraButton.HorizontalOptions = LayoutOptions.Center;
- CameraButton.VerticalOptions = LayoutOptions.Center;
- Children.Add(CameraButton);
-
- CameraLabel.Text = isVideo ? "Take Video" : "Camera";
- SetRow(CameraLabel, 1);
- SetColumn(CameraLabel, 0);
- CameraLabel.HorizontalOptions = LayoutOptions.Center;
- CameraLabel.HorizontalTextAlignment = TextAlignment.Center;
- CameraLabel.FontAttributes = FontAttributes.Bold;
- CameraLabel.FontSize = Device.GetNamedSize(NamedSize.Small, CameraLabel);
- CameraLabel.TextColor = Color.Black;
- Children.Add(CameraLabel);
-
- LibraryButton.Clicked += LibraryButton_Clicked;
- LibraryButton.Image = "gallery";
- LibraryButton.Margin = 5;
- LibraryButton.Padding = 2;
- SetRow(LibraryButton, 0);
- SetColumn(LibraryButton, 1);
- LibraryButton.BackgroundColor = Color.FromHex("#15C7C1");
- LibraryButton.HeightRequest = 70;
- LibraryButton.WidthRequest = 70;
- LibraryButton.HorizontalOptions = LayoutOptions.Center;
- LibraryButton.VerticalOptions = LayoutOptions.Center;
- LibraryButton.IsEnabled = !disableLibrary;
- Children.Add(LibraryButton);
-
- LibraryLabel.Text = "Library";
- SetRow(LibraryLabel, 1);
- SetColumn(LibraryLabel, 1);
- LibraryLabel.HorizontalOptions = LayoutOptions.Center;
- LibraryLabel.HorizontalTextAlignment = TextAlignment.Center;
- LibraryLabel.FontAttributes = FontAttributes.Bold;
- LibraryLabel.FontSize = Device.GetNamedSize(NamedSize.Small, LibraryLabel);
- LibraryLabel.TextColor = disableLibrary
- ? Color.Gray
- : Color.Black;
- Children.Add(LibraryLabel);
-
- }
-
- 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
- {
- var file = await MediaPicker.PickVideoAsync();
- if (file != null)
- AddVideo(file);
- }
- catch (FeatureNotSupportedException fnsEx)
- {
- await MaterialDialog.Instance.AlertAsync("Feature Not Supported!");
- }
- catch (PermissionException pEx)
- {
- await MaterialDialog.Instance.AlertAsync("Permission Not Granted!");
- }
- catch (Exception ex)
- {
- Console.WriteLine($"CapturePhotoAsync THREW: {ex.Message}");
- }
-
- // 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
- {
- var file = await MediaPicker.CaptureVideoAsync();
- if (file != null)
- AddVideo(file);
- }
- catch (FeatureNotSupportedException fnsEx)
- {
- await MaterialDialog.Instance.AlertAsync("Feature Not Supported!");
- }
- catch (PermissionException pEx)
- {
- await MaterialDialog.Instance.AlertAsync("Permission Not Granted!");
- }
- catch (Exception ex)
- {
- Console.WriteLine($"CapturePhotoAsync THREW: {ex.Message}");
- }
- // 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 = VideoQuality,
- // DesiredLength = new TimeSpan(0, 0, 20),
- // RotateImage = false
- // });
- //
- // if (file == null)
- // return;
- //
- // AddVideo(file);
- // }
- // }
- // catch
- // {
- // return;
- // }
- }
- private async void OpenLibrary()
- {
- try
- {
- var file = await MediaPicker.PickPhotoAsync();
- if (file != null)
- AddPhoto(file);
- }
- catch (FeatureNotSupportedException fnsEx)
- {
- await MaterialDialog.Instance.AlertAsync("Feature Not Supported!");
- }
- catch (PermissionException pEx)
- {
- await MaterialDialog.Instance.AlertAsync("Permission Not Granted!");
- }
- catch (Exception ex)
- {
- Console.WriteLine($"CapturePhotoAsync THREW: {ex.Message}");
- }
-
- // 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
- {
- var file = await MediaPicker.CapturePhotoAsync();
- if (file != null)
- AddPhoto(file);
- }
- catch (FeatureNotSupportedException fnsEx)
- {
- await MaterialDialog.Instance.AlertAsync("Feature Not Supported!");
- }
- catch (PermissionException pEx)
- {
- await MaterialDialog.Instance.AlertAsync("Permission Not Granted!");
- }
- catch (Exception ex)
- {
- Console.WriteLine($"CapturePhotoAsync THREW: {ex.Message}");
- }
-
- // 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(FileResult file)
- {
- try
- {
- using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Adding Video"))
- {
- var memoryStream = new MemoryStream();
- using (var stream = await file.OpenReadAsync())
- await stream.CopyToAsync(memoryStream);
- //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.FileName;
- 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" };
- 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();
- DoDelete();
- }
- private async void AddPhoto(FileResult file)
- {
- try
- {
- using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Adding Photo"))
- {
- var memoryStream = new MemoryStream();
-
- using (var stream = await file.OpenReadAsync())
- await stream.CopyToAsync(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,
- DataToImage,
- DoDelete
- );
- Navigation.PushAsync(imageViewEditor);
- }
- private void ImageViewEditor_OnSaveSelected(byte[] array)
- {
-
- }
- private void DoDelete()
- {
- Document = new Document();
- Image = new Image();
- Image.Source = null;
- Children.Clear();
- AddColumnsAndRows();
- AddButtons();
- }
- public void ClearItems()
- {
- Image = new Image();
- Image.Source = null;
- Children.Clear();
- }
- }
- }
|