Skip to content

Commit

Permalink
Merge branch 'Redth:main' into FixMultiPage
Browse files Browse the repository at this point in the history
  • Loading branch information
paoldev authored Sep 29, 2024
2 parents f6c4002 + 36f4294 commit 1ea12ac
Show file tree
Hide file tree
Showing 13 changed files with 110 additions and 43 deletions.
8 changes: 6 additions & 2 deletions BigIslandBarcode/BigIslandBarcode.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

<PropertyGroup>
<!-- iOS, Android, MacCatalyst -->
<TargetFrameworks>net7.0-android;net7.0-ios;net7.0-maccatalyst</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net7.0-windows10.0.19041</TargetFrameworks>
<TargetFrameworks>net8.0-android;net8.0-ios;net8.0-maccatalyst</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net8.0-windows10.0.19041</TargetFrameworks>
<OutputType>Exe</OutputType>
<SingleProject>true</SingleProject>
<UseMaui>true</UseMaui>
Expand Down Expand Up @@ -52,4 +52,8 @@
<ProjectReference Include="..\ZXing.Net.MAUI.Controls\ZXing.Net.MAUI.Controls.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Maui.Controls" Version="8.0.82" />
</ItemGroup>

</Project>
3 changes: 3 additions & 0 deletions BigIslandBarcode/Platforms/Windows/Package.appxmanifest
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest"
IgnorableNamespaces="uap rescap">

<Identity Name="maui-package-name-placeholder" Publisher="CN=User Name" Version="0.0.0.0" />

<mp:PhoneIdentity PhoneProductId="0F45CBB5-CE11-4ABF-BECD-E13B8FB38744" PhonePublisherId="00000000-0000-0000-0000-000000000000"/>

<Properties>
<DisplayName>$placeholder$</DisplayName>
<PublisherDisplayName>User Name</PublisherDisplayName>
Expand Down
7 changes: 5 additions & 2 deletions ZXing.Net.MAUI.Comet/ZXing.Net.MAUI.Comet.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net7.0;net7.0-android;net7.0-maccatalyst;net7.0-ios</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net7.0-windows10.0.19041</TargetFrameworks>
<TargetFrameworks>net8.0;net8.0-android;net8.0-maccatalyst;net8.0-ios</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net8.0-windows10.0.19041</TargetFrameworks>
<PackageId>ZXing.Net.Maui.Comet</PackageId>
<Title>ZXing.Net.MAUI Barcode Scanner for .NET MAUI</Title>
<Authors>Redth</Authors>
Expand All @@ -25,4 +25,7 @@
<ItemGroup>
<ProjectReference Include="..\ZXing.Net.MAUI\ZXing.Net.MAUI.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Maui.Controls" Version="8.0.82" />
</ItemGroup>
</Project>
12 changes: 10 additions & 2 deletions ZXing.Net.MAUI.Controls/Controls/CameraBarcodeReaderView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ public partial class CameraBarcodeReaderView : View, ICameraBarcodeReaderView
public event EventHandler<BarcodeDetectionEventArgs> BarcodesDetected;
public event EventHandler<CameraFrameBufferEventArgs> FrameReady;

void ICameraBarcodeReaderView.BarcodesDetected(BarcodeDetectionEventArgs e) => BarcodesDetected?.Invoke(this, e);
public CameraBarcodeReaderView()
{
Unloaded += (s, e) => Cleanup();
}

void ICameraBarcodeReaderView.BarcodesDetected(BarcodeDetectionEventArgs e) => BarcodesDetected?.Invoke(this, e);
void ICameraFrameAnalyzer.FrameReady(ZXing.Net.Maui.CameraFrameBufferEventArgs e) => FrameReady?.Invoke(this, e);

public static readonly BindableProperty OptionsProperty =
Expand Down Expand Up @@ -65,5 +70,8 @@ public void Focus(Point point)

CameraBarcodeReaderViewHandler StrongHandler
=> Handler as CameraBarcodeReaderViewHandler;
}

