Skip to content

Commit

Permalink
2.1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
afriscic committed Dec 11, 2024
1 parent fca72c2 commit d599940
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<IsAotCompatible>true</IsAotCompatible>
<Nullable>enable</Nullable>
<Version>2.1.2</Version>
<Version>2.1.3</Version>
<Authors>Alen Friščić</Authors>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand Down
32 changes: 15 additions & 17 deletions BarcodeScanning.Native.Maui/Platform/Android/BarcodeAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ internal class BarcodeAnalyzer : Java.Lang.Object, ImageAnalysis.IAnalyzer
private CoordinateTransform? _coordinateTransform;

private bool _updateCoordinateTransform = false;
private int _transformDegrees = 0;
private Point _previewViewCenter = new();
private Rect _previewViewRect = new();

Expand Down Expand Up @@ -62,36 +61,28 @@ public void Analyze(IImageProxy proxy)
if (_cameraManager.CameraView.PauseScanning)
return;

if (_cameraManager.CameraView.CaptureNextFrame)
{
_cameraManager.CameraView.CaptureNextFrame = false;
var image = new PlatformImage(proxy.ToBitmap());
_cameraManager.CameraView.TriggerOnImageCaptured(image);
}

if (_updateCoordinateTransform || _transformDegrees != proxy.ImageInfo.RotationDegrees)
if (_updateCoordinateTransform)
{
_coordinateTransform?.Dispose();
_coordinateTransform = _cameraManager.GetCoordinateTransform(proxy);
_transformDegrees = proxy.ImageInfo.RotationDegrees;

_previewViewCenter.X = _cameraManager.PreviewView.Width / 2;
_previewViewCenter.Y = _cameraManager.PreviewView.Height / 2;
_previewViewCenter.X = _cameraManager.PreviewView.Width * 0.5;
_previewViewCenter.Y = _cameraManager.PreviewView.Height * 0.5;
_previewViewRect.Width = _cameraManager.PreviewView.Width;
_previewViewRect.Height = _cameraManager.PreviewView.Height;

_updateCoordinateTransform = false;
}

using var inputImage = InputImage.FromMediaImage(proxy.Image, proxy.ImageInfo.RotationDegrees);
using var inputImage = InputImage.FromMediaImage(proxy.Image, 0);
using var task = _barcodeScanner.Process(inputImage);
var result = TasksClass.Await(task);

Java.Lang.Object? invertedResult = null;
if (_cameraManager.CameraView.ForceInverted)
{
Methods.InvertLuminance(proxy.Image);
using var invertedImage = InputImage.FromMediaImage(proxy.Image, proxy.ImageInfo.RotationDegrees);
using var invertedImage = InputImage.FromMediaImage(proxy.Image, 0);
using var invertedTask = _barcodeScanner.Process(invertedImage);
invertedResult = TasksClass.Await(invertedTask);
}
Expand All @@ -101,12 +92,19 @@ public void Analyze(IImageProxy proxy)
_barcodeResults.Clear();
AddResultToSet(result);
AddResultToSet(invertedResult);
}

_cameraManager.CameraView.DetectionFinished(_barcodeResults, _resultsLock);

result?.Dispose();
invertedResult?.Dispose();
if (_cameraManager.CameraView.CaptureNextFrame && _barcodeResults.Count > 0)
{
MainThread.BeginInvokeOnMainThread(() =>_cameraManager.CameraView.CaptureNextFrame = false);
var image = new PlatformImage(proxy.ToBitmap());
_cameraManager.CameraView.TriggerOnImageCaptured(image);
}

_cameraManager?.CameraView?.DetectionFinished(_barcodeResults, _resultsLock);
result?.Dispose();
invertedResult?.Dispose();
}
catch (Exception ex)
{
Expand Down
20 changes: 12 additions & 8 deletions BarcodeScanning.Native.Maui/Platform/Android/CameraManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ internal CameraManager(CameraView cameraView, Context context)

_previewView = new PreviewView(_context)
{
Controller = _cameraController,
LayoutParameters = new RelativeLayout.LayoutParams(LayoutParams.MatchParent, LayoutParams.MatchParent)
};
_previewView.SetBackgroundColor(_cameraView?.BackgroundColor?.ToPlatform() ?? Color.Transparent);
Expand Down Expand Up @@ -120,10 +119,13 @@ internal CameraManager(CameraView cameraView, Context context)
DeviceDisplay.Current.MainDisplayInfoChanged += MainDisplayInfoChanged;
}

