Skip to content

Commit

Permalink
1.2.4
Browse files Browse the repository at this point in the history
  • Loading branch information
afriscic committed Jan 24, 2024
1 parent f2f20d1 commit 955a411
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<UseMaui>true</UseMaui>
<SingleProject>true</SingleProject>
<ImplicitUsings>enable</ImplicitUsings>
<Version>1.2.3</Version>
<Version>1.2.4</Version>
<Authors>Alen Friščić</Authors>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand All @@ -28,9 +28,9 @@
</ItemGroup>

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

<PackageReference Include="Xamarin.AndroidX.Collection.Ktx" Version="1.3.0.2" />
</ItemGroup>
Expand Down
35 changes: 12 additions & 23 deletions BarcodeScanning.Native.Maui/Platforms/Android/BarcodeAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ internal class BarcodeAnalyzer : Java.Lang.Object, ImageAnalysis.IAnalyzer

private readonly IBarcodeScanner _barcodeScanner;
private readonly CameraView _cameraView;
private readonly PreviewView _previewView;
private readonly CameraViewHandler _cameraViewHandler;
private readonly PreviewView _previewView;

internal BarcodeAnalyzer(CameraView cameraView, PreviewView previewView, CameraViewHandler cameraViewHandler)
{
_cameraView = cameraView;
_previewView = previewView;
_cameraViewHandler = cameraViewHandler;
_previewView = previewView;

_barcodeScanner = Xamarin.Google.MLKit.Vision.BarCode.BarcodeScanning.GetClient(new BarcodeScannerOptions.Builder()
.SetBarcodeFormats(Methods.ConvertBarcodeFormats(_cameraView.BarcodeSymbologies))
Expand Down Expand Up @@ -82,31 +82,20 @@ public async void Analyze(IImageProxy proxy)

if (_barcodeResults is not null && _cameraView is not null)
_cameraView.DetectionFinished(_barcodeResults);
}
catch (Java.Lang.Exception)
{

}
catch (Exception)
{

}
finally
{
SafeCloseImageProxy(proxy, _cameraViewHandler);
}
}

private static void SafeCloseImageProxy(IImageProxy proxy, CameraViewHandler cameraViewHandler)
{
try
{
proxy?.Close();
}
catch (Exception)
{
MainThread.BeginInvokeOnMainThread(() => cameraViewHandler.Start());
try
{
proxy?.Close();
}
catch (Exception)
{
MainThread.BeginInvokeOnMainThread(_cameraViewHandler.Start);
}
}
}

}
}
}
20 changes: 14 additions & 6 deletions BarcodeScanning.Native.Maui/Platforms/Android/CameraViewHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public partial class CameraViewHandler
private PreviewView _previewView;

private readonly int _delay = 200;
private bool _cameraStarted = false;
private bool _cameraRunning = false;

protected override BarcodeView CreatePlatformView()
{
Expand All @@ -40,11 +40,12 @@ protected override BarcodeView CreatePlatformView()
return _barcodeView;
}

public void Start()
{
internal void Start()
{
if (_cameraController is not null)
{
_cameraController.Unbind();
_cameraRunning = false;

ILifecycleOwner lifecycleOwner = null;
if (Context is ILifecycleOwner)
Expand All @@ -57,10 +58,15 @@ public void Start()
if (lifecycleOwner is null)
return;

if (_cameraController.CameraSelector is null)
UpdateCamera();
if (_cameraController.ImageAnalysisTargetSize is null)
UpdateResolution();

UpdateAnalyzer();

_cameraController.BindToLifecycle(lifecycleOwner);
_cameraStarted = true;
_cameraRunning = true;
}
}

Expand All @@ -70,7 +76,7 @@ private void Stop()
{
_cameraController.EnableTorch(false);
_cameraController.Unbind();
_cameraStarted = false;
_cameraRunning = false;
}
}

Expand Down Expand Up @@ -117,12 +123,14 @@ private void UpdateCamera()

}

//TODO Implement setImageAnalysisResolutionSelector
//https://developer.android.com/reference/androidx/camera/view/CameraController#setImageAnalysisResolutionSelector(androidx.camera.core.resolutionselector.ResolutionSelector)
private void UpdateResolution()
{
if (_cameraController is not null)
_cameraController.ImageAnalysisTargetSize = new CameraController.OutputSize(Methods.TargetResolution(VirtualView?.CaptureQuality));

if (_cameraStarted)
if (_cameraRunning)
Start();
}

Expand Down
29 changes: 13 additions & 16 deletions BarcodeScanning.Native.Maui/Platforms/iOS/BarcodeAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,20 @@ internal class BarcodeAnalyzer : AVCaptureVideoDataOutputSampleBufferDelegate
{
private readonly AVCaptureVideoPreviewLayer _previewLayer;
private readonly CameraView _cameraView;
private readonly CameraViewHandler _cameraViewHandler;
private readonly VNDetectBarcodesRequest _barcodeRequest;
private readonly VNSequenceRequestHandler _sequenceRequestHandler;

private HashSet<BarcodeResult> _barcodeResults;

internal BarcodeAnalyzer(CameraView cameraView, AVCaptureVideoPreviewLayer previewLayer)
internal BarcodeAnalyzer(CameraView cameraView, AVCaptureVideoPreviewLayer previewLayer, CameraViewHandler cameraViewHandler)
{
_cameraView = cameraView;
_cameraViewHandler = cameraViewHandler;
_previewLayer = previewLayer;
_sequenceRequestHandler = new VNSequenceRequestHandler();
_barcodeRequest = new VNDetectBarcodesRequest((request, error) => {
_barcodeRequest = new VNDetectBarcodesRequest((request, error) =>
{
if (error is null)
_barcodeResults = Methods.ProcessBarcodeResult(request.GetResults<VNBarcodeObservation>(), _previewLayer);
});
Expand Down Expand Up @@ -64,23 +67,17 @@ public override void DidOutputSampleBuffer(AVCaptureOutput captureOutput, CMSamp
}
catch (Exception)
{

}
finally
{
SafeCloseSampleBuffer(sampleBuffer);
}
}

private static void SafeCloseSampleBuffer(CMSampleBuffer buffer)
{
try
{
buffer?.Dispose();
}
catch (Exception)
{

try
{
sampleBuffer?.Dispose();
}
catch (Exception)
{
MainThread.BeginInvokeOnMainThread(_cameraViewHandler.Start);
}
}
}
}
Loading

0 comments on commit 955a411

Please sign in to comment.