private void Cleanup()
=> Handler?.DisconnectHandler();
}
}
12 changes: 10 additions & 2 deletions ZXing.Net.MAUI.Controls/Controls/CameraView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ public partial class CameraView : View, ICameraView
{
public event EventHandler<CameraFrameBufferEventArgs> FrameReady;

void ICameraFrameAnalyzer.FrameReady(CameraFrameBufferEventArgs e)
public CameraView()
{
Unloaded += (s, e) => Cleanup();
}

void ICameraFrameAnalyzer.FrameReady(CameraFrameBufferEventArgs e)
=> FrameReady?.Invoke(this, e);

public static readonly BindableProperty IsTorchOnProperty =
Expand Down Expand Up @@ -38,5 +43,8 @@ public void Focus(Point point)

CameraViewHandler StrongHandler
=> Handler as CameraViewHandler;
}

private void Cleanup()
=> Handler?.DisconnectHandler();
}
}
7 changes: 5 additions & 2 deletions ZXing.Net.MAUI.Controls/ZXing.Net.MAUI.Controls.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net7.0;net7.0-android;net7.0-maccatalyst;net7.0-ios</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net7.0-windows10.0.19041</TargetFrameworks>
<TargetFrameworks>net8.0;net8.0-android;net8.0-maccatalyst;net8.0-ios</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net8.0-windows10.0.19041</TargetFrameworks>
<PackageId>ZXing.Net.Maui.Controls</PackageId>
<Title>ZXing.Net.MAUI Barcode Scanner for .NET MAUI</Title>
<Authors>Redth</Authors>
Expand All @@ -24,4 +24,7 @@
<ItemGroup>
<ProjectReference Include="..\ZXing.Net.MAUI\ZXing.Net.MAUI.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Maui.Controls" Version="8.0.82" />
</ItemGroup>
</Project>
44 changes: 31 additions & 13 deletions ZXing.Net.MAUI/Apple/BarcodeBitmapRenderer.ios.maccatalyst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using Microsoft.Maui.Graphics.Platform;
using MauiColor = Microsoft.Maui.Graphics.Color;
using ZXing.Common;
using System.Threading;


#if IOS || MACCATALYST
using Foundation;
Expand Down Expand Up @@ -42,24 +44,40 @@ public UIImage Render(BitMatrix matrix, ZXing.BarcodeFormat format, string conte

public UIImage Render(BitMatrix matrix, ZXing.BarcodeFormat format, string content, EncodingOptions options)
{
UIGraphics.BeginImageContext(new CGSize(matrix.Width, matrix.Height));
var context = UIGraphics.GetCurrentContext();
var renderer = new UIGraphicsImageRenderer(new CGSize(matrix.Width, matrix.Height), new UIGraphicsImageRendererFormat {
Opaque = false,
Scale = UIScreen.MainScreen.Scale
});

for (var x = 0; x < matrix.Width; x++)
var waiter = new ManualResetEvent(false);
UIImage image = null!;

renderer.CreateImage(context =>
{
for (var y = 0; y < matrix.Height; y++)
var black = new CGColor(0f, 0f, 0f);
var white = new CGColor(1.0f, 1.0f, 1.0f);

for (var x = 0; x < matrix.Width; x++)
{
context.SetFillColor(matrix[x, y] ? ForegroundColor : BackgroundColor);
context.FillRect(new CGRect(x, y, 1, 1));
for (var y = 0; y < matrix.Height; y++)
{
context.CGContext.SetFillColor(matrix[x, y] ? black : white);
context.CGContext.FillRect(new CGRect(x, y, 1, 1));
}
}
}

var img = UIGraphics.GetImageFromCurrentImageContext();
SetImage(context.CurrentImage);
});

UIGraphics.EndImageContext();

return img;
waiter.WaitOne();
return image;

void SetImage(UIImage img)
{
image = img;
waiter.Set();
}
}
}
}
#endif
#endif
4 changes: 3 additions & 1 deletion ZXing.Net.MAUI/BarcodeScannerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ public record BarcodeReaderOptions

