EmbeddedImageCapture.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. using System;
  2. using System.IO;
  3. using InABox.Core;
  4. using InABox.Mobile;
  5. using Xamarin.CommunityToolkit.UI.Views;
  6. using Xamarin.Essentials;
  7. using Xamarin.Forms;
  8. using XF.Material.Forms.UI.Dialogs;
  9. namespace PRS.Mobile
  10. {
  11. class EmbeddedImageCapture : Grid
  12. {
  13. public MobileButton CameraButton = new MobileButton();
  14. public MobileButton LibraryButton = new MobileButton();
  15. public Label CameraLabel = new Label();
  16. public Label LibraryLabel = new Label();
  17. public Document Document;
  18. public Image Image { get; set; }
  19. bool firstLoad;
  20. bool disableLibrary;
  21. bool isVideo;
  22. public byte[] bytes;
  23. public Plugin.Media.Abstractions.VideoQuality VideoQuality { get; set; }
  24. public TimeSpan VideoLength { get; set; }
  25. public EmbeddedImageCapture(bool disablelibrary = false, bool isvideo = false)
  26. {
  27. VerticalOptions = LayoutOptions.Center;
  28. firstLoad = true;
  29. Document = new Document();
  30. Image = new Image();
  31. disableLibrary = disablelibrary;
  32. isVideo = isvideo;
  33. AddColumnsAndRows();
  34. AddButtons();
  35. }
  36. private void AddColumnsAndRows()
  37. {
  38. Padding = new Thickness(10);
  39. ColumnSpacing = 0;
  40. RowSpacing = 0;
  41. ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
  42. ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
  43. RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) });
  44. RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) });
  45. }
  46. private void AddButtons()
  47. {
  48. if (firstLoad)
  49. {
  50. CameraButton.Clicked += CameraButton_Clicked;
  51. CameraButton.Image = "camera";
  52. CameraButton.ImageSize = new Size(50, 50);
  53. CameraButton.Margin = 5;
  54. CameraButton.Padding = 2;
  55. SetColumn(CameraButton, 0);
  56. SetRow(CameraButton, 0);
  57. CameraButton.BackgroundColor = Color.FromHex("#15C7C1");
  58. CameraButton.HeightRequest = 70;
  59. CameraButton.WidthRequest = 70;
  60. CameraButton.HorizontalOptions = LayoutOptions.Center;
  61. CameraButton.VerticalOptions = LayoutOptions.Center;
  62. Children.Add(CameraButton);
  63. CameraLabel.Text = isVideo ? "Take Video" : "Camera";
  64. SetRow(CameraLabel, 1);
  65. SetColumn(CameraLabel, 0);
  66. CameraLabel.HorizontalOptions = LayoutOptions.Center;
  67. CameraLabel.HorizontalTextAlignment = TextAlignment.Center;
  68. CameraLabel.FontAttributes = FontAttributes.Bold;
  69. CameraLabel.FontSize = Device.GetNamedSize(NamedSize.Small, CameraLabel);
  70. CameraLabel.TextColor = Color.Black;
  71. Children.Add(CameraLabel);
  72. LibraryButton.Clicked += LibraryButton_Clicked;
  73. LibraryButton.Image = "gallery";
  74. LibraryButton.Margin = 5;
  75. LibraryButton.Padding = 2;
  76. SetRow(LibraryButton, 0);
  77. SetColumn(LibraryButton, 1);
  78. LibraryButton.BackgroundColor = Color.FromHex("#15C7C1");
  79. LibraryButton.HeightRequest = 70;
  80. LibraryButton.WidthRequest = 70;
  81. LibraryButton.HorizontalOptions = LayoutOptions.Center;
  82. LibraryButton.VerticalOptions = LayoutOptions.Center;
  83. LibraryButton.IsEnabled = !disableLibrary;
  84. Children.Add(LibraryButton);
  85. LibraryLabel.Text = "Library";
  86. SetRow(LibraryLabel, 1);
  87. SetColumn(LibraryLabel, 1);
  88. LibraryLabel.HorizontalOptions = LayoutOptions.Center;
  89. LibraryLabel.HorizontalTextAlignment = TextAlignment.Center;
  90. LibraryLabel.FontAttributes = FontAttributes.Bold;
  91. LibraryLabel.FontSize = Device.GetNamedSize(NamedSize.Small, LibraryLabel);
  92. LibraryLabel.TextColor = disableLibrary
  93. ? Color.Gray
  94. : Color.Black;
  95. Children.Add(LibraryLabel);
  96. }
  97. firstLoad = false;
  98. }
  99. private void CameraButton_Clicked(object sender, EventArgs e)
  100. {
  101. if (isVideo)
  102. OpenVideoCamera();
  103. else
  104. OpenCamera();
  105. }
  106. private void LibraryButton_Clicked(object sender, EventArgs e)
  107. {
  108. if (isVideo)
  109. OpenVideoLibrary();
  110. else
  111. OpenLibrary();
  112. }
  113. private async void OpenVideoLibrary()
  114. {
  115. try
  116. {
  117. var file = await MediaPicker.PickVideoAsync();
  118. if (file != null)
  119. AddVideo(file);
  120. }
  121. catch (FeatureNotSupportedException fnsEx)
  122. {
  123. await MaterialDialog.Instance.AlertAsync("Feature Not Supported!");
  124. }
  125. catch (PermissionException pEx)
  126. {
  127. await MaterialDialog.Instance.AlertAsync("Permission Not Granted!");
  128. }
  129. catch (Exception ex)
  130. {
  131. Console.WriteLine($"CapturePhotoAsync THREW: {ex.Message}");
  132. }
  133. // try
  134. // {
  135. // await CrossMedia.Current.Initialize();
  136. //
  137. // if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakeVideoSupported)
  138. // {
  139. // return;
  140. // }
  141. //
  142. // var file = await CrossMedia.Current.PickVideoAsync();
  143. //
  144. // if (file == null)
  145. // return;
  146. //
  147. // AddVideo(file);
  148. // }
  149. // catch { }
  150. }
  151. private async void OpenVideoCamera()
  152. {
  153. try
  154. {
  155. var file = await MediaPicker.CaptureVideoAsync();
  156. if (file != null)
  157. AddVideo(file);
  158. }
  159. catch (FeatureNotSupportedException fnsEx)
  160. {
  161. await MaterialDialog.Instance.AlertAsync("Feature Not Supported!");
  162. }
  163. catch (PermissionException pEx)
  164. {
  165. await MaterialDialog.Instance.AlertAsync("Permission Not Granted!");
  166. }
  167. catch (Exception ex)
  168. {
  169. Console.WriteLine($"CapturePhotoAsync THREW: {ex.Message}");
  170. }
  171. // try
  172. // {
  173. // await CrossMedia.Current.Initialize();
  174. //
  175. // if (CrossMedia.Current.IsCameraAvailable && CrossMedia.Current.IsTakeVideoSupported)
  176. // {
  177. // var file = await CrossMedia.Current.TakeVideoAsync(new Plugin.Media.Abstractions.StoreVideoOptions()
  178. // {
  179. // CompressionQuality = 15,
  180. // PhotoSize = Plugin.Media.Abstractions.PhotoSize.MaxWidthHeight,
  181. // Quality = VideoQuality,
  182. // DesiredLength = new TimeSpan(0, 0, 20),
  183. // RotateImage = false
  184. // });
  185. //
  186. // if (file == null)
  187. // return;
  188. //
  189. // AddVideo(file);
  190. // }
  191. // }
  192. // catch
  193. // {
  194. // return;
  195. // }
  196. }
  197. private async void OpenLibrary()
  198. {
  199. try
  200. {
  201. var file = await MediaPicker.PickPhotoAsync();
  202. if (file != null)
  203. AddPhoto(file);
  204. }
  205. catch (FeatureNotSupportedException fnsEx)
  206. {
  207. await MaterialDialog.Instance.AlertAsync("Feature Not Supported!");
  208. }
  209. catch (PermissionException pEx)
  210. {
  211. await MaterialDialog.Instance.AlertAsync("Permission Not Granted!");
  212. }
  213. catch (Exception ex)
  214. {
  215. Console.WriteLine($"CapturePhotoAsync THREW: {ex.Message}");
  216. }
  217. // try
  218. // {
  219. // await CrossMedia.Current.Initialize();
  220. //
  221. // if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
  222. // {
  223. // return;
  224. // }
  225. //
  226. // var file = await CrossMedia.Current.PickPhotoAsync(new Plugin.Media.Abstractions.PickMediaOptions()
  227. // {
  228. // CompressionQuality = 15,
  229. // PhotoSize = Plugin.Media.Abstractions.PhotoSize.Full,
  230. // });
  231. //
  232. // if (file == null)
  233. // return;
  234. //
  235. // AddPhoto(file);
  236. // }
  237. // catch
  238. // {
  239. // return;
  240. // }
  241. }
  242. private async void OpenCamera()
  243. {
  244. try
  245. {
  246. var file = await MediaPicker.CapturePhotoAsync();
  247. if (file != null)
  248. AddPhoto(file);
  249. }
  250. catch (FeatureNotSupportedException fnsEx)
  251. {
  252. await MaterialDialog.Instance.AlertAsync("Feature Not Supported!");
  253. }
  254. catch (PermissionException pEx)
  255. {
  256. await MaterialDialog.Instance.AlertAsync("Permission Not Granted!");
  257. }
  258. catch (Exception ex)
  259. {
  260. Console.WriteLine($"CapturePhotoAsync THREW: {ex.Message}");
  261. }
  262. // try
  263. // {
  264. // await CrossMedia.Current.Initialize();
  265. //
  266. // if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
  267. // {
  268. // return;
  269. // }
  270. //
  271. // String filename = String.Format("{0:yyyy-MM-dd HH:mm:ss.fff}.png", DateTime.Now);
  272. //
  273. // var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
  274. // {
  275. // Name = filename,
  276. // CompressionQuality = 15,
  277. // PhotoSize = Plugin.Media.Abstractions.PhotoSize.Full,
  278. // SaveMetaData = false
  279. // });
  280. //
  281. // if (file == null)
  282. // return;
  283. //
  284. // AddPhoto(file);
  285. // }
  286. // catch (Exception e)
  287. // {
  288. //
  289. // }
  290. }
  291. private async void AddVideo(FileResult file)
  292. {
  293. try
  294. {
  295. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Adding Video"))
  296. {
  297. var memoryStream = new MemoryStream();
  298. using (var stream = await file.OpenReadAsync())
  299. await stream.CopyToAsync(memoryStream);
  300. //file.GetStreamWithImageRotatedForExternalStorage().CopyTo(memoryStream);
  301. var data = memoryStream.ToArray();
  302. bytes = data;
  303. ImageSource src = ImageSource.FromStream(() => new MemoryStream(data));
  304. MediaElement element = new MediaElement();
  305. element.HorizontalOptions = LayoutOptions.Center;
  306. element.VerticalOptions = LayoutOptions.Center;
  307. element.ShowsPlaybackControls = true;
  308. element.Aspect = Aspect.Fill;
  309. element.HeightRequest = 800;
  310. element.WidthRequest = 400;
  311. element.AutoPlay = false;
  312. element.Margin = 5;
  313. element.Source = file.FileName;
  314. Device.BeginInvokeOnMainThread(() =>
  315. {
  316. try
  317. {
  318. Children.Clear();
  319. RowDefinitions.Clear();
  320. ColumnDefinitions.Clear();
  321. RowDefinitions.Add(new RowDefinition() { Height = new GridLength(35, GridUnitType.Absolute) });
  322. RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) });
  323. ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Auto) });
  324. Image img = new Image { Source = "closee" };
  325. img.GestureRecognizers.Add(new TapGestureRecognizer
  326. {
  327. Command = new Command(OnVideoTap),
  328. CommandParameter = data,
  329. NumberOfTapsRequired = 1
  330. });
  331. img.HorizontalOptions = LayoutOptions.Center;
  332. img.VerticalOptions = LayoutOptions.Center;
  333. img.HeightRequest = 35;
  334. img.WidthRequest = 35;
  335. img.Margin = new Thickness(5, 5, 5, 0);
  336. SetRow(img, 0);
  337. SetColumn(img, 0);
  338. SetRow(element, 1);
  339. SetColumn(element, 0);
  340. Padding = new Thickness(0);
  341. Children.Add(img);
  342. Children.Add(element);
  343. HorizontalOptions = LayoutOptions.CenterAndExpand;
  344. ForceLayout();
  345. }
  346. catch { }
  347. });
  348. }
  349. }
  350. catch
  351. { }
  352. }
  353. private void OnVideoTap(object obj)
  354. {
  355. bytes = new byte[] { };
  356. RowDefinitions.Clear();
  357. ColumnDefinitions.Clear();
  358. Viewer_OnDeleteSelected();
  359. }
  360. private async void AddPhoto(FileResult file)
  361. {
  362. try
  363. {
  364. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Adding Photo"))
  365. {
  366. var memoryStream = new MemoryStream();
  367. using (var stream = await file.OpenReadAsync())
  368. await stream.CopyToAsync(memoryStream);
  369. // if (Device.RuntimePlatform.Equals(Device.Android))
  370. // file.GetStream().CopyTo(memoryStream);
  371. // else if (Device.RuntimePlatform.Equals(Device.iOS))
  372. // file.GetStreamWithImageRotatedForExternalStorage().CopyTo(memoryStream);
  373. var data = memoryStream.ToArray();
  374. DataToImage(data);
  375. }
  376. }
  377. catch
  378. { }
  379. }
  380. public void DataToImage(byte[] data)
  381. {
  382. try
  383. {
  384. ImageSource src = ImageSource.FromStream(() => new MemoryStream(data));
  385. Image = new Image();
  386. Image.HeightRequest = 200;
  387. Image.WidthRequest = 200;
  388. Image.Aspect = Aspect.AspectFit;
  389. Image.VerticalOptions = LayoutOptions.FillAndExpand;
  390. Image.HorizontalOptions = LayoutOptions.FillAndExpand;
  391. Image.Source = src;
  392. Image.GestureRecognizers.Add(new TapGestureRecognizer
  393. {
  394. Command = new Command(OnTap),
  395. CommandParameter = src,
  396. NumberOfTapsRequired = 1
  397. });
  398. if (Image != null)
  399. {
  400. Device.BeginInvokeOnMainThread(() =>
  401. {
  402. Children.Clear();
  403. RowDefinitions.Clear();
  404. ColumnDefinitions.Clear();
  405. Children.Add(Image);
  406. });
  407. }
  408. }
  409. catch
  410. { }
  411. }
  412. private void OnTap(object obj)
  413. {
  414. ImageViewerEditor imageViewEditor = new ImageViewerEditor(obj as ImageSource, true);
  415. imageViewEditor.OnDeleteSelected += Viewer_OnDeleteSelected;
  416. imageViewEditor.OnSaveSelected += ImageViewEditor_OnSaveSelected;
  417. Navigation.PushAsync(imageViewEditor);
  418. }
  419. private void ImageViewEditor_OnSaveSelected(byte[] array)
  420. {
  421. DataToImage(array);
  422. }
  423. private void Viewer_OnDeleteSelected()
  424. {
  425. Document = new Document();
  426. Image = new Image();
  427. Image.Source = null;
  428. Children.Clear();
  429. AddColumnsAndRows();
  430. AddButtons();
  431. }
  432. public void ClearItems()
  433. {
  434. Image = new Image();
  435. Image.Source = null;
  436. Children.Clear();
  437. }
  438. }
  439. }