Skip to content

Commit cdabe53

Browse files
committed
Added background color, relative pixelsize and fixed quietzone rendering
1 parent 942cd35 commit cdabe53

File tree

1 file changed

+52
-34
lines changed

1 file changed

+52
-34
lines changed

QRCoder/ArtQRCode.cs

Lines changed: 52 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,43 +18,39 @@ public ArtQRCode(QRCodeData data) : base(data) { }
1818

1919
public Bitmap GetGraphic(int pixelsPerModule)
2020
{
21-
return this.GetGraphic(pixelsPerModule, (pixelsPerModule * 8) / 10, Color.Black, Color.White);
21+
return this.GetGraphic(pixelsPerModule, Color.Black, Color.White, Color.Transparent);
2222
}
2323

2424
public Bitmap GetGraphic(Bitmap backgroundImage = null)
2525
{
26-
return this.GetGraphic(10, 7, Color.Black, Color.White, backgroundImage: backgroundImage);
26+
return this.GetGraphic(10, Color.Black, Color.White, Color.Transparent, backgroundImage: backgroundImage);
2727
}
2828

29-
public Bitmap GetGraphic(
30-
int pixelsPerModule,
31-
int pixelSize,
32-
Color darkColor,
33-
Color lightColor,
34-
bool drawQuietZones = false,
35-
Bitmap reticleImage = null,
36-
Bitmap backgroundImage = null)
29+
public Bitmap GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor, Color backgroundColor, Bitmap backgroundImage = null, double pixelSizeFactor = 0.8,
30+
bool drawQuietZones = true, QuietZoneStyle quietZoneRenderingStyle = QuietZoneStyle.Flat, Bitmap finderPatternImage = null)
3731
{
38-
var numModules = this.QrCodeData.ModuleMatrix.Count - (drawQuietZones ? 0 : 8);
32+
if (pixelSizeFactor > 1)
33+
throw new Exception("pixelSize must be between 0 and 1. (0-100%)");
34+
int pixelSize = (int)Math.Min(pixelsPerModule, Math.Floor(pixelsPerModule / pixelSizeFactor));
35+
36+
var numModules = QrCodeData.ModuleMatrix.Count - (drawQuietZones ? 0 : 8);
3937
var offset = (drawQuietZones ? 0 : 4);
4038
var size = numModules * pixelsPerModule;
4139

42-
var bitmap = Resize(backgroundImage,size) ?? new Bitmap(size, size);
40+
var bitmap = new Bitmap(size, size);
4341

4442
using (var graphics = Graphics.FromImage(bitmap))
4543
{
4644
using (var lightBrush = new SolidBrush(lightColor))
4745
{
4846
using (var darkBrush = new SolidBrush(darkColor))
4947
{
50-
// make background transparent if you don't have an image -- not sure this is needed
51-
if (backgroundImage == null)
52-
{
53-
using (var brush = new SolidBrush(Color.Transparent))
54-
{
55-
graphics.FillRectangle(brush, new Rectangle(0, 0, size, size));
56-
}
57-
}
48+
// make background transparent
49+
using (var brush = new SolidBrush(backgroundColor))
50+
graphics.FillRectangle(brush, new Rectangle(0, 0, size, size));
51+
//Render background if set
52+
if (backgroundImage != null)
53+
graphics.DrawImage(Resize(backgroundImage, size), 0, 0);
5854

5955
var darkModulePixel = MakeDotPixel(pixelsPerModule, pixelSize, darkBrush);
6056
var lightModulePixel = MakeDotPixel(pixelsPerModule, pixelSize, lightBrush);
@@ -69,19 +65,22 @@ public Bitmap GetGraphic(
6965
var solidBrush = pixelIsDark ? darkBrush : lightBrush;
7066
var pixelImage = pixelIsDark ? darkModulePixel : lightModulePixel;
7167

72-
if (!IsPartOfReticle(x, y, numModules, offset))
73-
graphics.DrawImage(pixelImage, rectangleF);
74-
else if (reticleImage == null)
68+
if (!IsPartOfFinderPattern(x, y, numModules, offset))
69+
if (drawQuietZones && quietZoneRenderingStyle == QuietZoneStyle.Flat && IsPartOfQuietZone(x, y, numModules))
70+
graphics.FillRectangle(solidBrush, rectangleF);
71+
else
72+
graphics.DrawImage(pixelImage, rectangleF);
73+
else if (finderPatternImage == null)
7574
graphics.FillRectangle(solidBrush, rectangleF);
7675
}
7776
}
7877

79-
if (reticleImage != null)
78+
if (finderPatternImage != null)
8079
{
81-
var reticleSize = 7 * pixelsPerModule;
82-
graphics.DrawImage(reticleImage, new Rectangle(0, 0, reticleSize, reticleSize));
83-
graphics.DrawImage(reticleImage, new Rectangle(size - reticleSize, 0, reticleSize, reticleSize));
84-
graphics.DrawImage(reticleImage, new Rectangle(0, size - reticleSize, reticleSize, reticleSize));
80+
var finderPatternSize = 7 * pixelsPerModule;
81+
graphics.DrawImage(finderPatternImage, new Rectangle(0, 0, finderPatternSize, finderPatternSize));
82+
graphics.DrawImage(finderPatternImage, new Rectangle(size - finderPatternSize, 0, finderPatternSize, finderPatternSize));
83+
graphics.DrawImage(finderPatternImage, new Rectangle(0, size - finderPatternSize, finderPatternSize, finderPatternSize));
8584
}
8685

8786
graphics.Save();
@@ -99,7 +98,7 @@ public Bitmap GetGraphic(
9998
/// <param name="brush"></param>
10099
/// <returns></returns>
101100
private Bitmap MakeDotPixel(int pixelsPerModule, int pixelSize, SolidBrush brush)
102-
{
101+
{
103102
// draw a dot
104103
var bitmap = new Bitmap(pixelSize, pixelSize);
105104
using (var graphics = Graphics.FromImage(bitmap))
@@ -124,13 +123,27 @@ private Bitmap MakeDotPixel(int pixelsPerModule, int pixelSize, SolidBrush brush
124123
return cropped;
125124
}
126125

127-
private bool IsPartOfReticle(int x, int y, int numModules, int offset)
126+
127+
private bool IsPartOfQuietZone(int x, int y, int numModules)
128+
{
129+
return
130+
x < 4 || //left
131+
y < 4 || //top
132+
x > numModules - 5 || //right
133+
y > numModules - 5; //bottom
134+
}
135+
136+
137+
private bool IsPartOfFinderPattern(int x, int y, int numModules, int offset)
128138
{
129139
var cornerSize = 11 - offset;
140+
var outerLimitLow = (numModules - cornerSize - 1);
141+
var outerLimitHigh = outerLimitLow + 8;
142+
var invertedOffset = 4 - offset;
130143
return
131-
(x < cornerSize && y < cornerSize) ||
132-
(x > (numModules - cornerSize - 1) && y < cornerSize) ||
133-
(x < cornerSize && y > (numModules - cornerSize - 1));
144+
(x >= invertedOffset && x < cornerSize && y >= invertedOffset && y < cornerSize) || //Top-left finder pattern
145+
(x > outerLimitLow && x < outerLimitHigh && y >= invertedOffset && y < cornerSize) || //Top-right finder pattern
146+
(x >= invertedOffset && x < cornerSize && y > outerLimitLow && y < outerLimitHigh); //Bottom-left finder pattern
134147
}
135148

136149
/// <summary>
@@ -167,9 +180,14 @@ private Bitmap Resize(Bitmap image, int newSize)
167180
graphics.DrawImage(scaledImage, new Rectangle(offsetX, offsetY, scaledWidth, scaledHeight));
168181
}
169182
}
170-
171183
return bm;
172184
}
185+
186+
public enum QuietZoneStyle
187+
{
188+
Dotted,
189+
Flat
190+
}
173191
}
174192
}
175193

0 commit comments

Comments
 (0)