diff --git a/BarcodeScanning.Native.Maui/BarcodeScanning.Native.Maui.csproj b/BarcodeScanning.Native.Maui/BarcodeScanning.Native.Maui.csproj index 1a1c5a1..cd22414 100644 --- a/BarcodeScanning.Native.Maui/BarcodeScanning.Native.Maui.csproj +++ b/BarcodeScanning.Native.Maui/BarcodeScanning.Native.Maui.csproj @@ -8,7 +8,7 @@ true enable true - 1.5.6 + 1.5.7 Alen Friščić MIT README.md @@ -49,12 +49,17 @@ - - - + + + - - + + + + + + + diff --git a/BarcodeScanning.Native.Maui/Platform/Android/CameraManager.cs b/BarcodeScanning.Native.Maui/Platform/Android/CameraManager.cs index 6ca21af..169ecb3 100644 --- a/BarcodeScanning.Native.Maui/Platform/Android/CameraManager.cs +++ b/BarcodeScanning.Native.Maui/Platform/Android/CameraManager.cs @@ -8,6 +8,7 @@ using AndroidX.Lifecycle; using Java.Util.Concurrent; using Microsoft.Maui.Graphics.Platform; +using Microsoft.Maui.Platform; using Xamarin.Google.MLKit.Vision.BarCode; using Xamarin.Google.MLKit.Vision.Common; @@ -63,6 +64,7 @@ internal CameraManager(CameraView cameraView, Context context) Controller = _cameraController, LayoutParameters = new RelativeLayout.LayoutParams(LayoutParams.MatchParent, LayoutParams.MatchParent) }; + _previewView.SetBackgroundColor(_cameraView?.BackgroundColor?.ToPlatform() ?? Color.Transparent); _previewView.SetImplementationMode(PreviewView.ImplementationMode.Compatible); _previewView.SetScaleType(PreviewView.ScaleType.FillCenter); @@ -123,7 +125,7 @@ internal void Start() UpdateOutput(); UpdateAnalyzer(); UpdateTorch(); - + _cameraController.BindToLifecycle(lifecycleOwner); _cameraRunning = true; } @@ -238,7 +240,7 @@ internal async Task PerformBarcodeDetection(IImageProxy proxy) using var coordinateTransform = new CoordinateTransform(source, target); using var image = InputImage.FromMediaImage(proxy.Image, proxy.ImageInfo.RotationDegrees); - using var results = await _barcodeScanner.Process(image); + using var results = await _barcodeScanner.Process(image).AsAsync(); Methods.ProcessBarcodeResult(results, _barcodeResults, coordinateTransform); @@ -246,7 +248,7 @@ internal async Task PerformBarcodeDetection(IImageProxy proxy) { Methods.InvertLuminance(proxy.Image); using var invertedimage = InputImage.FromMediaImage(proxy.Image, proxy.ImageInfo.RotationDegrees); - using var invertedresults = await _barcodeScanner.Process(invertedimage); + using var invertedresults = await _barcodeScanner.Process(invertedimage).AsAsync(); Methods.ProcessBarcodeResult(invertedresults, _barcodeResults, coordinateTransform); } diff --git a/BarcodeScanning.Native.Maui/Platform/Android/Methods.cs b/BarcodeScanning.Native.Maui/Platform/Android/Methods.cs index a44afae..3c36c7f 100644 --- a/BarcodeScanning.Native.Maui/Platform/Android/Methods.cs +++ b/BarcodeScanning.Native.Maui/Platform/Android/Methods.cs @@ -5,6 +5,8 @@ using Java.Net; using Java.Util; using Microsoft.Maui.Graphics.Platform; +using System.Runtime.Intrinsics; +using System.Runtime.Intrinsics.Arm; using Xamarin.Google.MLKit.Vision.Barcode.Common; using Xamarin.Google.MLKit.Vision.BarCode; using Xamarin.Google.MLKit.Vision.Common; @@ -42,7 +44,8 @@ private static async Task> ProcessBitmapAsync(Bitmap bitm .Build()); using var image = InputImage.FromBitmap(bitmap, 0); - ProcessBarcodeResult(await scanner.Process(image), barcodeResults); + using var results = await scanner.Process(image).AsAsync(); + ProcessBarcodeResult(results, barcodeResults); using var invertedBitmap = Bitmap.CreateBitmap(bitmap.Height, bitmap.Width, Bitmap.Config.Argb8888); using var canvas = new Canvas(invertedBitmap); @@ -62,16 +65,15 @@ private static async Task> ProcessBitmapAsync(Bitmap bitm canvas.DrawBitmap(bitmap, 0, 0, paint); using var invertedImage = InputImage.FromBitmap(invertedBitmap, 0); - ProcessBarcodeResult(await scanner.Process(invertedImage), barcodeResults); + using var invertedResults = await scanner.Process(invertedImage).AsAsync(); + ProcessBarcodeResult(invertedResults, barcodeResults); return barcodeResults; } internal static void ProcessBarcodeResult(Java.Lang.Object inputResults, HashSet outputResults, CoordinateTransform transform = null) { - if (inputResults is null) - return; - using var javaList = inputResults.JavaCast(); + using var javaList = inputResults?.JavaCast(); if (javaList?.IsEmpty ?? true) return; @@ -103,12 +105,30 @@ internal static void ProcessBarcodeResult(Java.Lang.Object inputResults, HashSet internal static void InvertLuminance(Image image) { var yBuffer = image.GetPlanes()[0].Buffer; + if (yBuffer.IsDirect) { unsafe { - ulong* data = (ulong*)yBuffer.GetDirectBufferAddress(); - Parallel.For(0, yBuffer.Capacity() / sizeof(ulong), parallelOptions, (i) => data[i] = ~data[i]); + if (AdvSimd.IsSupported && Vector128.IsHardwareAccelerated) + { + var data = (byte*)yBuffer.GetDirectBufferAddress(); + var length = yBuffer.Capacity(); + + for (int i = 0; i < length; i += Vector128.Count) + { + var vector = AdvSimd.LoadVector128(data + i); + var inverted = AdvSimd.Not(vector); + AdvSimd.Store(data + i, inverted); + } + } + else + { + var data = (ulong*)yBuffer.GetDirectBufferAddress(); + var length = yBuffer.Capacity() >> 3; + + Parallel.For(0, length, parallelOptions, (i) => data[i] = ~data[i]); + } } } else diff --git a/BarcodeScanning.Native.Maui/Platform/MaciOS/BarcodeView.cs b/BarcodeScanning.Native.Maui/Platform/MaciOS/BarcodeView.cs index 5118b5b..d2687c8 100644 --- a/BarcodeScanning.Native.Maui/Platform/MaciOS/BarcodeView.cs +++ b/BarcodeScanning.Native.Maui/Platform/MaciOS/BarcodeView.cs @@ -21,24 +21,25 @@ public override void LayoutSubviews() base.LayoutSubviews(); var layer = this.Layer; - var window = this.Window; - if (window is null || layer is null) { - return; - } - if (_previewLayer is not null) - _previewLayer.Frame = layer.Bounds; - if (_shapeLayer is not null) - _shapeLayer.Position = new CGPoint(layer.Bounds.Width / 2, layer.Bounds.Height / 2); + if (layer is not null) + { + if (_shapeLayer is not null) + _shapeLayer.Position = new CGPoint(layer.Bounds.Width / 2, layer.Bounds.Height / 2); - var previewLayerConnection = _previewLayer?.Connection; - if (previewLayerConnection is not null && previewLayerConnection.SupportsVideoOrientation) - previewLayerConnection.VideoOrientation = window.WindowScene?.InterfaceOrientation switch + if (_previewLayer is not null) { - UIInterfaceOrientation.LandscapeLeft => AVCaptureVideoOrientation.LandscapeLeft, - UIInterfaceOrientation.LandscapeRight => AVCaptureVideoOrientation.LandscapeRight, - UIInterfaceOrientation.PortraitUpsideDown => AVCaptureVideoOrientation.PortraitUpsideDown, - _ => AVCaptureVideoOrientation.Portrait - }; + _previewLayer.Frame = layer.Bounds; + + if (_previewLayer.Connection is not null && _previewLayer.Connection.SupportsVideoOrientation) + _previewLayer.Connection.VideoOrientation = this.Window?.WindowScene?.InterfaceOrientation switch + { + UIInterfaceOrientation.LandscapeLeft => AVCaptureVideoOrientation.LandscapeLeft, + UIInterfaceOrientation.LandscapeRight => AVCaptureVideoOrientation.LandscapeRight, + UIInterfaceOrientation.PortraitUpsideDown => AVCaptureVideoOrientation.PortraitUpsideDown, + _ => AVCaptureVideoOrientation.Portrait + }; + } + } } } \ No newline at end of file diff --git a/BarcodeScanning.Native.Maui/Platform/MaciOS/CameraManager.cs b/BarcodeScanning.Native.Maui/Platform/MaciOS/CameraManager.cs index d9a080d..018f4e3 100644 --- a/BarcodeScanning.Native.Maui/Platform/MaciOS/CameraManager.cs +++ b/BarcodeScanning.Native.Maui/Platform/MaciOS/CameraManager.cs @@ -6,6 +6,7 @@ using CoreMedia; using Foundation; using Microsoft.Maui.Graphics.Platform; +using Microsoft.Maui.Platform; using System.Diagnostics; using UIKit; using Vision; @@ -65,7 +66,8 @@ internal CameraManager(CameraView cameraView) _previewLayer = new AVCaptureVideoPreviewLayer(_captureSession) { - VideoGravity = AVLayerVideoGravity.ResizeAspectFill, + BackgroundColor = _cameraView?.BackgroundColor?.ToPlatform().CGColor, + VideoGravity = AVLayerVideoGravity.ResizeAspectFill }; _shapeLayer = new CAShapeLayer() { diff --git a/BarcodeScanning.Native.Maui/Platform/MaciOS/Methods.cs b/BarcodeScanning.Native.Maui/Platform/MaciOS/Methods.cs index da36b2b..9094f7a 100644 --- a/BarcodeScanning.Native.Maui/Platform/MaciOS/Methods.cs +++ b/BarcodeScanning.Native.Maui/Platform/MaciOS/Methods.cs @@ -43,6 +43,7 @@ internal static void ProcessBarcodeResult(VNBarcodeObservation[] inputResults, H lock (outputResults) { + //TODO NSProcessInfo.ProcessInfo.OperatingSystemVersion.Major > 17 and add payloadData foreach (var barcode in inputResults) { outputResults.Add(new BarcodeResult()