123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- using Avalonia;
- using Avalonia.Controls;
- using Avalonia.Markup.Xaml;
- using Avalonia.Platform;
- using Avalonia.Threading;
- using InABox.Core;
- using Microsoft.Maui.Graphics.Platform;
- using System.Timers;
- using System.Windows.Input;
- using Timer = System.Timers.Timer;
- namespace InABox.Avalonia.Platform.Barcodes;
- public interface ICameraViewControl : ILoggable
- {
- IPlatformHandle CreateControl(CameraView view, IPlatformHandle parent);
- }
- public class DefaultCameraViewControl : ICameraViewControl
- {
- public Logger? Logger { get; set; }
- public IPlatformHandle CreateControl(CameraView view, IPlatformHandle parent)
- {
- return null;
- }
- }
- public partial class CameraView : UserControl
- {
- public static readonly StyledProperty<ICommand> OnDetectionFinishedCommandProperty =
- AvaloniaProperty.Register<CameraView, ICommand>(nameof(OnDetectionFinishedCommand));
- public ICommand OnDetectionFinishedCommand
- {
- get => GetValue(OnDetectionFinishedCommandProperty);
- set => SetValue(OnDetectionFinishedCommandProperty, value);
- }
- public static readonly StyledProperty<ICommand> OnImageCapturedCommandProperty =
- AvaloniaProperty.Register<CameraView, ICommand>(nameof(OnImageCapturedCommand));
- public ICommand OnImageCapturedCommand
- {
- get => GetValue(OnImageCapturedCommandProperty);
- set => SetValue(OnImageCapturedCommandProperty, value);
- }
- public static readonly StyledProperty<bool> VibrationOnDetectedProperty =
- AvaloniaProperty.Register<CameraView, bool>(nameof(VibrationOnDetected), false);
- public bool VibrationOnDetected
- {
- get => GetValue(VibrationOnDetectedProperty);
- set => SetValue(VibrationOnDetectedProperty, value);
- }
- public static readonly StyledProperty<bool> CameraEnabledProperty =
- AvaloniaProperty.Register<CameraView, bool>(nameof(CameraEnabled), false);
- public bool CameraEnabled
- {
- get => GetValue(CameraEnabledProperty);
- set => SetValue(CameraEnabledProperty, value);
- }
- public static readonly StyledProperty<bool> PauseScanningProperty =
- AvaloniaProperty.Register<CameraView, bool>(nameof(PauseScanning), false);
- public bool PauseScanning
- {
- get => GetValue(PauseScanningProperty);
- set => SetValue(PauseScanningProperty, value);
- }
- public static readonly StyledProperty<bool> ForceInvertedProperty =
- AvaloniaProperty.Register<CameraView, bool>(nameof(ForceInverted), false);
- public bool ForceInverted
- {
- get => GetValue(ForceInvertedProperty);
- set => SetValue(ForceInvertedProperty, value);
- }
- public static readonly StyledProperty<bool> TorchOnProperty =
- AvaloniaProperty.Register<CameraView, bool>(nameof(TorchOn), false);
- public bool TorchOn
- {
- get => GetValue(TorchOnProperty);
- set => SetValue(TorchOnProperty, value);
- }
- public static readonly StyledProperty<bool> TapToFocusEnabledProperty =
- AvaloniaProperty.Register<CameraView, bool>(nameof(TapToFocusEnabled), false);
- public bool TapToFocusEnabled
- {
- get => GetValue(TapToFocusEnabledProperty);
- set => SetValue(TapToFocusEnabledProperty, value);
- }
- public static readonly StyledProperty<CameraFacing> CameraFacingProperty =
- AvaloniaProperty.Register<CameraView, CameraFacing>(nameof(CameraFacing), CameraFacing.Back);
- public CameraFacing CameraFacing
- {
- get => GetValue(CameraFacingProperty);
- set => SetValue(CameraFacingProperty, value);
- }
- public static readonly StyledProperty<CaptureQuality> CaptureQualityProperty =
- AvaloniaProperty.Register<CameraView, CaptureQuality>(nameof(CaptureQuality), CaptureQuality.Medium);
- public CaptureQuality CaptureQuality
- {
- get => GetValue(CaptureQualityProperty);
- set => SetValue(CaptureQualityProperty, value);
- }
- public static readonly StyledProperty<BarcodeFormats> BarcodeSymbologiesProperty =
- AvaloniaProperty.Register<CameraView, BarcodeFormats>(nameof(BarcodeSymbologies), BarcodeFormats.All);
- public BarcodeFormats BarcodeSymbologies
- {
- get => GetValue(BarcodeSymbologiesProperty);
- set => SetValue(BarcodeSymbologiesProperty, value);
- }
- public static readonly StyledProperty<bool> AimModeProperty =
- AvaloniaProperty.Register<CameraView, bool>(nameof(AimMode), false);
- public bool AimMode
- {
- get => GetValue(AimModeProperty);
- set => SetValue(AimModeProperty, value);
- }
- public static readonly StyledProperty<bool> ViewfinderModeProperty =
- AvaloniaProperty.Register<CameraView, bool>(nameof(ViewfinderMode), false);
- public bool ViewfinderMode
- {
- get => GetValue(ViewfinderModeProperty);
- set => SetValue(ViewfinderModeProperty, value);
- }
- public static readonly StyledProperty<bool> CaptureNextFrameProperty =
- AvaloniaProperty.Register<CameraView, bool>(nameof(CaptureNextFrame), false);
- public bool CaptureNextFrame
- {
- get => GetValue(CaptureNextFrameProperty);
- set => SetValue(CaptureNextFrameProperty, value);
- }
- public static readonly StyledProperty<bool> ForceFrameCaptureProperty =
- AvaloniaProperty.Register<CameraView, bool>(nameof(ForceFrameCapture), false);
- public bool ForceFrameCapture
- {
- get => GetValue(ForceFrameCaptureProperty);
- set => SetValue(ForceFrameCaptureProperty, value);
- }
- public static readonly StyledProperty<float> RequestZoomFactorProperty =
- AvaloniaProperty.Register<CameraView, float>(nameof(RequestZoomFactor), -1f);
- public float RequestZoomFactor
- {
- get => GetValue(RequestZoomFactorProperty);
- set => SetValue(RequestZoomFactorProperty, value);
- }
- public static readonly StyledProperty<float> CurrentZoomFactorProperty =
- AvaloniaProperty.Register<CameraView, float>(nameof(CurrentZoomFactor), -1f);
- public float CurrentZoomFactor
- {
- get => GetValue(CurrentZoomFactorProperty);
- set => SetValue(CurrentZoomFactorProperty, value);
- }
- public static readonly StyledProperty<float> MinZoomFactorProperty =
- AvaloniaProperty.Register<CameraView, float>(nameof(MinZoomFactor), -1f);
- public float MinZoomFactor
- {
- get => GetValue(MinZoomFactorProperty);
- set => SetValue(MinZoomFactorProperty, value);
- }
- public static readonly StyledProperty<float> MaxZoomFactorProperty =
- AvaloniaProperty.Register<CameraView, float>(nameof(MaxZoomFactor), -1f);
- public float MaxZoomFactor
- {
- get => GetValue(MaxZoomFactorProperty);
- set => SetValue(MaxZoomFactorProperty, value);
- }
- public static readonly StyledProperty<float[]> DeviceSwitchZoomFactorProperty =
- AvaloniaProperty.Register<CameraView, float[]>(nameof(DeviceSwitchZoomFactor), []);
- public float[] DeviceSwitchZoomFactor
- {
- get => GetValue(DeviceSwitchZoomFactorProperty);
- set => SetValue(DeviceSwitchZoomFactorProperty, value);
- }
- public CameraView()
- {
- InitializeComponent();
- LayoutUpdated += CameraView_LayoutUpdated;
- }
- private void CameraView_LayoutUpdated(object? sender, EventArgs e)
- {
- Host.TryUpdateNativeControlPosition();
- }
- public void DetectionFinished(HashSet<BarcodeResult> barCodeResults)
- {
- // if (_poolingTimer.Enabled)
- // {
- // _poolingTimer.Stop();
- // }
- TriggerOnDetectionFinished(barCodeResults);
- }
- private void TriggerOnDetectionFinished(HashSet<BarcodeResult> barCodeResults)
- {
- Dispatcher.UIThread.InvokeAsync(() =>
- {
- if(OnDetectionFinishedCommand?.CanExecute(barCodeResults) ?? false)
- {
- OnDetectionFinishedCommand?.Execute(barCodeResults);
- }
- });
- }
- public void TriggerOnImageCaptured(PlatformImage image)
- {
- Dispatcher.UIThread.InvokeAsync(() =>
- {
- CaptureNextFrame = false;
- if (PauseScanning)
- return;
- // OnImageCaptured?.Invoke(this, new OnImageCapturedEventArg { Image = image });
- if (OnImageCapturedCommand?.CanExecute(image) ?? false)
- OnImageCapturedCommand?.Execute(image);
- });
- }
- }
- internal class CameraViewControlHost : NativeControlHost
- {
- protected override IPlatformHandle CreateNativeControlCore(IPlatformHandle parent)
- {
- if(Parent is CameraView cameraView)
- {
- return PlatformTools.CameraViewControl.CreateControl(cameraView, parent)
- ?? base.CreateNativeControlCore(parent);
- }
- else
- {
- return base.CreateNativeControlCore(parent);
- }
- }
- }
|