Skip to content

Commit

Permalink
2.1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
afriscic committed Jan 12, 2025
1 parent d599940 commit d259051
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 63 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.3</Version>
<Version>2.1.4</Version>
<Authors>Alen Friščić</Authors>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand Down Expand Up @@ -51,8 +51,8 @@

<ItemGroup Condition="'$(TargetFramework)' == 'net9.0-android'">
<PackageReference Include="Xamarin.Google.MLKit.BarcodeScanning" Version="117.3.0.1" />
<PackageReference Include="Xamarin.AndroidX.Camera.View" Version="1.4.0.2" />
<PackageReference Include="Xamarin.AndroidX.Camera.Camera2" Version="1.4.0.2" />
<PackageReference Include="Xamarin.AndroidX.Camera.View" Version="1.4.1" />
<PackageReference Include="Xamarin.AndroidX.Camera.Camera2" Version="1.4.1" />

<PackageReference Include="Xamarin.AndroidX.Activity.Ktx" Version="1.9.3.1" />
<PackageReference Include="Xamarin.AndroidX.Collection.Ktx" Version="1.4.5.1" />
Expand Down
32 changes: 25 additions & 7 deletions BarcodeScanning.Native.Maui/Platform/MaciOS/BarcodeView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,33 @@ public override void LayoutSubviews()
_previewLayer.Frame = layer.Bounds;

