From 8830f43e5c1591b04826cf2d74ab9c05c78d39b3 Mon Sep 17 00:00:00 2001 From: redth Date: Fri, 5 Aug 2022 20:37:48 -0400 Subject: [PATCH 1/4] Refactor the ios camera config lock --- .../Apple/CameraManager.ios.maccatalyst.cs | 55 +++++++++++++++---- 1 file changed, 45 insertions(+), 10 deletions(-) diff --git a/ZXing.Net.MAUI/Apple/CameraManager.ios.maccatalyst.cs b/ZXing.Net.MAUI/Apple/CameraManager.ios.maccatalyst.cs index 02f5d19..f5783a8 100644 --- a/ZXing.Net.MAUI/Apple/CameraManager.ios.maccatalyst.cs +++ b/ZXing.Net.MAUI/Apple/CameraManager.ios.maccatalyst.cs @@ -136,14 +136,38 @@ public void UpdateCamera() public void Disconnect() { - captureSession.RemoveOutput(videoDataOutput); - captureSession.StopRunning(); + if (captureSession != null) + { + if (captureSession.Running) + captureSession.StopRunning(); + + captureSession.RemoveOutput(videoDataOutput); + + // Cleanup old input + if (captureInput != null && captureSession.Inputs.Length > 0 && captureSession.Inputs.Contains(captureInput)) + { + captureSession.RemoveInput(captureInput); + captureInput.Dispose(); + captureInput = null; + } + + // Cleanup old device + if (captureDevice != null) + { + captureDevice.Dispose(); + captureDevice = null; + } + } } public void UpdateTorch(bool on) { if (captureDevice != null && captureDevice.HasTorch && captureDevice.TorchAvailable) - captureDevice.TorchMode = on ? AVCaptureTorchMode.On : AVCaptureTorchMode.Off; + { + var isOn = captureDevice.TorchActive; + if (on != isOn) + captureDevice.TorchMode = on ? AVCaptureTorchMode.On : AVCaptureTorchMode.Off; + } } public void Focus(Microsoft.Maui.Graphics.Point point) @@ -158,12 +182,25 @@ public void Focus(Microsoft.Maui.Graphics.Point point) //See if it supports focusing on a point if (captureDevice.FocusPointOfInterestSupported && !captureDevice.AdjustingFocus) { - //Lock device to config - if (captureDevice.LockForConfiguration(out var err)) + CaptureDevicePerformWithLockedConfiguration(() => { //Focus at the point touched captureDevice.FocusPointOfInterest = point; - captureDevice.FocusMode = AVCaptureFocusMode.ContinuousAutoFocus; + captureDevice.FocusMode = focusMode; + }); + } + } + + void CaptureDevicePerformWithLockedConfiguration(Action handler) + { + if (captureDevice.LockForConfiguration(out var err)) + { + try + { + handler(); + } + finally + { captureDevice.UnlockForConfiguration(); } } @@ -178,14 +215,12 @@ public void AutoFocus() if (captureDevice.IsFocusModeSupported(AVCaptureFocusMode.ContinuousAutoFocus)) focusMode = AVCaptureFocusMode.ContinuousAutoFocus; - //Lock device to config - if (captureDevice.LockForConfiguration(out var err)) + CaptureDevicePerformWithLockedConfiguration(() => { if (captureDevice.FocusPointOfInterestSupported) captureDevice.FocusPointOfInterest = CoreGraphics.CGPoint.Empty; captureDevice.FocusMode = focusMode; - captureDevice.UnlockForConfiguration(); - } + }); } public void Dispose() From 86b26d02a997f32308c1e31ac1c7bfc8a91ecf80 Mon Sep 17 00:00:00 2001 From: redth Date: Sun, 7 Aug 2022 15:23:09 -0400 Subject: [PATCH 2/4] Default torch to off --- ZXing.Net.MAUI/Apple/CameraManager.ios.maccatalyst.cs | 2 +- ZXing.Net.MAUI/Controls/CameraBarcodeReaderView.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ZXing.Net.MAUI/Apple/CameraManager.ios.maccatalyst.cs b/ZXing.Net.MAUI/Apple/CameraManager.ios.maccatalyst.cs index f5783a8..a3716a8 100644 --- a/ZXing.Net.MAUI/Apple/CameraManager.ios.maccatalyst.cs +++ b/ZXing.Net.MAUI/Apple/CameraManager.ios.maccatalyst.cs @@ -164,7 +164,7 @@ public void UpdateTorch(bool on) { if (captureDevice != null && captureDevice.HasTorch && captureDevice.TorchAvailable) { - var isOn = captureDevice.TorchActive; + var isOn = captureDevice?.TorchActive ?? false; if (on != isOn) captureDevice.TorchMode = on ? AVCaptureTorchMode.On : AVCaptureTorchMode.Off; } diff --git a/ZXing.Net.MAUI/Controls/CameraBarcodeReaderView.cs b/ZXing.Net.MAUI/Controls/CameraBarcodeReaderView.cs index 7b460b9..0a822ee 100644 --- a/ZXing.Net.MAUI/Controls/CameraBarcodeReaderView.cs +++ b/ZXing.Net.MAUI/Controls/CameraBarcodeReaderView.cs @@ -56,7 +56,7 @@ void Handler_FrameReady(object sender, CameraFrameBufferEventArgs e) => FrameReady?.Invoke(this, e); public static readonly BindableProperty IsTorchOnProperty = - BindableProperty.Create(nameof(IsTorchOn), typeof(bool), typeof(CameraBarcodeReaderView), defaultValue: true); + BindableProperty.Create(nameof(IsTorchOn), typeof(bool), typeof(CameraBarcodeReaderView), defaultValue: false); public bool IsTorchOn { From 8883711871cd5c6668a60054d5689bfe9de375b6 Mon Sep 17 00:00:00 2001 From: redth Date: Sun, 7 Aug 2022 15:28:07 -0400 Subject: [PATCH 3/4] Lock config when setting torch on ios --- .../Apple/CameraManager.ios.maccatalyst.cs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/ZXing.Net.MAUI/Apple/CameraManager.ios.maccatalyst.cs b/ZXing.Net.MAUI/Apple/CameraManager.ios.maccatalyst.cs index a3716a8..33eb680 100644 --- a/ZXing.Net.MAUI/Apple/CameraManager.ios.maccatalyst.cs +++ b/ZXing.Net.MAUI/Apple/CameraManager.ios.maccatalyst.cs @@ -165,8 +165,19 @@ public void UpdateTorch(bool on) if (captureDevice != null && captureDevice.HasTorch && captureDevice.TorchAvailable) { var isOn = captureDevice?.TorchActive ?? false; - if (on != isOn) - captureDevice.TorchMode = on ? AVCaptureTorchMode.On : AVCaptureTorchMode.Off; + + try + { + if (on != isOn) + { + CaptureDevicePerformWithLockedConfiguration(() => + captureDevice.TorchMode = on ? AVCaptureTorchMode.On : AVCaptureTorchMode.Off); + } + } + catch (Exception ex) + { + Console.WriteLine(ex); + } } } From 158d6fa7052191aae232fd67a538374d6b28a040 Mon Sep 17 00:00:00 2001 From: redth Date: Sun, 7 Aug 2022 15:31:56 -0400 Subject: [PATCH 4/4] Change package id for now Until ownership of original one is resolved... --- ZXing.Net.MAUI/ZXing.Net.MAUI.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ZXing.Net.MAUI/ZXing.Net.MAUI.csproj b/ZXing.Net.MAUI/ZXing.Net.MAUI.csproj index df7aa1b..a7c1391 100644 --- a/ZXing.Net.MAUI/ZXing.Net.MAUI.csproj +++ b/ZXing.Net.MAUI/ZXing.Net.MAUI.csproj @@ -2,7 +2,7 @@ net6.0-android;net6.0-maccatalyst;net6.0-ios $(TargetFrameworks);net6.0-windows10.0.19041 - ZXing.Net.Maui + Redth.ZXing.Net.Maui ZXing.Net.MAUI Barcode Scanner for .NET MAUI Redth True