Skip to content

Commit

Permalink
GTK rendering support
Browse files Browse the repository at this point in the history
Based on Redth#798

Fixes Redth#797
  • Loading branch information
knocte committed Sep 17, 2020
1 parent 77fa778 commit 9694667
Show file tree
Hide file tree
Showing 43 changed files with 262 additions and 14 deletions.
4 changes: 2 additions & 2 deletions Samples/Sample.Android/Sample.Android.csproj
Expand Up @@ -93,8 +93,8 @@
<PackageReference Include="Xamarin.AndroidX.Browser">
<Version>1.0.0</Version>
</PackageReference>
<PackageReference Include="Xamarin.Essentials">
<Version>1.5.1</Version>
<PackageReference Include="DotNetEssentials">
<Version>1.5.9999--date20200916-1021.git-1e3232b</Version>
</PackageReference>
</ItemGroup>
</Project>
Expand Up @@ -58,7 +58,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Xamarin.Forms" Version="4.5.0.356" />
<PackageReference Include="Xamarin.Essentials" Version="1.5.1" />
<PackageReference Include="DotNetEssentials" Version="1.5.9999--date20200916-1021.git-1e3232b" />
</ItemGroup>
<ItemGroup>
<Compile Include="MainActivity.cs" />
Expand Down
Expand Up @@ -145,7 +145,7 @@
<ItemGroup>
<PackageReference Include="Xamarin.Forms" Version="4.5.0.356" />
<PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform" Version="6.1.9" />
<PackageReference Include="Xamarin.Essentials" Version="1.5.1" />
<PackageReference Include="DotNetEssentials" Version="1.5.9999--date20200916-1021.git-1e3232b" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\ZXing.Net.Mobile.Forms\ZXing.Net.Mobile.Forms.csproj">
Expand Down
Expand Up @@ -126,7 +126,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Xamarin.Forms" Version="4.5.0.356" />
<PackageReference Include="Xamarin.Essentials" Version="1.5.1" />
<PackageReference Include="DotNetEssentials" Version="1.5.9999--date20200916-1021.git-1e3232b" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\ZXing.Net.Mobile.Forms\ZXing.Net.Mobile.Forms.csproj">
Expand Down
2 changes: 1 addition & 1 deletion Samples/Sample.Forms/Sample.Forms/Sample.Forms.csproj
Expand Up @@ -12,7 +12,7 @@

<ItemGroup>
<PackageReference Include="Xamarin.Forms" Version="4.5.0.356" />
<PackageReference Include="Xamarin.Essentials" Version="1.5.1" />
<PackageReference Include="DotNetEssentials" Version="1.5.9999--date20200916-1021.git-1e3232b" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions Samples/Sample.Uwp/Sample.Uwp.csproj
Expand Up @@ -160,8 +160,8 @@
<PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform">
<Version>6.2.9</Version>
</PackageReference>
<PackageReference Include="Xamarin.Essentials">
<Version>1.5.1</Version>
<PackageReference Include="DotNetEssentials">
<Version>1.5.9999--date20200916-1021.git-1e3232b</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions Samples/Sample.iOS/Sample.iOS.csproj
Expand Up @@ -103,8 +103,8 @@
</Reference>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Xamarin.Essentials">
<Version>1.5.1</Version>
<PackageReference Include="DotNetEssentials">
<Version>1.5.9999--date20200916-1021.git-1e3232b</Version>
</PackageReference>
<PackageReference Include="Xamarin.TestCloud.Agent" Version="0.21.1" />
</ItemGroup>
Expand Down
12 changes: 12 additions & 0 deletions ZXing.Net.Mobile.Forms/Platform.gtk.cs
@@ -0,0 +1,12 @@
using System;

