SignaturePad.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using System;
  2. using System.Threading.Tasks;
  3. using Xamarin.Forms;
  4. using System.IO;
  5. using Syncfusion.XForms.SignaturePad;
  6. namespace PRS.Mobile
  7. {
  8. public delegate void NameAdded(string s);
  9. public delegate void NameRemoved(string s);
  10. class SignaturePad : Frame
  11. {
  12. private SfSignaturePad Pad = new SfSignaturePad();
  13. public string Name = "";
  14. public string Data = "";
  15. public bool StrokeAdded = false;
  16. public event NameAdded OnNameAdded;
  17. public event NameRemoved OnNameRemoved;
  18. private Entry entry = new Entry();
  19. Grid SignatureGrid = new Grid() { Margin = 0, Padding = 0 };
  20. EmbeddedImageCapture hiddenEmbeddedImageCapture = new EmbeddedImageCapture() { HeightRequest = 180, IsVisible = false, IsEnabled = false };
  21. public SignaturePad()
  22. {
  23. Padding = 4;
  24. Pad = new SfSignaturePad();
  25. Pad.BackgroundColor = Color.White;
  26. Pad.MinimumHeightRequest = 200;
  27. Pad.MinimumStrokeWidth = 0.2;
  28. Pad.MaximumStrokeWidth = 17;
  29. Pad.StrokeCompleted += Pad_StrokeCompleted;
  30. entry = new Entry
  31. {
  32. Placeholder = "Add Name",
  33. HorizontalOptions = LayoutOptions.FillAndExpand,
  34. Margin = new Thickness(1, 1, 0, 1),
  35. BackgroundColor = Color.White,
  36. VerticalOptions = LayoutOptions.Center
  37. };
  38. entry.TextChanged += (object sender, TextChangedEventArgs e) =>
  39. {
  40. entry.Text = entry.Text.ToUpper().Trim();
  41. Name = entry.Text;
  42. };
  43. Button lookupButton = new Button()
  44. {
  45. Text = "...",
  46. VerticalOptions = LayoutOptions.Center,
  47. FontAttributes = FontAttributes.Bold,
  48. CornerRadius = 5,
  49. Padding = 1,
  50. Margin = new Thickness(0, 1, 0, 1),
  51. };
  52. lookupButton.Clicked += (object sender, EventArgs e) =>
  53. {
  54. EmployeeSelectionPage page = new EmployeeSelectionPage(
  55. (employee) =>
  56. {
  57. entry.Text = employee.Name.ToUpper();
  58. OnNameAdded?.Invoke(entry.Text);
  59. });
  60. Navigation.PushAsync(page);
  61. };
  62. Button clearButton = new Button()
  63. {
  64. Text = "Clear",
  65. HorizontalOptions = LayoutOptions.FillAndExpand,
  66. VerticalOptions = LayoutOptions.End,
  67. Margin = new Thickness(0, 1, 1, 1),
  68. BackgroundColor = Color.FromHex("#15C7C1"),
  69. FontAttributes = FontAttributes.Bold,
  70. TextColor = Color.White,
  71. CornerRadius = 5,
  72. Padding = 1
  73. };
  74. clearButton.Clicked += (object sender, EventArgs e) =>
  75. {
  76. Pad.Clear();
  77. hiddenEmbeddedImageCapture.ClearItems();
  78. hiddenEmbeddedImageCapture.IsVisible = false;
  79. Pad.IsVisible = true;
  80. ClearName();
  81. Data = "";
  82. StrokeAdded = false;
  83. };
  84. Grid.SetColumn(entry, 0);
  85. Grid.SetColumn(lookupButton, 1);
  86. Grid.SetColumn(clearButton, 2);
  87. Grid buttonsGrid = new Grid { Margin = 0, Padding = 0, VerticalOptions = LayoutOptions.End };
  88. buttonsGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(2, GridUnitType.Star) });
  89. buttonsGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(35, GridUnitType.Absolute) });
  90. buttonsGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
  91. buttonsGrid.Children.Add(entry);
  92. buttonsGrid.Children.Add(lookupButton);
  93. buttonsGrid.Children.Add(clearButton);
  94. SignatureGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(200, GridUnitType.Absolute) });
  95. Grid.SetRow(Pad, 0);
  96. Grid.SetRow(hiddenEmbeddedImageCapture, 0);
  97. SignatureGrid.Children.Add(hiddenEmbeddedImageCapture);
  98. SignatureGrid.Children.Add(Pad);
  99. SignatureGrid.Children.Add(buttonsGrid);
  100. CornerRadius = 10;
  101. BorderColor = Color.FromHex("#15C7C1");
  102. Content = SignatureGrid;
  103. }
  104. private void Pad_StrokeCompleted(object sender, EventArgs e)
  105. {
  106. StrokeAdded = true;
  107. }
  108. public void ClearName()
  109. {
  110. OnNameRemoved?.Invoke(entry.Text);
  111. entry.Text = "";
  112. }
  113. public void SaveSignature()
  114. {
  115. if (Data == null || string.IsNullOrWhiteSpace(Data))
  116. {
  117. Pad.Save();
  118. if (Pad.ImageSource != null)
  119. {
  120. Data = ImageSourceToBase64(Pad.ImageSource);
  121. }
  122. }
  123. }
  124. public void PopulateSignature(byte[] data)
  125. {
  126. Pad.IsVisible = false;
  127. hiddenEmbeddedImageCapture.IsVisible = true;
  128. hiddenEmbeddedImageCapture.DataToImage(data);
  129. ImageSource src = ImageSource.FromStream(() => new MemoryStream(data));
  130. Data = ImageSourceToBase64(src);
  131. StrokeAdded = true;
  132. }
  133. public void PopulateName(string s)
  134. {
  135. entry.Text = s;
  136. }
  137. private string ImageSourceToBase64(ImageSource source)
  138. {
  139. StreamImageSource streamImageSource = (StreamImageSource)source;
  140. System.Threading.CancellationToken cancellationToken = System.Threading.CancellationToken.None;
  141. Task<Stream> task = streamImageSource.Stream(cancellationToken);
  142. Stream stream = task.Result;
  143. byte[] bytes = new byte[stream.Length];
  144. stream.Read(bytes, 0, bytes.Length);
  145. string s = Convert.ToBase64String(bytes, Base64FormattingOptions.InsertLineBreaks);
  146. return s;
  147. }
  148. }
  149. }