public bool Multiple { get; init; }

}
public bool UseCode39ExtendedMode { get; init; }

}
}
19 changes: 13 additions & 6 deletions ZXing.Net.MAUI/CameraBarcodeReaderViewHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,24 @@ protected override async void ConnectHandler(NativePlatformCameraPreviewView nat
{
base.ConnectHandler(nativeView);

if (await cameraManager.CheckPermissions())
cameraManager.Connect();
if (cameraManager != null)
{
if (await cameraManager.CheckPermissions())
cameraManager.Connect();

cameraManager.FrameReady += CameraManager_FrameReady;
cameraManager.FrameReady += CameraManager_FrameReady;
}
}

protected override void DisconnectHandler(NativePlatformCameraPreviewView nativeView)
{
cameraManager.FrameReady -= CameraManager_FrameReady;
if (cameraManager != null)
{
cameraManager.FrameReady -= CameraManager_FrameReady;

cameraManager.Disconnect();
cameraManager.Disconnect();
cameraManager.Dispose();
}

base.DisconnectHandler(nativeView);
}
Expand All @@ -70,7 +77,7 @@ private void CameraManager_FrameReady(object sender, CameraFrameBufferEventArgs
{
VirtualView?.FrameReady(e);

if (VirtualView.IsDetecting)
if (VirtualView?.IsDetecting ?? false)
{
var barcodes = BarcodeReader.Decode(e.Data);

Expand Down
21 changes: 14 additions & 7 deletions ZXing.Net.MAUI/CameraViewHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,28 @@ protected override NativePlatformCameraPreviewView CreatePlatformView()
protected override async void ConnectHandler(NativePlatformCameraPreviewView nativeView)
{
base.ConnectHandler(nativeView);

if (await cameraManager.CheckPermissions())
cameraManager.Connect();

cameraManager.FrameReady += CameraManager_FrameReady;

if (cameraManager != null)
{
if (await cameraManager.CheckPermissions())
cameraManager.Connect();

cameraManager.FrameReady += CameraManager_FrameReady;
}
}

void CameraManager_FrameReady(object sender, CameraFrameBufferEventArgs e)
=> VirtualView?.FrameReady(e);

protected override void DisconnectHandler(NativePlatformCameraPreviewView nativeView)
{
cameraManager.FrameReady -= CameraManager_FrameReady;
if (cameraManager != null)
{
cameraManager.FrameReady -= CameraManager_FrameReady;

cameraManager.Disconnect();
cameraManager.Disconnect();
cameraManager.Dispose();
}

base.DisconnectHandler(nativeView);
}
Expand Down
2 changes: 2 additions & 0 deletions ZXing.Net.MAUI/Platforms/Android/CameraManager.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ public void AutoFocus()

public void Dispose()
{
cameraProvider?.Shutdown();

cameraExecutor?.Shutdown();
cameraExecutor?.Dispose();
}
Expand Down
11 changes: 6 additions & 5 deletions ZXing.Net.MAUI/ZXing.Net.MAUI.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net7.0;net7.0-android;net7.0-maccatalyst;net7.0-ios</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net7.0-windows10.0.19041</TargetFrameworks>
<TargetFrameworks>net8.0;net8.0-android;net8.0-maccatalyst;net8.0-ios</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net8.0-windows10.0.19041</TargetFrameworks>
<PackageId>ZXing.Net.Maui</PackageId>
<Title>ZXing.Net.MAUI Barcode Scanner for .NET MAUI</Title>
<Authors>Redth</Authors>
Expand All @@ -22,9 +22,6 @@
<DebugType>portable</DebugType>
<IsPackable>true</IsPackable>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net7.0-ios|AnyCPU'">
<CreatePackage>false</CreatePackage>
</PropertyGroup>
<ItemGroup Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">
<PackageReference Include="Xamarin.AndroidX.Camera.Camera2" Version="1.1.0" />
<PackageReference Include="Xamarin.AndroidX.Camera.Lifecycle" Version="1.1.0" />
Expand Down Expand Up @@ -89,4 +86,8 @@
<Compile Condition=" '%(Compile.ExcludeFromCurrentConfiguration)' == 'true' " Remove="**\*.net*$(DefaultLanguageSourceExtension)" />
</ItemGroup>
</Target>

<ItemGroup>
<PackageReference Include="Microsoft.Maui.Controls" Version="8.0.82" />
</ItemGroup>
</Project>
3 changes: 2 additions & 1 deletion ZXing.Net.MAUI/ZXingBarcodeReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public BarcodeReaderOptions Options
zxingReader.Options.TryHarder = options.TryHarder;
zxingReader.AutoRotate = options.AutoRotate;
zxingReader.Options.TryInverted = options.TryInverted;
}
zxingReader.Options.UseCode39ExtendedMode = options.UseCode39ExtendedMode;
}
}

public BarcodeResult[] Decode(PixelBufferHolder image)
Expand Down

0 comments on commit 1ea12ac

Please sign in to comment.