namespace ZXing.Net.Mobile.Forms.GTK
{
public class Platform
{
public static void Init ()
{
ZXing.Net.Mobile.Forms.GTK.ZXingBarcodeImageViewRenderer.Init();
}
}
}
15 changes: 13 additions & 2 deletions ZXing.Net.Mobile.Forms/ZXing.Net.Mobile.Forms.csproj
@@ -1,6 +1,6 @@
<Project Sdk="MSBuild.Sdk.Extras/2.0.54">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;Xamarin.iOS10;Xamarin.Mac20;MonoAndroid90;MonoAndroid10.0;tizen40</TargetFrameworks>
<TargetFrameworks>netstandard2.0;Xamarin.iOS10;Xamarin.Mac20;MonoAndroid90;MonoAndroid10.0;tizen40;net461</TargetFrameworks>
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">$(TargetFrameworks);uap10.0.16299;</TargetFrameworks>
<AssemblyName>ZXing.Net.Mobile.Forms</AssemblyName>
<RootNamespace>ZXing.Net.Mobile.Forms</RootNamespace>
Expand Down Expand Up @@ -93,8 +93,19 @@
<Compile Include="**\*.tizen.cs" />
<Compile Include="**\*.tizen.*.cs" />
</ItemGroup>
<ItemGroup Condition=" $(TargetFramework.StartsWith('net461')) ">
<PackageReference Include="Xamarin.Forms.Platform.GTK" Version="4.5.0.356" />
<Reference Include="glib-sharp">
<HintPath>..\ZXing.Net.Mobile\GTK\Libs\gtk-sharp\gtk-sharp-2.0\glib-sharp.dll</HintPath>
</Reference>
<Reference Include="gtk-sharp">
<HintPath>..\ZXing.Net.Mobile\GTK\Libs\gtk-sharp\gtk-sharp-2.0\gtk-sharp.dll</HintPath>
</Reference>
<Compile Include="**\*.gtk.cs" />
<Compile Include="**\*.gtk.*.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Xamarin.Essentials" Version="1.5.1" />
<PackageReference Include="DotNetEssentials" Version="1.5.9999--date20200916-1021.git-1e3232b" />
<PackageReference Include="Xamarin.Forms" Version="4.5.0.356" />
</ItemGroup>
<ItemGroup>
Expand Down
73 changes: 73 additions & 0 deletions ZXing.Net.Mobile.Forms/ZXingBarcodeImageViewRenderer.gtk.cs
@@ -0,0 +1,73 @@
using System;
using System.ComponentModel;
using Xamarin.Forms;
using Xamarin.Forms.Platform.GTK;
using ZXing.Net.Mobile.Forms;
using ZXing.Net.Mobile.Forms.GTK;
using ZXing.Net.Mobile.GTK;
using Image = Gtk.Image;

[assembly:ExportRenderer(typeof(ZXingBarcodeImageView), typeof(ZXingBarcodeImageViewRenderer))]
namespace ZXing.Net.Mobile.Forms.GTK
{
public class ZXingBarcodeImageViewRenderer : ViewRenderer<ZXingBarcodeImageView, Image>
{
public static void Init()
{
var temp = DateTime.Now;
}

ZXingBarcodeImageView formsView;
Image gtkImage;

protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
// in GTK there are a way to many properties that are changed compared to other platforms
if (e.PropertyName == ZXingBarcodeImageView.BarcodeValueProperty.PropertyName ||
e.PropertyName == ZXingBarcodeImageView.BarcodeFormatProperty.PropertyName ||
e.PropertyName == ZXingBarcodeImageView.BarcodeOptionsProperty.PropertyName)
{
Regenerate();
}

base.OnElementPropertyChanged(sender, e);
}

protected override void OnElementChanged(ElementChangedEventArgs<ZXingBarcodeImageView> e)
{
formsView = Element;

if (gtkImage == null)
{
gtkImage = new Image();

base.SetNativeControl(gtkImage);
}

Regenerate();

base.OnElementChanged(e);
}

void Regenerate ()
{
if (formsView != null && formsView.BarcodeValue != null)
{
var writer = new BarcodeWriter();

if (formsView != null && formsView.BarcodeOptions != null)
writer.Options = formsView.BarcodeOptions;
if (formsView != null && formsView.BarcodeFormat != null)
writer.Format = formsView.BarcodeFormat;

var value = formsView != null ? formsView.BarcodeValue : string.Empty;

Device.BeginInvokeOnMainThread(() =>
{
var pixBuf = writer.Write(value);
gtkImage.Pixbuf = pixBuf;
});
}
}
}
}
13 changes: 13 additions & 0 deletions ZXing.Net.Mobile/GTK/BarcodeWriter.gtk.cs
@@ -0,0 +1,13 @@
using Gdk;

namespace ZXing.Net.Mobile.GTK
{
public class BarcodeWriter : BarcodeWriter<Pixbuf>, IBarcodeWriter
{
public BarcodeWriter()
{
Renderer = new BitmapRenderer();
}
}
}

