|
| 1 | +using DIPS.Mobile.UI.API.Camera; |
| 2 | +using DIPS.Mobile.UI.API.Camera.BarcodeScanning; |
| 3 | +using DIPS.Mobile.UI.Resources.Styles; |
| 4 | +using DIPS.Mobile.UI.Resources.Styles.Label; |
| 5 | +using Colors = Microsoft.Maui.Graphics.Colors; |
| 6 | + |
| 7 | +namespace Components.ComponentsSamples.BarcodeScanning; |
| 8 | + |
| 9 | +public partial class BarcodeOverlaySample |
| 10 | +{ |
| 11 | + private readonly BarcodeScanner m_barcodeScanner; |
| 12 | + private BarcodeScanningResultBottomSheet? m_barCodeResultBottomSheet; |
| 13 | + private View? m_topToolbarView; |
| 14 | + |
| 15 | + public BarcodeOverlaySample() |
| 16 | + { |
| 17 | + InitializeComponent(); |
| 18 | + m_barcodeScanner = new BarcodeScanner(); |
| 19 | + } |
| 20 | + |
| 21 | + private async Task Start() |
| 22 | + { |
| 23 | + try |
| 24 | + { |
| 25 | + await m_barcodeScanner.Start(new BarcodeScannerStartOptions |
| 26 | + { |
| 27 | + Preview = CameraPreview, |
| 28 | + OnCameraFailed = CameraFailed, |
| 29 | + OnBarcodeAcceptedAsync = HandleBarcodeAcceptedAsync, |
| 30 | + ScanRectangle = new BarcodeScanRectangleOptions |
| 31 | + { |
| 32 | + WidthFraction = 0.8f, |
| 33 | + HeightFraction = 0.3f |
| 34 | + } |
| 35 | + }); |
| 36 | + |
| 37 | + CameraPreview.AddTopToolbarView(GetTopToolbarView()); |
| 38 | + } |
| 39 | + catch (Exception exception) |
| 40 | + { |
| 41 | + await Application.Current?.MainPage?.DisplayAlert("Failed, check console!", exception.Message, "Ok")!; |
| 42 | + Console.WriteLine(exception); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + private void CameraFailed(CameraException e) |
| 47 | + { |
| 48 | + App.Current.MainPage.DisplayAlert("Something failed!", e.Message, "Ok"); |
| 49 | + } |
| 50 | + |
| 51 | + private View GetTopToolbarView() |
| 52 | + { |
| 53 | + return m_topToolbarView ??= new VerticalStackLayout |
| 54 | + { |
| 55 | + Spacing = 4, |
| 56 | + Children = |
| 57 | + { |
| 58 | + new Label |
| 59 | + { |
| 60 | + Text = "Scan barcode", |
| 61 | + Style = Styles.GetLabelStyle(LabelStyle.UI200), |
| 62 | + TextColor = Colors.White, |
| 63 | + HorizontalTextAlignment = TextAlignment.Center |
| 64 | + }, |
| 65 | + new Label |
| 66 | + { |
| 67 | + Text = "Point the camera at a barcode", |
| 68 | + Style = Styles.GetLabelStyle(LabelStyle.UI100), |
| 69 | + TextColor = Colors.White, |
| 70 | + HorizontalTextAlignment = TextAlignment.Center, |
| 71 | + Opacity = 0.7 |
| 72 | + } |
| 73 | + } |
| 74 | + }; |
| 75 | + } |
| 76 | + |
| 77 | + private Task HandleBarcodeAcceptedAsync(BarcodeScanResult barcodeScanResult) |
| 78 | + { |
| 79 | + if (m_barCodeResultBottomSheet is { HasBarCode: true }) |
| 80 | + { |
| 81 | + return Task.CompletedTask; |
| 82 | + } |
| 83 | + |
| 84 | + m_barCodeResultBottomSheet = new BarcodeScanningResultBottomSheet(); |
| 85 | + m_barCodeResultBottomSheet.Closed += BottomSheetClosed; |
| 86 | + m_barCodeResultBottomSheet.OpenWithBarCode(barcodeScanResult); |
| 87 | + m_barcodeScanner.PauseScanning(resetOverlay: false); |
| 88 | + return Task.CompletedTask; |
| 89 | + } |
| 90 | + |
| 91 | + private void BottomSheetClosed(object? sender, EventArgs e) |
| 92 | + { |
| 93 | + if (m_barCodeResultBottomSheet != null) |
| 94 | + { |
| 95 | + m_barCodeResultBottomSheet.Closed -= BottomSheetClosed; |
| 96 | + } |
| 97 | + |
| 98 | + m_barCodeResultBottomSheet = null; |
| 99 | + m_barcodeScanner.ResumeScanning(); |
| 100 | + } |
| 101 | + |
| 102 | + protected override void OnAppearing() |
| 103 | + { |
| 104 | + _ = Start(); |
| 105 | + base.OnAppearing(); |
| 106 | + } |
| 107 | + |
| 108 | + private void Close(object? sender, EventArgs e) |
| 109 | + { |
| 110 | + m_barcodeScanner.StopAndDispose(); |
| 111 | + Shell.Current.Navigation.PopModalAsync(); |
| 112 | + } |
| 113 | +} |
0 commit comments