var connection = _previewLayer.Connection;
if (connection is not null && connection.SupportsVideoOrientation)
if (connection is not null)
{
connection.VideoOrientation = this.Window?.WindowScene?.InterfaceOrientation switch
/*if (OperatingSystem.IsIOSVersionAtLeast(17))
{
UIInterfaceOrientation.LandscapeLeft => AVCaptureVideoOrientation.LandscapeLeft,
UIInterfaceOrientation.LandscapeRight => AVCaptureVideoOrientation.LandscapeRight,
UIInterfaceOrientation.PortraitUpsideDown => AVCaptureVideoOrientation.PortraitUpsideDown,
_ => AVCaptureVideoOrientation.Portrait
};
var angle = this.Window?.WindowScene?.InterfaceOrientation switch
{
UIInterfaceOrientation.LandscapeLeft => 0,
UIInterfaceOrientation.LandscapeRight => 180,
UIInterfaceOrientation.PortraitUpsideDown => 270,
_ => 90
};
if (connection.IsVideoRotationAngleSupported(angle))
connection.VideoRotationAngle = angle;
}
else
{*/
if (connection.SupportsVideoOrientation)
{
connection.VideoOrientation = this.Window?.WindowScene?.InterfaceOrientation switch
{
UIInterfaceOrientation.LandscapeLeft => AVCaptureVideoOrientation.LandscapeLeft,
UIInterfaceOrientation.LandscapeRight => AVCaptureVideoOrientation.LandscapeRight,
UIInterfaceOrientation.PortraitUpsideDown => AVCaptureVideoOrientation.PortraitUpsideDown,
_ => AVCaptureVideoOrientation.Portrait
};
}
//}
}
}
}
Expand Down
112 changes: 59 additions & 53 deletions BarcodeScanning.Native.Maui/Platform/MaciOS/CameraManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,29 +75,25 @@ internal CameraManager(CameraView cameraView)
internal void Start()
{
if (_captureSession is not null)
{
if (_captureSession.Running)
_dispatchQueue.DispatchAsync(_captureSession.StopRunning);

{
UpdateCamera();
UpdateResolution();

if (!_captureSession.Outputs.Contains(_videoDataOutput) && _captureSession.CanAddOutput(_videoDataOutput))
{
_dispatchQueue.DispatchAsync(() =>
{
_captureSession.BeginConfiguration();
_captureSession.AddOutput(_videoDataOutput);
_captureSession.CommitConfiguration();
});
}
_dispatchQueue.DispatchBarrierAsync(() =>
{
if (_captureSession.Running)
_captureSession.StopRunning();

_dispatchQueue.DispatchAsync(() =>
{
_captureSession.StartRunning();

if (_videoDataOutput is not null)
{
if (!_captureSession.Outputs.Contains(_videoDataOutput) && _captureSession.CanAddOutput(_videoDataOutput))
{
_captureSession.BeginConfiguration();
_captureSession.AddOutput(_videoDataOutput);
_captureSession.CommitConfiguration();
}

_videoDataOutput.SetSampleBufferDelegate(null, null);
_videoDataOutput.SetSampleBufferDelegate(_barcodeAnalyzer, DispatchQueue.DefaultGlobalQueue);
}
Expand All @@ -117,7 +113,7 @@ internal void Stop()
DeviceLock(() => _captureDevice.TorchMode = AVCaptureTorchMode.Off);

if (_captureSession.Running)
_dispatchQueue.DispatchAsync(_captureSession.StopRunning);
_dispatchQueue.DispatchBarrierAsync(_captureSession.StopRunning);
}
}

Expand All @@ -139,46 +135,52 @@ internal void UpdateCamera()
{
if (_captureSession is not null)
{
_dispatchQueue.DispatchAsync(() =>
AVCaptureDevice? newDevice = null;
if (_cameraView?.CameraFacing == CameraFacing.Front)
{
_captureSession.BeginConfiguration();
newDevice = AVCaptureDevice.GetDefaultDevice(AVCaptureDeviceType.BuiltInWideAngleCamera, AVMediaTypes.Video, AVCaptureDevicePosition.Front);
}
else
{
newDevice = AVCaptureDevice.GetDefaultDevice(AVCaptureDeviceType.BuiltInTripleCamera, AVMediaTypes.Video, AVCaptureDevicePosition.Back);
newDevice ??= AVCaptureDevice.GetDefaultDevice(AVCaptureDeviceType.BuiltInDualWideCamera, AVMediaTypes.Video, AVCaptureDevicePosition.Back);
newDevice ??= AVCaptureDevice.GetDefaultDevice(AVCaptureDeviceType.BuiltInDualCamera, AVMediaTypes.Video, AVCaptureDevicePosition.Back);
newDevice ??= AVCaptureDevice.GetDefaultDevice(AVCaptureDeviceType.BuiltInWideAngleCamera, AVMediaTypes.Video, AVCaptureDevicePosition.Back);
}
newDevice ??= AVCaptureDevice.GetDefaultDevice(AVMediaTypes.Video);

if (_captureInput is not null)
if (_captureDevice != newDevice)
{
_dispatchQueue.DispatchBarrierAsync(() =>
{
if (_captureSession.Inputs.Contains(_captureInput))
_captureSession.RemoveInput(_captureInput);
_captureSession.BeginConfiguration();

_captureInput.Dispose();
}
if (_captureInput is not null)
{
if (_captureSession.Inputs.Contains(_captureInput))
_captureSession.RemoveInput(_captureInput);

_captureDevice?.Dispose();
if (_cameraView?.CameraFacing == CameraFacing.Front)
{
_captureDevice = AVCaptureDevice.GetDefaultDevice(AVCaptureDeviceType.BuiltInWideAngleCamera, AVMediaTypes.Video, AVCaptureDevicePosition.Front);
}
else
{
_captureDevice = AVCaptureDevice.GetDefaultDevice(AVCaptureDeviceType.BuiltInTripleCamera, AVMediaTypes.Video, AVCaptureDevicePosition.Back);
_captureDevice ??= AVCaptureDevice.GetDefaultDevice(AVCaptureDeviceType.BuiltInDualWideCamera, AVMediaTypes.Video, AVCaptureDevicePosition.Back);
_captureDevice ??= AVCaptureDevice.GetDefaultDevice(AVCaptureDeviceType.BuiltInDualCamera, AVMediaTypes.Video, AVCaptureDevicePosition.Back);
_captureDevice ??= AVCaptureDevice.GetDefaultDevice(AVCaptureDeviceType.BuiltInWideAngleCamera, AVMediaTypes.Video, AVCaptureDevicePosition.Back);
}
_captureDevice ??= AVCaptureDevice.GetDefaultDevice(AVMediaTypes.Video);

if (_captureDevice is not null)
{
_captureInput = new AVCaptureDeviceInput(_captureDevice, out _);
_captureInput.Dispose();
}

if (_captureSession.CanAddInput(_captureInput))
_captureSession.AddInput(_captureInput);
}

_captureSession.SessionPreset = Methods.GetBestSupportedPreset(_captureSession, _cameraView?.CaptureQuality ?? CaptureQuality.Medium);
_captureSession.CommitConfiguration();
_captureDevice?.Dispose();
_captureDevice = newDevice;

if (_captureDevice is not null)
{
_captureInput = new AVCaptureDeviceInput(_captureDevice, out _);

if (_captureInput is not null && _captureSession.CanAddInput(_captureInput))
_captureSession.AddInput(_captureInput);
}

_captureSession.SessionPreset = Methods.GetBestSupportedPreset(_captureSession, _cameraView?.CaptureQuality ?? CaptureQuality.Medium);
_captureSession.CommitConfiguration();

UpdateZoomFactor();
ResetFocus();
});
UpdateZoomFactor();
ResetFocus();
});
}
}
}

Expand All @@ -194,7 +196,7 @@ internal void UpdateResolution()
{
if (_captureSession is not null)
{
_dispatchQueue.DispatchAsync(() =>
_dispatchQueue.DispatchBarrierAsync(() =>
{
_captureSession.BeginConfiguration();
_captureSession.SessionPreset = Methods.GetBestSupportedPreset(_captureSession, _cameraView?.CaptureQuality ?? CaptureQuality.Medium);
Expand Down Expand Up @@ -255,7 +257,7 @@ internal void UpdateZoomFactor()

private void DeviceLock(Action action)
{
DispatchQueue.MainQueue.DispatchAsync(() =>
DispatchQueue.MainQueue.DispatchBarrierAsync(() =>
{
if (_captureDevice?.LockForConfiguration(out _) ?? false)
{
Expand Down Expand Up @@ -314,7 +316,11 @@ protected virtual void Dispose(bool disposing)
{
if (disposing)
{
Stop();
if (_captureDevice is not null && _captureDevice.TorchActive)
DeviceLock(() => _captureDevice.TorchMode = AVCaptureTorchMode.Off);

if (_captureSession?.Running ?? false)
_dispatchQueue?.DispatchBarrierSync(_captureSession.StopRunning);

if (_subjectAreaChangedNotificaion is not null)
NSNotificationCenter.DefaultCenter.RemoveObserver(_subjectAreaChangedNotificaion);
Expand Down

0 comments on commit d259051

Please sign in to comment.