Skip to content

Commit

Permalink
1.4.2
Browse files Browse the repository at this point in the history
  • Loading branch information
afriscic committed Apr 4, 2024
1 parent 26b5ed6 commit 3f1c9cb
Show file tree
Hide file tree
Showing 16 changed files with 859 additions and 825 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<SingleProject>true</SingleProject>
<ImplicitUsings>enable</ImplicitUsings>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Version>1.4.1</Version>
<Version>1.4.2</Version>
<Authors>Alen Friščić</Authors>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand Down Expand Up @@ -40,7 +40,7 @@
</ItemGroup>

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

Expand Down
20 changes: 11 additions & 9 deletions BarcodeScanning.Native.Maui/CameraViewHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ namespace BarcodeScanning;

public partial class CameraViewHandler : ViewHandler<CameraView, BarcodeView>
{
private CameraManager _cameraManager = null;

public static readonly PropertyMapper<CameraView, CameraViewHandler> CameraViewMapper = new()
{
[nameof(CameraView.CameraFacing)] = (handler, virtualView) => handler.PlatformView?.UpdateCamera(),
[nameof(CameraView.CaptureQuality)] = (handler, virtualView) => handler.PlatformView?.UpdateResolution(),
[nameof(CameraView.BarcodeSymbologies)] = (handler, virtualView) => handler.PlatformView?.UpdateAnalyzer(),
[nameof(CameraView.TorchOn)] = (handler, virtualView) => handler.PlatformView?.UpdateTorch(),
[nameof(CameraView.CameraEnabled)] = (handler, virtualView) => handler.PlatformView?.HandleCameraEnabled(),
[nameof(CameraView.AimMode)] = (handler, virtualView) => handler.PlatformView?.HandleAimMode(),
[nameof(CameraView.TapToFocusEnabled)] = (handler, virtualView) => handler.PlatformView?.HandleTapToFocus(),
[nameof(CameraView.RequestZoomFactor)] = (handler, virtualView) => handler.PlatformView?.UpdateZoomFactor()
[nameof(CameraView.CameraFacing)] = (handler, virtualView) => handler._cameraManager?.UpdateCamera(),
[nameof(CameraView.CaptureQuality)] = (handler, virtualView) => handler._cameraManager?.UpdateResolution(),
[nameof(CameraView.BarcodeSymbologies)] = (handler, virtualView) => handler._cameraManager?.UpdateAnalyzer(),
[nameof(CameraView.TorchOn)] = (handler, virtualView) => handler._cameraManager?.UpdateTorch(),
[nameof(CameraView.CameraEnabled)] = (handler, virtualView) => handler._cameraManager?.HandleCameraEnabled(),
[nameof(CameraView.AimMode)] = (handler, virtualView) => handler._cameraManager?.HandleAimMode(),
[nameof(CameraView.TapToFocusEnabled)] = (handler, virtualView) => handler._cameraManager?.HandleTapToFocus(),
[nameof(CameraView.RequestZoomFactor)] = (handler, virtualView) => handler._cameraManager?.UpdateZoomFactor()
};

public static readonly CommandMapper<CameraView, CameraViewHandler> CameraCommandMapper = new()
Expand All @@ -26,7 +28,7 @@ public CameraViewHandler() : base(CameraViewMapper, CameraCommandMapper)

protected override void DisconnectHandler(BarcodeView barcodeView)
{
barcodeView.Dispose();
_cameraManager.Dispose();
base.DisconnectHandler(barcodeView);
}
}
92 changes: 7 additions & 85 deletions BarcodeScanning.Native.Maui/Platform/Android/BarcodeAnalyzer.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using Android.Gms.Extensions;
using AndroidX.Camera.Core;
using AndroidX.Camera.View.Transform;
using Xamarin.Google.MLKit.Vision.BarCode;
using Xamarin.Google.MLKit.Vision.Common;
using AndroidX.Camera.Core;
using Size = Android.Util.Size;

namespace BarcodeScanning;
Expand All @@ -12,76 +8,19 @@ internal class BarcodeAnalyzer : Java.Lang.Object, ImageAnalysis.IAnalyzer
public Size DefaultTargetResolution => Methods.TargetResolution(CaptureQuality.Medium);
public int TargetCoordinateSystem => ImageAnalysis.CoordinateSystemOriginal;

private readonly HashSet<BarcodeResult> _barcodeResults;
private readonly BarcodeView _barcodeView;
private readonly CameraView _cameraView;
private readonly CameraManager _cameraManager;

private IBarcodeScanner _barcodeScanner;

internal BarcodeAnalyzer(CameraView cameraView, BarcodeView barcodeView)
internal BarcodeAnalyzer(CameraManager cameraManager)
{
_cameraView = cameraView;
_barcodeView = barcodeView;

_barcodeResults = [];
_cameraManager = cameraManager;
}

public async void Analyze(IImageProxy proxy)
{
try
{
_barcodeScanner ??= Xamarin.Google.MLKit.Vision.BarCode.BarcodeScanning.GetClient(new BarcodeScannerOptions.Builder()
.SetBarcodeFormats(Methods.ConvertBarcodeFormats(_cameraView.BarcodeSymbologies))
.Build());

if (proxy is null || _cameraView.PauseScanning)
return;

using var target = await MainThread.InvokeOnMainThreadAsync(() => _barcodeView.PreviewView.OutputTransform);
using var source = new ImageProxyTransformFactory
{
UsingRotationDegrees = true
}
.GetOutputTransform(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);

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);

Methods.ProcessBarcodeResult(invertedresults, _barcodeResults, coordinateTransform);
}

if (_cameraView.AimMode)
{
var previewCenter = new Point(_barcodeView.PreviewView.Width / 2, _barcodeView.PreviewView.Height / 2);

foreach (var barcode in _barcodeResults)
{
if (!barcode.BoundingBox.Contains(previewCenter))
_barcodeResults.Remove(barcode);
}
}

if (_cameraView.ViewfinderMode)
{
var previewRect = new RectF(0, 0, _barcodeView.PreviewView.Width, _barcodeView.PreviewView.Height);

foreach (var barcode in _barcodeResults)
{
if (!previewRect.Contains(barcode.BoundingBox))
_barcodeResults.Remove(barcode);
}
}

_cameraView?.DetectionFinished(_barcodeResults);
if (proxy is not null)
await _cameraManager.PerformBarcodeDetection(proxy);
}
catch (Exception)
{
Expand All @@ -90,7 +29,6 @@ public async void Analyze(IImageProxy proxy)
{
try
{
_barcodeResults.Clear();
proxy?.Close();
}
catch (Exception)
Expand All @@ -99,7 +37,7 @@ public async void Analyze(IImageProxy proxy)
{
try
{
_barcodeView?.Start();
_cameraManager?.Start();
}
catch (Exception)
{
Expand All @@ -108,20 +46,4 @@ public async void Analyze(IImageProxy proxy)
}
}
}

protected override void Dispose(bool disposing)
{
if (disposing)
{
try
{
_barcodeScanner?.Dispose();
}
catch (Exception)
{
}
}

base.Dispose(disposing);
}
}
Loading

0 comments on commit 3f1c9cb

Please sign in to comment.