52 changes: 52 additions & 0 deletions ZXing.Net.Mobile/GTK/BitmapRenderer.gtk.cs
@@ -0,0 +1,52 @@
using Cairo;
using Gdk;
using ZXing.Common;
using ZXing.Rendering;

namespace ZXing.Net.Mobile.GTK
{
public class BitmapRenderer : IBarcodeRenderer<Pixbuf>
{
public Pixbuf Render(BitMatrix matrix, BarcodeFormat format, string content)
{
var black = new Cairo.Color(0, 0, 0);
var white = new Cairo.Color(1, 1, 1);
using (var surface = new ImageSurface(Format.RGB24, matrix.Width, matrix.Height))
{
using (var cr = new Context(surface))
{
for (var x = 0; x < matrix.Width; x++)
{
for (var y = 0; y < matrix.Height; y++)
{
SetSourceColor(cr, matrix[x, y] ? black : white);
cr.MoveTo(x, y);
cr.LineTo(x + 1, y);
cr.Stroke();
}
}

const int onlyBitsPerSampleValueSupportedByGdk = 8;
return new Pixbuf(surface.Data,
Colorspace.Rgb,
true,
onlyBitsPerSampleValueSupportedByGdk,
matrix.Width,
matrix.Height,
surface.Stride);
}
}
}

private void SetSourceColor(Context context, Cairo.Color color)
{
context.SetSourceRGB(color.R, color.G, color.B);
}

public Pixbuf Render(BitMatrix matrix, BarcodeFormat format, string content, EncodingOptions options)
{
return Render(matrix, format, content);
}
}
}

