Skip to content

Commit 2fb719b

Browse files
committed
Barcodes you can use backgrounds with
1 parent 079c236 commit 2fb719b

File tree

5 files changed

+182
-66
lines changed

5 files changed

+182
-66
lines changed

QRCoder/LogoQRCode.cs

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
using System;
2+
using System.Drawing;
3+
using System.Drawing.Drawing2D;
4+
5+
namespace QRCoder
6+
{
7+
public class LogoQRCode : AbstractQRCode, IDisposable
8+
{
9+
/// <summary>
10+
/// Constructor without params to be used in COM Objects connections
11+
/// </summary>
12+
public LogoQRCode() { }
13+
14+
public LogoQRCode(QRCodeData data) : base(data) { }
15+
16+
public Bitmap GetGraphic(QRCodeData qrCodeData,
17+
int pixelsPerModule,
18+
int pixelSize,
19+
Color darkColor,
20+
Color lightColor,
21+
bool drawQuietZones = false,
22+
Bitmap reticalImage = null,
23+
Base64QRCode.ImageType imgType = Base64QRCode.ImageType.Png,
24+
Bitmap backgroundImage = null)
25+
{
26+
int numModules = qrCodeData.ModuleMatrix.Count - (drawQuietZones ? 0 : 8);
27+
var offset = (drawQuietZones ? 0 : 4);
28+
int size = numModules * pixelsPerModule;
29+
var moduleMargin = pixelsPerModule - pixelSize;
30+
31+
Bitmap bitmap = backgroundImage ?? new Bitmap(size, size);
32+
bitmap = Resize(bitmap, size);
33+
34+
using (Graphics graphics = Graphics.FromImage(bitmap))
35+
{
36+
using (SolidBrush lightBrush = new SolidBrush(lightColor))
37+
{
38+
using (SolidBrush darkBrush = new SolidBrush(darkColor))
39+
{
40+
// make background transparent if you don't have an image
41+
if (backgroundImage == null)
42+
{
43+
using (var brush = new SolidBrush(Color.Transparent))
44+
{
45+
graphics.FillRectangle(brush, new Rectangle(0, 0, size, size));
46+
}
47+
}
48+
49+
for (int x = 0; x < numModules; x += 1)
50+
{
51+
for (int y = 0; y < numModules; y += 1)
52+
{
53+
var solidBrush = (Brush)(qrCodeData.ModuleMatrix[offset + y][offset + x] ? darkBrush : lightBrush);
54+
55+
if (IsPartOfRetical(x, y, numModules, offset))
56+
if (reticalImage == null)
57+
graphics.FillRectangle(solidBrush, new Rectangle(x * pixelsPerModule, y * pixelsPerModule, pixelsPerModule, pixelsPerModule));
58+
else
59+
graphics.FillEllipse(solidBrush, new Rectangle(x * pixelsPerModule + moduleMargin, y * pixelsPerModule + moduleMargin, pixelSize, pixelSize));
60+
}
61+
}
62+
63+
if (reticalImage != null)
64+
{
65+
var reticleSize = 7 * pixelsPerModule;
66+
graphics.DrawImage(reticalImage, new Rectangle(0, 0, reticleSize, reticleSize));
67+
graphics.DrawImage(reticalImage, new Rectangle(size - reticleSize, 0, reticleSize, reticleSize));
68+
graphics.DrawImage(reticalImage, new Rectangle(0, size - reticleSize, reticleSize, reticleSize));
69+
}
70+
71+
graphics.Save();
72+
}
73+
}
74+
}
75+
return bitmap;
76+
}
77+
78+
private bool IsPartOfRetical(int x, int y, int numModules, int offset)
79+
{
80+
var cornerSize = 7 + offset;
81+
return
82+
(x < cornerSize && y < cornerSize) ||
83+
(x > (numModules - cornerSize) && y < cornerSize) ||
84+
(x < cornerSize && y < (numModules - cornerSize));
85+
}
86+
87+
private Bitmap Resize(Bitmap image, int size)
88+
{
89+
float scale = Math.Min((float)size / image.Width, (float)size / image.Height);
90+
var scaleWidth = (int)(image.Width * scale);
91+
var scaleHeight = (int)(image.Height * scale);
92+
var scaledImage = new Bitmap(image, new Size(scaleWidth, scaleHeight));
93+
94+
var bm = new Bitmap(size, size);
95+
96+
using (Graphics graphics = Graphics.FromImage(bm))
97+
{
98+
using (var brush = new SolidBrush(Color.Transparent))
99+
{
100+
graphics.FillRectangle(brush, new Rectangle(0, 0, size, size));
101+
102+
graphics.InterpolationMode = InterpolationMode.High;
103+
graphics.CompositingQuality = CompositingQuality.HighQuality;
104+
graphics.SmoothingMode = SmoothingMode.AntiAlias;
105+
graphics.DrawImage(scaledImage, new Rectangle((size / 2) - (scaledImage.Width / 2), (size / 2) - (scaledImage.Height / 2), scaledImage.Width, scaledImage.Height));
106+
}
107+
}
108+
109+
return bm;
110+
}
111+
}
112+
}

