CameraView.axaml.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. using Avalonia;
  2. using Avalonia.Controls;
  3. using Avalonia.Markup.Xaml;
  4. using Avalonia.Platform;
  5. using Avalonia.Threading;
  6. using InABox.Core;
  7. using Microsoft.Maui.Graphics.Platform;
  8. using System.Timers;
  9. using System.Windows.Input;
  10. using Timer = System.Timers.Timer;
  11. namespace InABox.Avalonia.Platform.Barcodes;
  12. public interface ICameraViewControl : ILoggable
  13. {
  14. IPlatformHandle CreateControl(CameraView view, IPlatformHandle parent);
  15. }
  16. public class DefaultCameraViewControl : ICameraViewControl
  17. {
  18. public Logger? Logger { get; set; }
  19. public IPlatformHandle CreateControl(CameraView view, IPlatformHandle parent)
  20. {
  21. return null;
  22. }
  23. }
  24. public partial class CameraView : UserControl
  25. {
  26. public static readonly StyledProperty<ICommand> OnDetectionFinishedCommandProperty =
  27. AvaloniaProperty.Register<CameraView, ICommand>(nameof(OnDetectionFinishedCommand));
  28. public ICommand OnDetectionFinishedCommand
  29. {
  30. get => GetValue(OnDetectionFinishedCommandProperty);
  31. set => SetValue(OnDetectionFinishedCommandProperty, value);
  32. }
  33. public static readonly StyledProperty<ICommand> OnImageCapturedCommandProperty =
  34. AvaloniaProperty.Register<CameraView, ICommand>(nameof(OnImageCapturedCommand));
  35. public ICommand OnImageCapturedCommand
  36. {
  37. get => GetValue(OnImageCapturedCommandProperty);
  38. set => SetValue(OnImageCapturedCommandProperty, value);
  39. }
  40. public static readonly StyledProperty<bool> VibrationOnDetectedProperty =
  41. AvaloniaProperty.Register<CameraView, bool>(nameof(VibrationOnDetected), false);
  42. public bool VibrationOnDetected
  43. {
  44. get => GetValue(VibrationOnDetectedProperty);
  45. set => SetValue(VibrationOnDetectedProperty, value);
  46. }
  47. public static readonly StyledProperty<bool> CameraEnabledProperty =
  48. AvaloniaProperty.Register<CameraView, bool>(nameof(CameraEnabled), false);
  49. public bool CameraEnabled
  50. {
  51. get => GetValue(CameraEnabledProperty);
  52. set => SetValue(CameraEnabledProperty, value);
  53. }
  54. public static readonly StyledProperty<bool> PauseScanningProperty =
  55. AvaloniaProperty.Register<CameraView, bool>(nameof(PauseScanning), false);
  56. public bool PauseScanning
  57. {
  58. get => GetValue(PauseScanningProperty);
  59. set => SetValue(PauseScanningProperty, value);
  60. }
  61. public static readonly StyledProperty<bool> ForceInvertedProperty =
  62. AvaloniaProperty.Register<CameraView, bool>(nameof(ForceInverted), false);
  63. public bool ForceInverted
  64. {
  65. get => GetValue(ForceInvertedProperty);
  66. set => SetValue(ForceInvertedProperty, value);
  67. }
  68. public static readonly StyledProperty<bool> TorchOnProperty =
  69. AvaloniaProperty.Register<CameraView, bool>(nameof(TorchOn), false);
  70. public bool TorchOn
  71. {
  72. get => GetValue(TorchOnProperty);
  73. set => SetValue(TorchOnProperty, value);
  74. }
  75. public static readonly StyledProperty<bool> TapToFocusEnabledProperty =
  76. AvaloniaProperty.Register<CameraView, bool>(nameof(TapToFocusEnabled), false);
  77. public bool TapToFocusEnabled
  78. {
  79. get => GetValue(TapToFocusEnabledProperty);
  80. set => SetValue(TapToFocusEnabledProperty, value);
  81. }
  82. public static readonly StyledProperty<CameraFacing> CameraFacingProperty =
  83. AvaloniaProperty.Register<CameraView, CameraFacing>(nameof(CameraFacing), CameraFacing.Back);
  84. public CameraFacing CameraFacing
  85. {
  86. get => GetValue(CameraFacingProperty);
  87. set => SetValue(CameraFacingProperty, value);
  88. }
  89. public static readonly StyledProperty<CaptureQuality> CaptureQualityProperty =
  90. AvaloniaProperty.Register<CameraView, CaptureQuality>(nameof(CaptureQuality), CaptureQuality.Medium);
  91. public CaptureQuality CaptureQuality
  92. {
  93. get => GetValue(CaptureQualityProperty);
  94. set => SetValue(CaptureQualityProperty, value);
  95. }
  96. public static readonly StyledProperty<BarcodeFormats> BarcodeSymbologiesProperty =
  97. AvaloniaProperty.Register<CameraView, BarcodeFormats>(nameof(BarcodeSymbologies), BarcodeFormats.All);
  98. public BarcodeFormats BarcodeSymbologies
  99. {
  100. get => GetValue(BarcodeSymbologiesProperty);
  101. set => SetValue(BarcodeSymbologiesProperty, value);
  102. }
  103. public static readonly StyledProperty<bool> AimModeProperty =
  104. AvaloniaProperty.Register<CameraView, bool>(nameof(AimMode), false);
  105. public bool AimMode
  106. {
  107. get => GetValue(AimModeProperty);
  108. set => SetValue(AimModeProperty, value);
  109. }
  110. public static readonly StyledProperty<bool> ViewfinderModeProperty =
  111. AvaloniaProperty.Register<CameraView, bool>(nameof(ViewfinderMode), false);
  112. public bool ViewfinderMode
  113. {
  114. get => GetValue(ViewfinderModeProperty);
  115. set => SetValue(ViewfinderModeProperty, value);
  116. }
  117. public static readonly StyledProperty<bool> CaptureNextFrameProperty =
  118. AvaloniaProperty.Register<CameraView, bool>(nameof(CaptureNextFrame), false);
  119. public bool CaptureNextFrame
  120. {
  121. get => GetValue(CaptureNextFrameProperty);
  122. set => SetValue(CaptureNextFrameProperty, value);
  123. }
  124. public static readonly StyledProperty<bool> ForceFrameCaptureProperty =
  125. AvaloniaProperty.Register<CameraView, bool>(nameof(ForceFrameCapture), false);
  126. public bool ForceFrameCapture
  127. {
  128. get => GetValue(ForceFrameCaptureProperty);
  129. set => SetValue(ForceFrameCaptureProperty, value);
  130. }
  131. public static readonly StyledProperty<float> RequestZoomFactorProperty =
  132. AvaloniaProperty.Register<CameraView, float>(nameof(RequestZoomFactor), -1f);
  133. public float RequestZoomFactor
  134. {
  135. get => GetValue(RequestZoomFactorProperty);
  136. set => SetValue(RequestZoomFactorProperty, value);
  137. }
  138. public static readonly StyledProperty<float> CurrentZoomFactorProperty =
  139. AvaloniaProperty.Register<CameraView, float>(nameof(CurrentZoomFactor), -1f);
  140. public float CurrentZoomFactor
  141. {
  142. get => GetValue(CurrentZoomFactorProperty);
  143. set => SetValue(CurrentZoomFactorProperty, value);
  144. }
  145. public static readonly StyledProperty<float> MinZoomFactorProperty =
  146. AvaloniaProperty.Register<CameraView, float>(nameof(MinZoomFactor), -1f);
  147. public float MinZoomFactor
  148. {
  149. get => GetValue(MinZoomFactorProperty);
  150. set => SetValue(MinZoomFactorProperty, value);
  151. }
  152. public static readonly StyledProperty<float> MaxZoomFactorProperty =
  153. AvaloniaProperty.Register<CameraView, float>(nameof(MaxZoomFactor), -1f);
  154. public float MaxZoomFactor
  155. {
  156. get => GetValue(MaxZoomFactorProperty);
  157. set => SetValue(MaxZoomFactorProperty, value);
  158. }
  159. public static readonly StyledProperty<float[]> DeviceSwitchZoomFactorProperty =
  160. AvaloniaProperty.Register<CameraView, float[]>(nameof(DeviceSwitchZoomFactor), []);
  161. public float[] DeviceSwitchZoomFactor
  162. {
  163. get => GetValue(DeviceSwitchZoomFactorProperty);
  164. set => SetValue(DeviceSwitchZoomFactorProperty, value);
  165. }
  166. public CameraView()
  167. {
  168. InitializeComponent();
  169. LayoutUpdated += CameraView_LayoutUpdated;
  170. }
  171. private void CameraView_LayoutUpdated(object? sender, EventArgs e)
  172. {
  173. Host.TryUpdateNativeControlPosition();
  174. }
  175. public void DetectionFinished(HashSet<BarcodeResult> barCodeResults)
  176. {
  177. // if (_poolingTimer.Enabled)
  178. // {
  179. // _poolingTimer.Stop();
  180. // }
  181. TriggerOnDetectionFinished(barCodeResults);
  182. }
  183. private void TriggerOnDetectionFinished(HashSet<BarcodeResult> barCodeResults)
  184. {
  185. Dispatcher.UIThread.InvokeAsync(() =>
  186. {
  187. if(OnDetectionFinishedCommand?.CanExecute(barCodeResults) ?? false)
  188. {
  189. OnDetectionFinishedCommand?.Execute(barCodeResults);
  190. }
  191. });
  192. }
  193. public void TriggerOnImageCaptured(PlatformImage image)
  194. {
  195. Dispatcher.UIThread.InvokeAsync(() =>
  196. {
  197. CaptureNextFrame = false;
  198. if (PauseScanning)
  199. return;
  200. // OnImageCaptured?.Invoke(this, new OnImageCapturedEventArg { Image = image });
  201. if (OnImageCapturedCommand?.CanExecute(image) ?? false)
  202. OnImageCapturedCommand?.Execute(image);
  203. });
  204. }
  205. }
  206. internal class CameraViewControlHost : NativeControlHost
  207. {
  208. protected override IPlatformHandle CreateNativeControlCore(IPlatformHandle parent)
  209. {
  210. if(Parent is CameraView cameraView)
  211. {
  212. return PlatformTools.CameraViewControl.CreateControl(cameraView, parent)
  213. ?? base.CreateNativeControlCore(parent);
  214. }
  215. else
  216. {
  217. return base.CreateNativeControlCore(parent);
  218. }
  219. }
  220. }