Binary file added ZXing.Net.Mobile/GTK/Libs/OpenTK/OpenTK.dll
Binary file not shown.
25 changes: 25 additions & 0 deletions ZXing.Net.Mobile/GTK/Libs/OpenTK/OpenTK.dll.config
@@ -0,0 +1,25 @@
<configuration>
<dllmap os="linux" dll="opengl32.dll" target="libGL.so.1"/>
<dllmap os="linux" dll="glu32.dll" target="libGLU.so.1"/>
<dllmap os="linux" dll="openal32.dll" target="libopenal.so.1"/>
<dllmap os="linux" dll="alut.dll" target="libalut.so.0"/>
<dllmap os="linux" dll="opencl.dll" target="libOpenCL.so"/>
<dllmap os="linux" dll="libX11" target="libX11.so.6"/>
<dllmap os="linux" dll="libXi" target="libXi.so.6"/>
<dllmap os="linux" dll="SDL2.dll" target="libSDL2-2.0.so.0"/>
<dllmap os="osx" dll="opengl32.dll" target="/System/Library/Frameworks/OpenGL.framework/OpenGL"/>
<dllmap os="osx" dll="openal32.dll" target="/System/Library/Frameworks/OpenAL.framework/OpenAL" />
<dllmap os="osx" dll="alut.dll" target="/System/Library/Frameworks/OpenAL.framework/OpenAL" />
<dllmap os="osx" dll="libGLES.dll" target="/System/Library/Frameworks/OpenGLES.framework/OpenGLES" />
<dllmap os="osx" dll="libGLESv1_CM.dll" target="/System/Library/Frameworks/OpenGLES.framework/OpenGLES" />
<dllmap os="osx" dll="libGLESv2.dll" target="/System/Library/Frameworks/OpenGLES.framework/OpenGLES" />
<dllmap os="osx" dll="opencl.dll" target="/System/Library/Frameworks/OpenCL.framework/OpenCL"/>
<dllmap os="osx" dll="SDL2.dll" target="libSDL2.dylib"/>
<!-- XQuartz compatibility (X11 on Mac) -->
<dllmap os="osx" dll="libGL.so.1" target="/usr/X11/lib/libGL.dylib"/>
<dllmap os="osx" dll="libX11" target="/usr/X11/lib/libX11.dylib"/>
<dllmap os="osx" dll="libXcursor.so.1" target="/usr/X11/lib/libXcursor.dylib"/>
<dllmap os="osx" dll="libXi" target="/usr/X11/lib/libXi.dylib"/>
<dllmap os="osx" dll="libXinerama" target="/usr/X11/lib/libXinerama.dylib"/>
<dllmap os="osx" dll="libXrandr.so.2" target="/usr/X11/lib/libXrandr.dylib"/>
</configuration>
1 change: 1 addition & 0 deletions ZXing.Net.Mobile/GTK/Libs/OpenTK/README.txt
@@ -0,0 +1 @@
https://github.com/opentk/opentk
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions ZXing.Net.Mobile/GTK/Libs/gtk-sharp/README.txt
@@ -0,0 +1 @@
https://github.com/mono/gtk-sharp
Binary file not shown.
@@ -0,0 +1,4 @@
<configuration>
<dllmap dll="libgobject-2.0-0.dll" target="/Library/Frameworks/Mono.framework/Versions/5.12.0/lib/libgobject-2.0.0.dylib"/>
<dllmap dll="libatk-1.0-0.dll" target="/Library/Frameworks/Mono.framework/Versions/5.12.0/lib/libatk-1.0.0.dylib"/>
</configuration>
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,6 @@
<configuration>
<dllmap dll="libglib-2.0-0.dll" target="/Library/Frameworks/Mono.framework/Versions/Current/lib/libglib-2.0.0.dylib"/>
<dllmap dll="libgobject-2.0-0.dll" target="/Library/Frameworks/Mono.framework/Versions/Current/lib/libgobject-2.0.0.dylib"/>
<dllmap dll="libgdk-win32-2.0-0.dll" target="/Library/Frameworks/Mono.framework/Versions/Current/lib/libgdk-quartz-2.0.0.dylib"/>
<dllmap dll="libgdk_pixbuf-2.0-0.dll" target="/Library/Frameworks/Mono.framework/Versions/Current/lib/libgdk_pixbuf-2.0.0.dylib"/>
</configuration>
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,3 @@
<configuration>
<dllmap dll="libglade-2.0-0.dll" target="/Library/Frameworks/Mono.framework/Versions/Current/lib/libglade-2.0.0.dylib"/>
</configuration>
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,5 @@
<configuration>
<dllmap dll="libglib-2.0-0.dll" target="/Library/Frameworks/Mono.framework/Versions/Current/lib/libglib-2.0.0.dylib"/>
<dllmap dll="libgobject-2.0-0.dll" target="/Library/Frameworks/Mono.framework/Versions/Current/lib/libgobject-2.0.0.dylib"/>
<dllmap dll="libgthread-2.0-0.dll" target="/Library/Frameworks/Mono.framework/Versions/Current/lib/libgthread-2.0.0.dylib"/>
</configuration>
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,3 @@
<configuration>
<dllmap dll="libgdk-win32-2.0-0.dll" target="/Library/Frameworks/Mono.framework/Versions/Current/lib/libgdk-quartz-2.0.0.dylib"/>
</configuration>
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,7 @@
<configuration>
<dllmap dll="libglib-2.0-0.dll" target="/Library/Frameworks/Mono.framework/Versions/Current/lib/libglib-2.0.0.dylib"/>
<dllmap dll="libgobject-2.0-0.dll" target="/Library/Frameworks/Mono.framework/Versions/Current/lib/libgobject-2.0.0.dylib"/>
<dllmap dll="libatk-1.0-0.dll" target="/Library/Frameworks/Mono.framework/Versions/Current/lib/libatk-1.0.0.dylib"/>
<dllmap dll="libgtk-win32-2.0-0.dll" target="/Library/Frameworks/Mono.framework/Versions/Current/lib/libgtk-quartz-2.0.0.dylib"/>
<dllmap dll="libgdk-win32-2.0-0.dll" target="/Library/Frameworks/Mono.framework/Versions/Current/lib/libgdk-quartz-2.0.0.dylib"/>
</configuration>
Binary file not shown.
@@ -0,0 +1,6 @@
<configuration>
<dllmap dll="libglib-2.0-0.dll" target="/Library/Frameworks/Mono.framework/Versions/Current/lib/libglib-2.0.0.dylib"/>
<dllmap dll="libgobject-2.0-0.dll" target="/Library/Frameworks/Mono.framework/Versions/Current/lib/libgobject-2.0.0.dylib"/>
<dllmap dll="libpango-1.0-0.dll" target="/Library/Frameworks/Mono.framework/Versions/Current/lib/libpango-1.0.0.dylib"/>
<dllmap dll="libpangocairo-1.0-0.dll" target="/Library/Frameworks/Mono.framework/Versions/Current/lib/libpangocairo-1.0.0.dylib"/>
</configuration>
Binary file not shown.
1 change: 1 addition & 0 deletions ZXing.Net.Mobile/GTK/Libs/webkit-sharp/README.txt
@@ -0,0 +1 @@
https://github.com/mono/webkit-sharp
Binary file not shown.
@@ -0,0 +1,5 @@
<configuration>
<dllmap dll="webkit-1.0" target="libwebkitgtk-1.0.0.dylib"/>
<dllmap dll="webkit-1.0" os="linux" target="libwebkitgtk-1.0.so.0"/>
<dllmap dll="webkit-1.0" os="windows" target="libwebkitgtk-1.0-0.dll"/>
</configuration>
15 changes: 13 additions & 2 deletions ZXing.Net.Mobile/ZXing.Net.Mobile.csproj
@@ -1,6 +1,6 @@
<Project Sdk="MSBuild.Sdk.Extras/2.0.54">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;Xamarin.iOS10;Xamarin.Mac20;MonoAndroid90;MonoAndroid10.0;tizen40</TargetFrameworks>
<TargetFrameworks>netstandard2.0;Xamarin.iOS10;Xamarin.Mac20;MonoAndroid90;MonoAndroid10.0;tizen40;net461</TargetFrameworks>
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">$(TargetFrameworks);uap10.0.16299;</TargetFrameworks>
<AssemblyName>ZXingNetMobile</AssemblyName>
<RootNamespace>ZXing.Net.Mobile</RootNamespace>
Expand Down Expand Up @@ -111,8 +111,18 @@
<Compile Include="**\*.tizen.cs" />
<Compile Include="**\*.tizen.*.cs" />
</ItemGroup>
<ItemGroup Condition=" $(TargetFramework.StartsWith('net461')) ">
<Reference Include="gtk-sharp">
<HintPath>GTK\Libs\gtk-sharp\gtk-sharp-2.0\gtk-sharp.dll</HintPath>
</Reference>
<Reference Include="gdk-sharp">
<HintPath>GTK\Libs\gtk-sharp\gtk-sharp-2.0\gdk-sharp.dll</HintPath>
</Reference>
<Compile Include="**\*.gtk.cs" />
<Compile Include="**\*.gtk.*.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Xamarin.Essentials" Version="1.5.1" />
<PackageReference Include="DotNetEssentials" Version="1.5.9999--date20200916-1021.git-1e3232b" />
</ItemGroup>
<ItemGroup>
<Reference Include="zxing">
Expand All @@ -121,6 +131,7 @@
<None Include="..\ZXing.Net\zxing.dll" Pack="True" PackagePath="lib\monoandroid10.0" />
<None Include="..\ZXing.Net\zxing.dll" Pack="True" PackagePath="lib\monoandroid90" />
<None Include="..\ZXing.Net\zxing.dll" Pack="True" PackagePath="lib\netstandard2.0" />
<None Include="..\ZXing.Net\zxing.dll" Pack="True" PackagePath="lib\net461" />
<None Include="..\ZXing.Net\zxing.dll" Pack="True" PackagePath="lib\tizen40" />
<None Include="..\ZXing.Net\zxing.dll" Pack="True" PackagePath="lib\uap10.0.16299" />
<None Include="..\ZXing.Net\zxing.dll" Pack="True" PackagePath="lib\xamarinios10" />
Expand Down
9 changes: 9 additions & 0 deletions readme.md
Expand Up @@ -87,13 +87,22 @@ In your `AppDelegate`'s `FinishedLaunching (..)` implementation, call:
ZXing.Net.Mobile.Forms.MacOS.Platform.Init();
```

##### GTK

In your `MainClass`'s `Main (..)` implementation, call:

```csharp
ZXing.Net.Mobile.Forms.GTK.Platform.Init();
```


### Features
- Xamarin.iOS
- Xamarin.Android
- Tizen
- UWP
- Xamarin.Mac (rendering only, not scanning)
- GTK# (rendering only, not scanning)
- Simple API - Scan in as little as 2 lines of code!
- Scanner as a View - UIView (iOS) / Fragment (Android) / Control (WP)

Expand Down

0 comments on commit 9694667

Please sign in to comment.