|
1 |
| -using System; |
2 |
| -using System.Linq; |
3 |
| -using Microsoft.Extensions.DependencyInjection; |
| 1 | +using Microsoft.Extensions.DependencyInjection; |
4 | 2 | using Microsoft.Maui;
|
5 | 3 | using Microsoft.Maui.Graphics;
|
6 | 4 | using Microsoft.Maui.Handlers;
|
| 5 | +using System; |
| 6 | +using System.Linq; |
7 | 7 |
|
8 | 8 | namespace ZXing.Net.Maui
|
9 | 9 | {
|
10 |
| - public partial class CameraBarcodeReaderViewHandler : ViewHandler<ICameraBarcodeReaderView, NativePlatformCameraPreviewView> |
11 |
| - { |
12 |
| - public static PropertyMapper<ICameraBarcodeReaderView, CameraBarcodeReaderViewHandler> CameraBarcodeReaderViewMapper = new() |
13 |
| - { |
14 |
| - [nameof(ICameraBarcodeReaderView.Options)] = MapOptions, |
15 |
| - [nameof(ICameraBarcodeReaderView.IsDetecting)] = MapIsDetecting, |
16 |
| - [nameof(ICameraBarcodeReaderView.IsTorchOn)] = (handler, virtualView) => handler.cameraManager.UpdateTorch(virtualView.IsTorchOn), |
17 |
| - [nameof(ICameraBarcodeReaderView.CameraLocation)] = (handler, virtualView) => handler.cameraManager.UpdateCameraLocation(virtualView.CameraLocation) |
18 |
| - }; |
| 10 | + public partial class CameraBarcodeReaderViewHandler : ViewHandler<ICameraBarcodeReaderView, NativePlatformCameraPreviewView> |
| 11 | + { |
| 12 | + public static PropertyMapper<ICameraBarcodeReaderView, CameraBarcodeReaderViewHandler> CameraBarcodeReaderViewMapper = new() |
| 13 | + { |
| 14 | + [nameof(ICameraBarcodeReaderView.Options)] = MapOptions, |
| 15 | + [nameof(ICameraBarcodeReaderView.IsDetecting)] = MapIsDetecting, |
| 16 | + [nameof(ICameraBarcodeReaderView.IsTorchOn)] = (handler, virtualView) => handler.cameraManager.UpdateTorch(virtualView.IsTorchOn), |
| 17 | + [nameof(ICameraBarcodeReaderView.CameraLocation)] = (handler, virtualView) => handler.cameraManager.UpdateCameraLocation(virtualView.CameraLocation) |
| 18 | + }; |
19 | 19 |
|
20 |
| - public static CommandMapper<ICameraBarcodeReaderView, CameraBarcodeReaderViewHandler> CameraBarcodeReaderCommandMapper = new() |
21 |
| - { |
22 |
| - [nameof(ICameraBarcodeReaderView.Focus)] = MapFocus, |
23 |
| - [nameof(ICameraBarcodeReaderView.AutoFocus)] = MapAutoFocus, |
24 |
| - }; |
| 20 | + public static CommandMapper<ICameraBarcodeReaderView, CameraBarcodeReaderViewHandler> CameraBarcodeReaderCommandMapper = new() |
| 21 | + { |
| 22 | + [nameof(ICameraBarcodeReaderView.Focus)] = MapFocus, |
| 23 | + [nameof(ICameraBarcodeReaderView.AutoFocus)] = MapAutoFocus, |
| 24 | + }; |
25 | 25 |
|
26 |
| - public CameraBarcodeReaderViewHandler() : base(CameraBarcodeReaderViewMapper) |
27 |
| - { |
28 |
| - } |
| 26 | + public CameraBarcodeReaderViewHandler() : base(CameraBarcodeReaderViewMapper, CameraBarcodeReaderCommandMapper) |
| 27 | + { |
| 28 | + } |
29 | 29 |
|
30 |
| - public CameraBarcodeReaderViewHandler(PropertyMapper mapper = null) : base(mapper ?? CameraBarcodeReaderViewMapper) |
31 |
| - { |
32 |
| - } |
| 30 | + public CameraBarcodeReaderViewHandler(PropertyMapper propertyMapper = null, CommandMapper commandMapper = null) |
| 31 | + : base(propertyMapper ?? CameraBarcodeReaderViewMapper, commandMapper ?? CameraBarcodeReaderCommandMapper) |
| 32 | + { |
| 33 | + } |
33 | 34 |
|
34 |
| - CameraManager cameraManager; |
| 35 | + CameraManager cameraManager; |
35 | 36 |
|
36 |
| - Readers.IBarcodeReader barcodeReader; |
| 37 | + Readers.IBarcodeReader barcodeReader; |
37 | 38 |
|
38 |
| - protected Readers.IBarcodeReader BarcodeReader |
39 |
| - => barcodeReader ??= Services.GetService<Readers.IBarcodeReader>(); |
| 39 | + protected Readers.IBarcodeReader BarcodeReader |
| 40 | + => barcodeReader ??= Services.GetService<Readers.IBarcodeReader>(); |
40 | 41 |
|
41 |
| - protected override NativePlatformCameraPreviewView CreatePlatformView() |
42 |
| - { |
43 |
| - if (cameraManager == null) |
44 |
| - cameraManager = new(MauiContext, VirtualView?.CameraLocation ?? CameraLocation.Rear); |
45 |
| - var v = cameraManager.CreateNativeView(); |
46 |
| - return v; |
47 |
| - } |
| 42 | + protected override NativePlatformCameraPreviewView CreatePlatformView() |
| 43 | + { |
| 44 | + if (cameraManager == null) |
| 45 | + cameraManager = new(MauiContext, VirtualView?.CameraLocation ?? CameraLocation.Rear); |
| 46 | + var v = cameraManager.CreateNativeView(); |
| 47 | + return v; |
| 48 | + } |
48 | 49 |
|
49 |
| - protected override async void ConnectHandler(NativePlatformCameraPreviewView nativeView) |
50 |
| - { |
51 |
| - base.ConnectHandler(nativeView); |
| 50 | + protected override async void ConnectHandler(NativePlatformCameraPreviewView nativeView) |
| 51 | + { |
| 52 | + base.ConnectHandler(nativeView); |
52 | 53 |
|
53 |
| - if (await cameraManager.CheckPermissions()) |
54 |
| - cameraManager.Connect(); |
| 54 | + if (await cameraManager.CheckPermissions()) |
| 55 | + cameraManager.Connect(); |
55 | 56 |
|
56 |
| - cameraManager.FrameReady += CameraManager_FrameReady; |
57 |
| - } |
| 57 | + cameraManager.FrameReady += CameraManager_FrameReady; |
| 58 | + } |
58 | 59 |
|
59 |
| - protected override void DisconnectHandler(NativePlatformCameraPreviewView nativeView) |
60 |
| - { |
61 |
| - cameraManager.FrameReady -= CameraManager_FrameReady; |
| 60 | + protected override void DisconnectHandler(NativePlatformCameraPreviewView nativeView) |
| 61 | + { |
| 62 | + cameraManager.FrameReady -= CameraManager_FrameReady; |
62 | 63 |
|
63 |
| - cameraManager.Disconnect(); |
| 64 | + cameraManager.Disconnect(); |
64 | 65 |
|
65 |
| - base.DisconnectHandler(nativeView); |
66 |
| - } |
| 66 | + base.DisconnectHandler(nativeView); |
| 67 | + } |
67 | 68 |
|
68 |
| - private void CameraManager_FrameReady(object sender, CameraFrameBufferEventArgs e) |
69 |
| - { |
70 |
| - VirtualView?.FrameReady(e); |
| 69 | + private void CameraManager_FrameReady(object sender, CameraFrameBufferEventArgs e) |
| 70 | + { |
| 71 | + VirtualView?.FrameReady(e); |
71 | 72 |
|
72 |
| - if (VirtualView.IsDetecting) |
73 |
| - { |
74 |
| - var barcodes = BarcodeReader.Decode(e.Data); |
| 73 | + if (VirtualView.IsDetecting) |
| 74 | + { |
| 75 | + var barcodes = BarcodeReader.Decode(e.Data); |
75 | 76 |
|
76 |
| - if (barcodes?.Any() ?? false) |
77 |
| - VirtualView?.BarcodesDetected(new BarcodeDetectionEventArgs(barcodes)); |
78 |
| - } |
79 |
| - } |
| 77 | + if (barcodes?.Any() ?? false) |
| 78 | + VirtualView?.BarcodesDetected(new BarcodeDetectionEventArgs(barcodes)); |
| 79 | + } |
| 80 | + } |
80 | 81 |
|
81 |
| - public static void MapOptions(CameraBarcodeReaderViewHandler handler, ICameraBarcodeReaderView cameraBarcodeReaderView) |
82 |
| - => handler.BarcodeReader.Options = cameraBarcodeReaderView.Options; |
| 82 | + public static void MapOptions(CameraBarcodeReaderViewHandler handler, ICameraBarcodeReaderView cameraBarcodeReaderView) |
| 83 | + => handler.BarcodeReader.Options = cameraBarcodeReaderView.Options; |
83 | 84 |
|
84 |
| - public static void MapIsDetecting(CameraBarcodeReaderViewHandler handler, ICameraBarcodeReaderView cameraBarcodeReaderView) |
85 |
| - { } |
| 85 | + public static void MapIsDetecting(CameraBarcodeReaderViewHandler handler, ICameraBarcodeReaderView cameraBarcodeReaderView) |
| 86 | + { } |
86 | 87 |
|
87 |
| - public void Focus(Point point) |
88 |
| - => cameraManager?.Focus(point); |
| 88 | + public void Focus(Point point) |
| 89 | + => cameraManager?.Focus(point); |
89 | 90 |
|
90 |
| - public void AutoFocus() |
91 |
| - => cameraManager?.AutoFocus(); |
| 91 | + public void AutoFocus() |
| 92 | + => cameraManager?.AutoFocus(); |
92 | 93 |
|
93 |
| - public static void MapFocus(CameraBarcodeReaderViewHandler handler, ICameraBarcodeReaderView cameraBarcodeReaderView, object? parameter) |
94 |
| - { |
95 |
| - if (parameter is not Point point) |
96 |
| - throw new ArgumentException("Invalid parameter", "point"); |
| 94 | + public static void MapFocus(CameraBarcodeReaderViewHandler handler, ICameraBarcodeReaderView cameraBarcodeReaderView, object? parameter) |
| 95 | + { |
| 96 | + if (parameter is not Point point) |
| 97 | + throw new ArgumentException("Invalid parameter", "point"); |
97 | 98 |
|
98 |
| - handler.Focus(point); |
99 |
| - } |
| 99 | + handler.Focus(point); |
| 100 | + } |
100 | 101 |
|
101 |
| - public static void MapAutoFocus(CameraBarcodeReaderViewHandler handler, ICameraBarcodeReaderView cameraBarcodeReaderView, object? parameters) |
102 |
| - => handler.AutoFocus(); |
103 |
| - } |
| 102 | + public static void MapAutoFocus(CameraBarcodeReaderViewHandler handler, ICameraBarcodeReaderView cameraBarcodeReaderView, object? parameters) |
| 103 | + => handler.AutoFocus(); |
| 104 | + } |
104 | 105 | }
|
0 commit comments