From b8d099bef10d239206e49561f734c2d1fd9435d4 Mon Sep 17 00:00:00 2001 From: afriscic Date: Wed, 13 Mar 2024 19:03:57 +0100 Subject: [PATCH] 1.3.0 --- .../BarcodeScanning.Native.Maui.csproj | 18 +++++- .../Android/BarcodeAnalyzer.cs | 0 .../Android/BarcodeView.cs | 0 .../Android/CameraViewHandler.cs | 0 .../Android/Methods.cs | 24 ++++++-- .../MaciOS}/BarcodeAnalyzer.cs | 0 .../iOS => Platform/MaciOS}/BarcodeView.cs | 0 .../MaciOS}/CameraViewHandler.cs | 61 ++++++++++++------- .../iOS => Platform/MaciOS}/Methods.cs | 6 +- .../{ => Platform}/NET/BarcodeView.cs | 0 .../{ => Platform}/NET/CameraViewHandler.cs | 0 .../{ => Platform}/NET/Methods.cs | 0 .../BarcodeScanning.Test.csproj | 3 +- BarcodeScanning.Test/MainPage.xaml | 2 +- BarcodeScanning.Test/MainPage.xaml.cs | 2 + .../Platforms/MacCatalyst/Info.plist | 2 + README.md | 14 ++--- 17 files changed, 92 insertions(+), 40 deletions(-) rename BarcodeScanning.Native.Maui/{Platforms => Platform}/Android/BarcodeAnalyzer.cs (100%) rename BarcodeScanning.Native.Maui/{Platforms => Platform}/Android/BarcodeView.cs (100%) rename BarcodeScanning.Native.Maui/{Platforms => Platform}/Android/CameraViewHandler.cs (100%) rename BarcodeScanning.Native.Maui/{Platforms => Platform}/Android/Methods.cs (90%) rename BarcodeScanning.Native.Maui/{Platforms/iOS => Platform/MaciOS}/BarcodeAnalyzer.cs (100%) rename BarcodeScanning.Native.Maui/{Platforms/iOS => Platform/MaciOS}/BarcodeView.cs (100%) rename BarcodeScanning.Native.Maui/{Platforms/iOS => Platform/MaciOS}/CameraViewHandler.cs (86%) rename BarcodeScanning.Native.Maui/{Platforms/iOS => Platform/MaciOS}/Methods.cs (96%) rename BarcodeScanning.Native.Maui/{ => Platform}/NET/BarcodeView.cs (100%) rename BarcodeScanning.Native.Maui/{ => Platform}/NET/CameraViewHandler.cs (100%) rename BarcodeScanning.Native.Maui/{ => Platform}/NET/Methods.cs (100%) diff --git a/BarcodeScanning.Native.Maui/BarcodeScanning.Native.Maui.csproj b/BarcodeScanning.Native.Maui/BarcodeScanning.Native.Maui.csproj index 89c7168..981ad1b 100644 --- a/BarcodeScanning.Native.Maui/BarcodeScanning.Native.Maui.csproj +++ b/BarcodeScanning.Native.Maui/BarcodeScanning.Native.Maui.csproj @@ -1,21 +1,33 @@  - net8.0;net8.0-android;net8.0-ios + net8.0;net8.0-android;net8.0-ios;net8.0-maccatalyst true true enable - 1.2.5 + true + 1.3.0 Alen Friščić MIT README.md Barcode scanning library for .NET MAUI https://github.com/afriscic/BarcodeScanning.Native.Maui - .NET MAUI android ios barcode-scanner + .NET MAUI android ios macos barcode-scanner 24.0 + 15.0 15.1 + + + + + + + + + + diff --git a/BarcodeScanning.Native.Maui/Platforms/Android/BarcodeAnalyzer.cs b/BarcodeScanning.Native.Maui/Platform/Android/BarcodeAnalyzer.cs similarity index 100% rename from BarcodeScanning.Native.Maui/Platforms/Android/BarcodeAnalyzer.cs rename to BarcodeScanning.Native.Maui/Platform/Android/BarcodeAnalyzer.cs diff --git a/BarcodeScanning.Native.Maui/Platforms/Android/BarcodeView.cs b/BarcodeScanning.Native.Maui/Platform/Android/BarcodeView.cs similarity index 100% rename from BarcodeScanning.Native.Maui/Platforms/Android/BarcodeView.cs rename to BarcodeScanning.Native.Maui/Platform/Android/BarcodeView.cs diff --git a/BarcodeScanning.Native.Maui/Platforms/Android/CameraViewHandler.cs b/BarcodeScanning.Native.Maui/Platform/Android/CameraViewHandler.cs similarity index 100% rename from BarcodeScanning.Native.Maui/Platforms/Android/CameraViewHandler.cs rename to BarcodeScanning.Native.Maui/Platform/Android/CameraViewHandler.cs diff --git a/BarcodeScanning.Native.Maui/Platforms/Android/Methods.cs b/BarcodeScanning.Native.Maui/Platform/Android/Methods.cs similarity index 90% rename from BarcodeScanning.Native.Maui/Platforms/Android/Methods.cs rename to BarcodeScanning.Native.Maui/Platform/Android/Methods.cs index 7eadc6e..e4dc2cc 100644 --- a/BarcodeScanning.Native.Maui/Platforms/Android/Methods.cs +++ b/BarcodeScanning.Native.Maui/Platform/Android/Methods.cs @@ -15,6 +15,11 @@ namespace BarcodeScanning; public static partial class Methods { + private static readonly ParallelOptions parallelOptions = new() + { + MaxDegreeOfParallelism = Environment.ProcessorCount * 2 + }; + public static async Task> ScanFromImage(byte[] imageArray) => await ProcessBitmap(await BitmapFactory.DecodeByteArrayAsync(imageArray, 0, imageArray.Length)); public static async Task> ScanFromImage(FileResult file) @@ -38,11 +43,22 @@ private static async Task> ProcessBitmap(Bitmap bitmap) internal static void InvertLuminance(Image image) { var yBuffer = image.GetPlanes()[0].Buffer; - using (var bits = BitSet.ValueOf(yBuffer)) + if (yBuffer.IsDirect) { - bits.Flip(0, bits.Length()); - yBuffer.Rewind(); - yBuffer.Put(bits.ToByteArray()); + unsafe + { + ulong* data = (ulong*)yBuffer.GetDirectBufferAddress(); + Parallel.For(0, yBuffer.Capacity() / 8, parallelOptions, (i) => data[i] = ~data[i]); + } + } + else + { + using (var bits = BitSet.ValueOf(yBuffer)) + { + bits.Flip(0, bits.Length()); + yBuffer.Rewind(); + yBuffer.Put(bits.ToByteArray()); + } } } diff --git a/BarcodeScanning.Native.Maui/Platforms/iOS/BarcodeAnalyzer.cs b/BarcodeScanning.Native.Maui/Platform/MaciOS/BarcodeAnalyzer.cs similarity index 100% rename from BarcodeScanning.Native.Maui/Platforms/iOS/BarcodeAnalyzer.cs rename to BarcodeScanning.Native.Maui/Platform/MaciOS/BarcodeAnalyzer.cs diff --git a/BarcodeScanning.Native.Maui/Platforms/iOS/BarcodeView.cs b/BarcodeScanning.Native.Maui/Platform/MaciOS/BarcodeView.cs similarity index 100% rename from BarcodeScanning.Native.Maui/Platforms/iOS/BarcodeView.cs rename to BarcodeScanning.Native.Maui/Platform/MaciOS/BarcodeView.cs diff --git a/BarcodeScanning.Native.Maui/Platforms/iOS/CameraViewHandler.cs b/BarcodeScanning.Native.Maui/Platform/MaciOS/CameraViewHandler.cs similarity index 86% rename from BarcodeScanning.Native.Maui/Platforms/iOS/CameraViewHandler.cs rename to BarcodeScanning.Native.Maui/Platform/MaciOS/CameraViewHandler.cs index 89baa20..a220ef5 100644 --- a/BarcodeScanning.Native.Maui/Platforms/iOS/CameraViewHandler.cs +++ b/BarcodeScanning.Native.Maui/Platform/MaciOS/CameraViewHandler.cs @@ -16,6 +16,7 @@ public partial class CameraViewHandler private BarcodeAnalyzer _barcodeAnalyzer; private BarcodeView _barcodeView; private UITapGestureRecognizer _uITapGestureRecognizer; + private NSObject _subjectAreaChangedNotificaion; private readonly object _syncLock = new(); private readonly object _configLock = new(); @@ -34,7 +35,12 @@ protected override BarcodeView CreatePlatformView() }; _barcodeView = new BarcodeView(_videoPreviewLayer); _barcodeView.AddGestureRecognizer(_uITapGestureRecognizer); - + _subjectAreaChangedNotificaion = NSNotificationCenter.DefaultCenter.AddObserver(AVCaptureDevice.SubjectAreaDidChangeNotification, (n) => + { + if (n.Name == AVCaptureDevice.SubjectAreaDidChangeNotification) + ResetFocus(); + }); + return _barcodeView; } @@ -168,11 +174,7 @@ private void UpdateCamera() if (_captureDevice is not null) { - if (_captureDevice.IsFocusModeSupported(AVCaptureFocusMode.ContinuousAutoFocus)) - CaptureDeviceLock(() => _captureDevice.FocusMode = AVCaptureFocusMode.ContinuousAutoFocus); - else - CaptureDeviceLock(() => _captureDevice.FocusMode = AVCaptureFocusMode.AutoFocus); - + ResetFocus(); _captureInput = new AVCaptureDeviceInput(_captureDevice, out _); if (_captureSession.CanAddInput(_captureInput)) @@ -185,7 +187,7 @@ private void UpdateCamera() UpdateResolution(); } } - + private void UpdateTorch() { if (_captureDevice is not null && _captureDevice.HasTorch && _captureDevice.TorchAvailable) @@ -223,14 +225,25 @@ private void FocusOnTap() CaptureDeviceLock(() => { _captureDevice.FocusPointOfInterest = _videoPreviewLayer.CaptureDevicePointOfInterestForPoint(_uITapGestureRecognizer.LocationInView(_barcodeView)); - if (_captureDevice.IsFocusModeSupported(AVCaptureFocusMode.ContinuousAutoFocus)) - _captureDevice.FocusMode = AVCaptureFocusMode.ContinuousAutoFocus; - else - _captureDevice.FocusMode = AVCaptureFocusMode.AutoFocus; + _captureDevice.FocusMode = AVCaptureFocusMode.AutoFocus; + _captureDevice.SubjectAreaChangeMonitoringEnabled = true; }); } } + private void ResetFocus() + { + CaptureDeviceLock(() => + { + if (_captureDevice.IsFocusModeSupported(AVCaptureFocusMode.ContinuousAutoFocus)) + _captureDevice.FocusMode = AVCaptureFocusMode.ContinuousAutoFocus; + else + _captureDevice.FocusMode = AVCaptureFocusMode.AutoFocus; + + _captureDevice.SubjectAreaChangeMonitoringEnabled = false; + }); + } + private static NSString GetCaptureSessionResolution(CaptureQuality quality) { return quality switch @@ -248,18 +261,20 @@ private void CaptureDeviceLock(Action handler) { lock (_configLock) { - try - { - _captureDevice.LockForConfiguration(out _); - handler(); - } - catch (Exception) - { - - } - finally + if (_captureDevice?.LockForConfiguration(out _) ?? false) { - _captureDevice?.UnlockForConfiguration(); + try + { + handler(); + } + catch (Exception) + { + + } + finally + { + _captureDevice.UnlockForConfiguration(); + } } } }); @@ -273,6 +288,8 @@ private void DisposeView() { this.Stop(); + _subjectAreaChangedNotificaion?.Dispose(); + _subjectAreaChangedNotificaion = null; _barcodeView?.Dispose(); _barcodeView = null; _uITapGestureRecognizer?.Dispose(); diff --git a/BarcodeScanning.Native.Maui/Platforms/iOS/Methods.cs b/BarcodeScanning.Native.Maui/Platform/MaciOS/Methods.cs similarity index 96% rename from BarcodeScanning.Native.Maui/Platforms/iOS/Methods.cs rename to BarcodeScanning.Native.Maui/Platform/MaciOS/Methods.cs index bd44007..ad86dfc 100644 --- a/BarcodeScanning.Native.Maui/Platforms/iOS/Methods.cs +++ b/BarcodeScanning.Native.Maui/Platform/MaciOS/Methods.cs @@ -3,6 +3,7 @@ using CoreImage; using Foundation; using Microsoft.Maui.Platform; +using System.Text; using UIKit; using Vision; @@ -42,14 +43,13 @@ internal static HashSet ProcessBarcodeResult(VNBarcodeObservation foreach (var barcode in result) { - resultList.Add(new BarcodeResult() { BarcodeType = BarcodeTypes.Unknown, BarcodeFormat = ConvertFromIOSFormats(barcode.Symbology), DisplayValue = barcode.PayloadStringValue, RawValue = barcode.PayloadStringValue, - RawBytes = GetRawBytes(barcode) ?? System.Text.Encoding.ASCII.GetBytes(barcode.PayloadStringValue), + RawBytes = GetRawBytes(barcode) ?? Encoding.ASCII.GetBytes(barcode.PayloadStringValue), BoundingBox = previewLayer?.MapToLayerCoordinates(InvertY(barcode.BoundingBox)).ToRectangle() ?? barcode.BoundingBox.ToRectangle() }); }; @@ -107,6 +107,8 @@ internal static VNBarcodeSymbology[] SelectedSymbologies(BarcodeFormats barcodeF symbologiesList.Add(VNBarcodeSymbology.MicroPdf417); if (barcodeFormats.HasFlag(BarcodeFormats.QRCode)) symbologiesList.Add(VNBarcodeSymbology.QR); + if (barcodeFormats.HasFlag(BarcodeFormats.Upca)) + symbologiesList.Add(VNBarcodeSymbology.Ean13); if (barcodeFormats.HasFlag(BarcodeFormats.Upce)) symbologiesList.Add(VNBarcodeSymbology.Upce); diff --git a/BarcodeScanning.Native.Maui/NET/BarcodeView.cs b/BarcodeScanning.Native.Maui/Platform/NET/BarcodeView.cs similarity index 100% rename from BarcodeScanning.Native.Maui/NET/BarcodeView.cs rename to BarcodeScanning.Native.Maui/Platform/NET/BarcodeView.cs diff --git a/BarcodeScanning.Native.Maui/NET/CameraViewHandler.cs b/BarcodeScanning.Native.Maui/Platform/NET/CameraViewHandler.cs similarity index 100% rename from BarcodeScanning.Native.Maui/NET/CameraViewHandler.cs rename to BarcodeScanning.Native.Maui/Platform/NET/CameraViewHandler.cs diff --git a/BarcodeScanning.Native.Maui/NET/Methods.cs b/BarcodeScanning.Native.Maui/Platform/NET/Methods.cs similarity index 100% rename from BarcodeScanning.Native.Maui/NET/Methods.cs rename to BarcodeScanning.Native.Maui/Platform/NET/Methods.cs diff --git a/BarcodeScanning.Test/BarcodeScanning.Test.csproj b/BarcodeScanning.Test/BarcodeScanning.Test.csproj index 094a3e6..604ca4d 100644 --- a/BarcodeScanning.Test/BarcodeScanning.Test.csproj +++ b/BarcodeScanning.Test/BarcodeScanning.Test.csproj @@ -1,7 +1,7 @@  - net8.0-android;net8.0-ios + net8.0-android;net8.0-ios;net8.0-maccatalyst Exe BarcodeScanning.Test true @@ -21,6 +21,7 @@ 1 11.0 + 13.1 21.0 diff --git a/BarcodeScanning.Test/MainPage.xaml b/BarcodeScanning.Test/MainPage.xaml index 3cb0ae9..9774e84 100644 --- a/BarcodeScanning.Test/MainPage.xaml +++ b/BarcodeScanning.Test/MainPage.xaml @@ -21,7 +21,7 @@ InputTransparent="True"/> - +