QRCoder/QRCode.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,9 @@ public Bitmap GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor,
9393
{
9494
for (var y = 0; y < size + offset; y = y + pixelsPerModule)
9595
{
96-
var module = this.QrCodeData.ModuleMatrix[(y + pixelsPerModule) / pixelsPerModule - 1][(x + pixelsPerModule) / pixelsPerModule - 1];
96+
var moduleBrush = this.QrCodeData.ModuleMatrix[(y + pixelsPerModule) / pixelsPerModule - 1][(x + pixelsPerModule) / pixelsPerModule - 1] ? darkBrush : lightBrush;
9797

98-
if (module)
99-
{
100-
var r = new Rectangle(x - offset, y - offset, pixelsPerModule, pixelsPerModule);
101-
gfx.FillRectangle(darkBrush, r);
102-
}
103-
else
104-
{
105-
gfx.FillRectangle(lightBrush, new Rectangle(x - offset, y - offset, pixelsPerModule, pixelsPerModule));
106-
}
98+
gfx.FillRectangle(moduleBrush , new Rectangle(x - offset, y - offset, pixelsPerModule, pixelsPerModule));
10799
}
108100
}
109101

QRCoderDemoUWP/QRCoderDemoUWP.csproj

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111
<AssemblyName>QRCoderDemoUWP</AssemblyName>
1212
<DefaultLanguage>en-US</DefaultLanguage>
1313
<TargetPlatformIdentifier>UAP</TargetPlatformIdentifier>
14-
<TargetPlatformVersion>10.0.10586.0</TargetPlatformVersion>
15-
<TargetPlatformMinVersion>10.0.10240.0</TargetPlatformMinVersion>
14+
<TargetPlatformVersion>10.0.18362.0</TargetPlatformVersion>
15+
<TargetPlatformMinVersion>10.0.18362.0</TargetPlatformMinVersion>
1616
<MinimumVisualStudioVersion>14</MinimumVisualStudioVersion>
1717
<FileAlignment>512</FileAlignment>
1818
<ProjectTypeGuids>{A5A43C5B-DE2A-4C0C-9213-0A381AF9435A};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
1919
<PackageCertificateKeyFile>QRCoderDemoUWP_TemporaryKey.pfx</PackageCertificateKeyFile>
20+
<RuntimeIdentifiers>win10-arm;win10-arm-aot;win10-x86;win10-x86-aot;win10-x64;win10-x64-aot</RuntimeIdentifiers>
2021
</PropertyGroup>
2122
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
2223
<DebugSymbols>true</DebugSymbols>
@@ -87,10 +88,6 @@
8788
<Prefer32Bit>true</Prefer32Bit>
8889
<UseDotNetNativeToolchain>true</UseDotNetNativeToolchain>
8990
</PropertyGroup>
90-
<ItemGroup>
91-
<!-- A reference to the entire .Net Framework and Windows SDK are automatically included -->
92-
<None Include="project.json" />
93-
</ItemGroup>
9491
<ItemGroup>
9592
<Compile Include="App.xaml.cs">
9693
<DependentUpon>App.xaml</DependentUpon>
@@ -131,6 +128,11 @@
131128
<HintPath>..\QRCoder\bin\Debug\netstandard1.1\QRCoder.dll</HintPath>
132129
</Reference>
133130
</ItemGroup>
131+
<ItemGroup>
132+
<PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform">
133+
<Version>5.0.0</Version>
134+
</PackageReference>
135+
</ItemGroup>
134136
<PropertyGroup Condition=" '$(VisualStudioVersion)' == '' or '$(VisualStudioVersion)' &lt; '14.0' ">
135137
<VisualStudioVersion>14.0</VisualStudioVersion>
136138
</PropertyGroup>

QRCoderDemoUWP/project.json

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)