Skip to content

Commit

Permalink
Merge pull request #190 from Cheesebaron/patch-1
Browse files Browse the repository at this point in the history
Update iOS BarcodeBitmapRenderer to use UIGraphicsImaegeRenderer
  • Loading branch information
Redth authored Sep 12, 2024
2 parents d37bbd0 + 39233c0 commit 36f4294
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 16 deletions.
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
6 changes: 3 additions & 3 deletions ZXing.Net.MAUI/ZXing.Net.MAUI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
<IsPackable>true</IsPackable>
</PropertyGroup>
<ItemGroup Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">
<PackageReference Include="Xamarin.AndroidX.Camera.Camera2" Version="1.3.4.2" />
<PackageReference Include="Xamarin.AndroidX.Camera.Lifecycle" Version="1.3.4.2" />
<PackageReference Include="Xamarin.AndroidX.Camera.View" Version="1.3.4.2" />
<PackageReference Include="Xamarin.AndroidX.Camera.Camera2" Version="1.1.0" />
<PackageReference Include="Xamarin.AndroidX.Camera.Lifecycle" Version="1.1.0" />
<PackageReference Include="Xamarin.AndroidX.Camera.View" Version="1.1.0" />

<AndroidManifest Include="Platforms/Android/AndroidManifest.xml" />
</ItemGroup>
Expand Down

0 comments on commit 36f4294

Please sign in to comment.