Skip to content

Commit

Permalink
Merge pull request #23 from Redth/ios-lock-config
Browse files Browse the repository at this point in the history
Fix iOS Scanning and Torch default
  • Loading branch information
Redth authored Aug 7, 2022
2 parents fe68ff0 + 158d6fa commit 67a31c3
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 12 deletions.
66 changes: 56 additions & 10 deletions ZXing.Net.MAUI/Apple/CameraManager.ios.maccatalyst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,49 @@ public void UpdateCamera()

public void Disconnect()
{
captureSession.RemoveOutput(videoDataOutput);
captureSession.StopRunning();
if (captureSession != null)
{
if (captureSession.Running)
captureSession.StopRunning();

captureSession.RemoveOutput(videoDataOutput);

// Cleanup old input
if (captureInput != null && captureSession.Inputs.Length > 0 && captureSession.Inputs.Contains(captureInput))
{
captureSession.RemoveInput(captureInput);
captureInput.Dispose();
captureInput = null;
}

// Cleanup old device
if (captureDevice != null)
{
captureDevice.Dispose();
captureDevice = null;
}
}
}

public void UpdateTorch(bool on)
{
if (captureDevice != null && captureDevice.HasTorch && captureDevice.TorchAvailable)
captureDevice.TorchMode = on ? AVCaptureTorchMode.On : AVCaptureTorchMode.Off;
{
var isOn = captureDevice?.TorchActive ?? false;

try
{
if (on != isOn)
{
CaptureDevicePerformWithLockedConfiguration(() =>
captureDevice.TorchMode = on ? AVCaptureTorchMode.On : AVCaptureTorchMode.Off);
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}

public void Focus(Microsoft.Maui.Graphics.Point point)
Expand All @@ -158,12 +193,25 @@ public void Focus(Microsoft.Maui.Graphics.Point point)
//See if it supports focusing on a point
if (captureDevice.FocusPointOfInterestSupported && !captureDevice.AdjustingFocus)
{
//Lock device to config
if (captureDevice.LockForConfiguration(out var err))
CaptureDevicePerformWithLockedConfiguration(() =>
{
//Focus at the point touched
captureDevice.FocusPointOfInterest = point;
captureDevice.FocusMode = AVCaptureFocusMode.ContinuousAutoFocus;
captureDevice.FocusMode = focusMode;
});
}
}

void CaptureDevicePerformWithLockedConfiguration(Action handler)
{
if (captureDevice.LockForConfiguration(out var err))
{
try
{
handler();
}
finally
{
captureDevice.UnlockForConfiguration();
}
}
Expand All @@ -178,14 +226,12 @@ public void AutoFocus()
if (captureDevice.IsFocusModeSupported(AVCaptureFocusMode.ContinuousAutoFocus))
focusMode = AVCaptureFocusMode.ContinuousAutoFocus;

//Lock device to config
if (captureDevice.LockForConfiguration(out var err))
CaptureDevicePerformWithLockedConfiguration(() =>
{
if (captureDevice.FocusPointOfInterestSupported)
captureDevice.FocusPointOfInterest = CoreGraphics.CGPoint.Empty;
captureDevice.FocusMode = focusMode;
captureDevice.UnlockForConfiguration();
}
});
}

public void Dispose()
Expand Down
2 changes: 1 addition & 1 deletion ZXing.Net.MAUI/Controls/CameraBarcodeReaderView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void Handler_FrameReady(object sender, CameraFrameBufferEventArgs e)
=> FrameReady?.Invoke(this, e);

public static readonly BindableProperty IsTorchOnProperty =
BindableProperty.Create(nameof(IsTorchOn), typeof(bool), typeof(CameraBarcodeReaderView), defaultValue: true);
BindableProperty.Create(nameof(IsTorchOn), typeof(bool), typeof(CameraBarcodeReaderView), defaultValue: false);

public bool IsTorchOn
{
Expand Down
2 changes: 1 addition & 1 deletion ZXing.Net.MAUI/ZXing.Net.MAUI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>net6.0-android;net6.0-maccatalyst;net6.0-ios</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows')) and '$(MSBuildRuntimeType)' == 'Full'">$(TargetFrameworks);net6.0-windows10.0.19041</TargetFrameworks>
<PackageId>ZXing.Net.Maui</PackageId>
<PackageId>Redth.ZXing.Net.Maui</PackageId>
<Title>ZXing.Net.MAUI Barcode Scanner for .NET MAUI</Title>
<Authors>Redth</Authors>
<UseMaui>True</UseMaui>
Expand Down

0 comments on commit 67a31c3

Please sign in to comment.