internal void Start()
internal async void Start()
{
if (_cameraController is not null)
{
if (_previewView is not null)
_previewView.Controller = null;

if (OpenedCameraState?.GetType() != CameraState.Type.Closed)
_cameraController.Unbind();

Expand All @@ -138,6 +140,12 @@ internal void Start()

if (_lifecycleOwner is not null)
_cameraController.BindToLifecycle(_lifecycleOwner);

if (_previewView is not null)
{
await Task.Delay(100);
_previewView.Controller = _cameraController;
}
}
}

Expand Down Expand Up @@ -227,11 +235,7 @@ internal void UpdateZoomFactor()

internal CoordinateTransform? GetCoordinateTransform(IImageProxy proxy)
{
var imageOutputTransform = new ImageProxyTransformFactory
{
UsingRotationDegrees = true
}
.GetOutputTransform(proxy);
var imageOutputTransform = new ImageProxyTransformFactory().GetOutputTransform(proxy);
var previewOutputTransform = MainThread.InvokeOnMainThreadAsync(() => _previewView?.OutputTransform).GetAwaiter().GetResult();

if (imageOutputTransform is not null && previewOutputTransform is not null)
Expand All @@ -242,7 +246,7 @@ internal void UpdateZoomFactor()

private async void MainDisplayInfoChanged(object? sender, DisplayInfoChangedEventArgs e)
{
if (_previewView is not null)
if (_previewView is not null && OpenedCameraState?.GetType() == CameraState.Type.Open)
{
_previewView.Controller = null;
await Task.Delay(100);
Expand Down
8 changes: 1 addition & 7 deletions BarcodeScanning.Native.Maui/Platform/Android/Methods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,7 @@ internal static void InvertLuminance(Image image)
var data = yBuffer.GetDirectBufferAddress();
var length = yBuffer.Capacity();

int result;
if (neonSupported)
result = InvertBytes(data, length);
else
result = -1;

if (result != 0)
if (!neonSupported || InvertBytes(data, length) != 0)
{
unsafe
{
Expand Down
34 changes: 17 additions & 17 deletions BarcodeScanning.Native.Maui/Platform/MaciOS/BarcodeAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,6 @@ public override void DidOutputSampleBuffer(AVCaptureOutput captureOutput, CMSamp
if (_cameraManager.CameraView.PauseScanning)
return;

if (_cameraManager.CameraView.CaptureNextFrame)
{
_cameraManager.CameraView.CaptureNextFrame = false;
using var imageBuffer = sampleBuffer.GetImageBuffer();
if (imageBuffer is not null)
{
using var cIImage = new CIImage(imageBuffer);
using var cIContext = new CIContext();
using var cGImage = cIContext.CreateCGImage(cIImage, cIImage.Extent);
if (cGImage is not null)
{
var image = new PlatformImage(new UIImage(cGImage));
_cameraManager.CameraView.TriggerOnImageCaptured(image);
}
}
}

_sequenceRequestHandler.Perform([_detectBarcodesRequest], sampleBuffer, out _);

lock (_resultsLock)
Expand Down Expand Up @@ -106,6 +89,23 @@ public override void DidOutputSampleBuffer(AVCaptureOutput captureOutput, CMSamp
}

_cameraManager.CameraView.DetectionFinished(_barcodeResults, _resultsLock);

if (_cameraManager.CameraView.CaptureNextFrame && _barcodeResults.Count > 0)
{
MainThread.BeginInvokeOnMainThread(() =>_cameraManager.CameraView.CaptureNextFrame = false);
using var imageBuffer = sampleBuffer.GetImageBuffer();
if (imageBuffer is not null)
{
using var cIImage = new CIImage(imageBuffer);
using var cIContext = new CIContext();
using var cGImage = cIContext.CreateCGImage(cIImage, cIImage.Extent);
if (cGImage is not null)
{
var image = new PlatformImage(new UIImage(cGImage));
_cameraManager.CameraView.TriggerOnImageCaptured(image);
}
}
}
}
catch (Exception ex)
{
Expand Down

0 comments on commit d599940

Please sign in to comment.