From 747818c71ddc924e75052c6ff7ab418e44a3ca21 Mon Sep 17 00:00:00 2001 From: Bryan Stadick <2057235+bstadick@users.noreply.github.com> Date: Fri, 19 Jul 2024 13:36:34 -0500 Subject: [PATCH 1/2] Add missing null checks in CameraBarcodeReaderViewHandler --- .../CameraBarcodeReaderViewHandler.cs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/ZXing.Net.MAUI/CameraBarcodeReaderViewHandler.cs b/ZXing.Net.MAUI/CameraBarcodeReaderViewHandler.cs index 971f42f..8727dd7 100644 --- a/ZXing.Net.MAUI/CameraBarcodeReaderViewHandler.cs +++ b/ZXing.Net.MAUI/CameraBarcodeReaderViewHandler.cs @@ -51,17 +51,23 @@ protected override async void ConnectHandler(NativePlatformCameraPreviewView nat { base.ConnectHandler(nativeView); - if (await cameraManager.CheckPermissions()) - cameraManager.Connect(); + if (cameraManager != null) + { + if (await cameraManager.CheckPermissions()) + cameraManager.Connect(); - cameraManager.FrameReady += CameraManager_FrameReady; + cameraManager.FrameReady += CameraManager_FrameReady; + } } protected override void DisconnectHandler(NativePlatformCameraPreviewView nativeView) { - cameraManager.FrameReady -= CameraManager_FrameReady; + if (cameraManager != null) + { + cameraManager.FrameReady -= CameraManager_FrameReady; - cameraManager.Disconnect(); + cameraManager.Disconnect(); + } base.DisconnectHandler(nativeView); } @@ -70,7 +76,7 @@ private void CameraManager_FrameReady(object sender, CameraFrameBufferEventArgs { VirtualView?.FrameReady(e); - if (VirtualView.IsDetecting) + if (VirtualView?.IsDetecting ?? false) { var barcodes = BarcodeReader.Decode(e.Data); From 43d71e1f916f688b20317c29cfc14c01f570b23f Mon Sep 17 00:00:00 2001 From: Bryan Stadick <2057235+bstadick@users.noreply.github.com> Date: Thu, 25 Jul 2024 11:00:09 -0500 Subject: [PATCH 2/2] Handle camera manager null in CameraViewHandler.cs --- ZXing.Net.MAUI/CameraViewHandler.cs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/ZXing.Net.MAUI/CameraViewHandler.cs b/ZXing.Net.MAUI/CameraViewHandler.cs index 239fc9a..9a20992 100644 --- a/ZXing.Net.MAUI/CameraViewHandler.cs +++ b/ZXing.Net.MAUI/CameraViewHandler.cs @@ -44,11 +44,14 @@ protected override NativePlatformCameraPreviewView CreatePlatformView() protected override async void ConnectHandler(NativePlatformCameraPreviewView nativeView) { base.ConnectHandler(nativeView); - - if (await cameraManager.CheckPermissions()) - cameraManager.Connect(); - - cameraManager.FrameReady += CameraManager_FrameReady; + + if (cameraManager != null) + { + if (await cameraManager.CheckPermissions()) + cameraManager.Connect(); + + cameraManager.FrameReady += CameraManager_FrameReady; + } } void CameraManager_FrameReady(object sender, CameraFrameBufferEventArgs e) @@ -56,9 +59,12 @@ void CameraManager_FrameReady(object sender, CameraFrameBufferEventArgs e) protected override void DisconnectHandler(NativePlatformCameraPreviewView nativeView) { - cameraManager.FrameReady -= CameraManager_FrameReady; + if (cameraManager != null) + { + cameraManager.FrameReady -= CameraManager_FrameReady; - cameraManager.Disconnect(); + cameraManager.Disconnect(); + } base.DisconnectHandler(nativeView); }