From 8df90b7960a6f98abfdbe0c3e013057b79a985ce Mon Sep 17 00:00:00 2001 From: Jan Marek Date: Tue, 14 Nov 2023 11:03:28 +0100 Subject: [PATCH] Camera resources are released, NFC reader is available after barcode reader usage. #118 --- .../Controls/CameraBarcodeReaderView.cs | 12 ++++++++++-- ZXing.Net.MAUI.Controls/Controls/CameraView.cs | 12 ++++++++++-- ZXing.Net.MAUI/CameraBarcodeReaderViewHandler.cs | 1 + ZXing.Net.MAUI/CameraViewHandler.cs | 1 + .../Platforms/Android/CameraManager.android.cs | 2 ++ 5 files changed, 24 insertions(+), 4 deletions(-) diff --git a/ZXing.Net.MAUI.Controls/Controls/CameraBarcodeReaderView.cs b/ZXing.Net.MAUI.Controls/Controls/CameraBarcodeReaderView.cs index 86b8b94..2f67e37 100644 --- a/ZXing.Net.MAUI.Controls/Controls/CameraBarcodeReaderView.cs +++ b/ZXing.Net.MAUI.Controls/Controls/CameraBarcodeReaderView.cs @@ -15,7 +15,12 @@ public partial class CameraBarcodeReaderView : View, ICameraBarcodeReaderView public event EventHandler BarcodesDetected; public event EventHandler FrameReady; - void ICameraBarcodeReaderView.BarcodesDetected(BarcodeDetectionEventArgs e) => BarcodesDetected?.Invoke(this, e); + public CameraBarcodeReaderView() + { + Unloaded += (s, e) => Cleanup(); + } + + void ICameraBarcodeReaderView.BarcodesDetected(BarcodeDetectionEventArgs e) => BarcodesDetected?.Invoke(this, e); void ICameraFrameAnalyzer.FrameReady(ZXing.Net.Maui.CameraFrameBufferEventArgs e) => FrameReady?.Invoke(this, e); public static readonly BindableProperty OptionsProperty = @@ -65,5 +70,8 @@ public void Focus(Point point) CameraBarcodeReaderViewHandler StrongHandler => Handler as CameraBarcodeReaderViewHandler; - } + + private void Cleanup() + => Handler?.DisconnectHandler(); + } } diff --git a/ZXing.Net.MAUI.Controls/Controls/CameraView.cs b/ZXing.Net.MAUI.Controls/Controls/CameraView.cs index 724d38d..ebc3a1a 100644 --- a/ZXing.Net.MAUI.Controls/Controls/CameraView.cs +++ b/ZXing.Net.MAUI.Controls/Controls/CameraView.cs @@ -9,7 +9,12 @@ public partial class CameraView : View, ICameraView { public event EventHandler FrameReady; - void ICameraFrameAnalyzer.FrameReady(CameraFrameBufferEventArgs e) + public CameraView() + { + Unloaded += (s, e) => Cleanup(); + } + + void ICameraFrameAnalyzer.FrameReady(CameraFrameBufferEventArgs e) => FrameReady?.Invoke(this, e); public static readonly BindableProperty IsTorchOnProperty = @@ -38,5 +43,8 @@ public void Focus(Point point) CameraViewHandler StrongHandler => Handler as CameraViewHandler; - } + + private void Cleanup() + => Handler?.DisconnectHandler(); + } } diff --git a/ZXing.Net.MAUI/CameraBarcodeReaderViewHandler.cs b/ZXing.Net.MAUI/CameraBarcodeReaderViewHandler.cs index 971f42f..77ab6bb 100644 --- a/ZXing.Net.MAUI/CameraBarcodeReaderViewHandler.cs +++ b/ZXing.Net.MAUI/CameraBarcodeReaderViewHandler.cs @@ -62,6 +62,7 @@ protected override void DisconnectHandler(NativePlatformCameraPreviewView native cameraManager.FrameReady -= CameraManager_FrameReady; cameraManager.Disconnect(); + cameraManager.Dispose(); base.DisconnectHandler(nativeView); } diff --git a/ZXing.Net.MAUI/CameraViewHandler.cs b/ZXing.Net.MAUI/CameraViewHandler.cs index 239fc9a..652ec9a 100644 --- a/ZXing.Net.MAUI/CameraViewHandler.cs +++ b/ZXing.Net.MAUI/CameraViewHandler.cs @@ -59,6 +59,7 @@ protected override void DisconnectHandler(NativePlatformCameraPreviewView native cameraManager.FrameReady -= CameraManager_FrameReady; cameraManager.Disconnect(); + cameraManager.Dispose(); base.DisconnectHandler(nativeView); } diff --git a/ZXing.Net.MAUI/Platforms/Android/CameraManager.android.cs b/ZXing.Net.MAUI/Platforms/Android/CameraManager.android.cs index 7e764f1..86c5308 100644 --- a/ZXing.Net.MAUI/Platforms/Android/CameraManager.android.cs +++ b/ZXing.Net.MAUI/Platforms/Android/CameraManager.android.cs @@ -127,6 +127,8 @@ public void AutoFocus() public void Dispose() { + cameraProvider?.Shutdown(); + cameraExecutor?.Shutdown(); cameraExecutor?.Dispose(); }