Skip to content

Commit

Permalink
1.5.7
Browse files Browse the repository at this point in the history
  • Loading branch information
afriscic committed Jul 11, 2024
1 parent d7c4ad7 commit ed75bf1
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 33 deletions.
17 changes: 11 additions & 6 deletions BarcodeScanning.Native.Maui/BarcodeScanning.Native.Maui.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<SingleProject>true</SingleProject>
<ImplicitUsings>enable</ImplicitUsings>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Version>1.5.6</Version>
<Version>1.5.7</Version>
<Authors>Alen Friščić</Authors>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand Down Expand Up @@ -49,12 +49,17 @@
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net8.0-android'">
<PackageReference Include="Xamarin.Google.MLKit.BarcodeScanning" Version="117.2.0.5" />
<PackageReference Include="Xamarin.AndroidX.Camera.View" Version="1.3.3.2" />
<PackageReference Include="Xamarin.AndroidX.Camera.Camera2" Version="1.3.3.2" />
<PackageReference Include="Xamarin.Google.MLKit.BarcodeScanning" Version="117.2.0.6" />
<PackageReference Include="Xamarin.AndroidX.Camera.View" Version="1.3.4" />
<PackageReference Include="Xamarin.AndroidX.Camera.Camera2" Version="1.3.4" />

<PackageReference Include="Xamarin.AndroidX.Activity.Ktx" Version="1.9.0.2" />
<PackageReference Include="Xamarin.AndroidX.Collection.Ktx" Version="1.4.0.3" />
<PackageReference Include="Xamarin.AndroidX.Activity.Ktx" Version="1.9.0.3" />
<PackageReference Include="Xamarin.AndroidX.Collection.Ktx" Version="1.4.0.4" />
<PackageReference Include="Xamarin.AndroidX.Lifecycle.Process" Version="2.8.2.1" />
<PackageReference Include="Xamarin.AndroidX.Lifecycle.Runtime" Version="2.8.2.1" />
<PackageReference Include="Xamarin.AndroidX.Lifecycle.Runtime.Ktx" Version="2.8.2.1" />
<PackageReference Include="Xamarin.AndroidX.Lifecycle.ViewModel" Version="2.8.2.1" />
<PackageReference Include="Xamarin.AndroidX.Lifecycle.ViewModel.Ktx" Version="2.8.2.1" />
</ItemGroup>

<ItemGroup Condition="$(TargetFramework.StartsWith('net8.0-windows'))">
Expand Down
8 changes: 5 additions & 3 deletions BarcodeScanning.Native.Maui/Platform/Android/CameraManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -123,7 +125,7 @@ internal void Start()
UpdateOutput();
UpdateAnalyzer();
UpdateTorch();

_cameraController.BindToLifecycle(lifecycleOwner);
_cameraRunning = true;
}
Expand Down Expand Up @@ -238,15 +240,15 @@ 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<Java.Lang.Object>();

Methods.ProcessBarcodeResult(results, _barcodeResults, coordinateTransform);

if (_cameraView.ForceInverted)
{
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<Java.Lang.Object>();

Methods.ProcessBarcodeResult(invertedresults, _barcodeResults, coordinateTransform);
}
Expand Down
34 changes: 27 additions & 7 deletions BarcodeScanning.Native.Maui/Platform/Android/Methods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -42,7 +44,8 @@ private static async Task<HashSet<BarcodeResult>> 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<Java.Lang.Object>();
ProcessBarcodeResult(results, barcodeResults);

using var invertedBitmap = Bitmap.CreateBitmap(bitmap.Height, bitmap.Width, Bitmap.Config.Argb8888);
using var canvas = new Canvas(invertedBitmap);
Expand All @@ -62,16 +65,15 @@ private static async Task<HashSet<BarcodeResult>> 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<Java.Lang.Object>();
ProcessBarcodeResult(invertedResults, barcodeResults);

return barcodeResults;
}

internal static void ProcessBarcodeResult(Java.Lang.Object inputResults, HashSet<BarcodeResult> outputResults, CoordinateTransform transform = null)
{
if (inputResults is null)
return;
using var javaList = inputResults.JavaCast<ArrayList>();
using var javaList = inputResults?.JavaCast<ArrayList>();
if (javaList?.IsEmpty ?? true)
return;

Expand Down Expand Up @@ -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<byte>.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
Expand Down
33 changes: 17 additions & 16 deletions BarcodeScanning.Native.Maui/Platform/MaciOS/BarcodeView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
}
}
}
}
4 changes: 3 additions & 1 deletion BarcodeScanning.Native.Maui/Platform/MaciOS/CameraManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using CoreMedia;
using Foundation;
using Microsoft.Maui.Graphics.Platform;
using Microsoft.Maui.Platform;
using System.Diagnostics;
using UIKit;
using Vision;
Expand Down Expand Up @@ -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()
{
Expand Down
1 change: 1 addition & 0 deletions BarcodeScanning.Native.Maui/Platform/MaciOS/Methods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit ed75bf1

Please sign in to comment.