Skip to content

Commit

Permalink
Merge pull request #20 from OpterJohan-Hallberg/master
Browse files Browse the repository at this point in the history
Cannot access a disposed object. Object name: 'AndroidX.Camera.Core.SettableImageProxy' workaround
  • Loading branch information
afriscic authored Jan 24, 2024
2 parents 2504c6a + 378ab2a commit f2f20d1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
18 changes: 10 additions & 8 deletions BarcodeScanning.Native.Maui/Platforms/Android/BarcodeAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ internal class BarcodeAnalyzer : Java.Lang.Object, ImageAnalysis.IAnalyzer
private readonly IBarcodeScanner _barcodeScanner;
private readonly CameraView _cameraView;
private readonly PreviewView _previewView;
private readonly CameraViewHandler _cameraViewHandler;

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

_barcodeScanner = Xamarin.Google.MLKit.Vision.BarCode.BarcodeScanning.GetClient(new BarcodeScannerOptions.Builder()
.SetBarcodeFormats(Methods.ConvertBarcodeFormats(_cameraView.BarcodeSymbologies))
Expand All @@ -33,7 +35,7 @@ public async void Analyze(IImageProxy proxy)
{
if (proxy is null || _cameraView.PauseScanning)
return;

var target = await MainThread.InvokeOnMainThreadAsync(() => _previewView.OutputTransform);
var source = new ImageProxyTransformFactory
{
Expand All @@ -55,7 +57,7 @@ public async void Analyze(IImageProxy proxy)

_barcodeResults.UnionWith(Methods.ProcessBarcodeResult(results, coordinateTransform));
}

if (_cameraView.AimMode)
{
var previewCenter = new Point(_previewView.Width / 2, _previewView.Height / 2);
Expand Down Expand Up @@ -91,19 +93,19 @@ public async void Analyze(IImageProxy proxy)
}
finally
{
SafeCloseImageProxy(proxy);
SafeCloseImageProxy(proxy, _cameraViewHandler);
}
}
}

private static void SafeCloseImageProxy(IImageProxy proxy)
private static void SafeCloseImageProxy(IImageProxy proxy, CameraViewHandler cameraViewHandler)
{
try
{
proxy?.Close();
}
catch (Exception)
catch (Exception)
{

MainThread.BeginInvokeOnMainThread(() => cameraViewHandler.Start());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected override BarcodeView CreatePlatformView()
return _barcodeView;
}

private void Start()
public void Start()
{
if (_cameraController is not null)
{
Expand Down Expand Up @@ -100,7 +100,7 @@ private void UpdateAnalyzer()
{
_cameraController.ClearImageAnalysisAnalyzer();
_barcodeAnalyzer?.Dispose();
_barcodeAnalyzer = new BarcodeAnalyzer(VirtualView, _previewView);
_barcodeAnalyzer = new BarcodeAnalyzer(VirtualView, _previewView, this);
_cameraController.SetImageAnalysisAnalyzer(_cameraExecutor, _barcodeAnalyzer);
}
}
Expand Down

0 comments on commit f2f20d1

Please sign in to comment.