EmbeddedImageCapture.cs 17 KB

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