Skip to content

Commit a1dd899

Browse files
committed
Added drawQuietZones parameter to PngByteQRCode
1 parent da3eb22 commit a1dd899

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

QRCoder/PngByteQRCode.cs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ public PngByteQRCode(QRCodeData data) : base(data)
2020
/// <summary>
2121
/// Creates a black & white PNG of the QR code, using 1-bit grayscale.
2222
/// </summary>
23-
public byte[] GetGraphic(int pixelsPerModule)
23+
public byte[] GetGraphic(int pixelsPerModule, bool drawQuietZones = true)
2424
{
2525
using (var png = new PngBuilder())
2626
{
27-
var size = this.QrCodeData.ModuleMatrix.Count * pixelsPerModule;
27+
var size = (this.QrCodeData.ModuleMatrix.Count - (drawQuietZones ? 0 : 8)) * pixelsPerModule;
2828
png.WriteHeader(size, size, 1, PngBuilder.ColorType.Greyscale);
29-
png.WriteScanlines(this.DrawScanlines(pixelsPerModule));
29+
png.WriteScanlines(this.DrawScanlines(pixelsPerModule, drawQuietZones));
3030
png.WriteEnd();
3131
return png.GetBytes();
3232
}
@@ -35,14 +35,14 @@ public byte[] GetGraphic(int pixelsPerModule)
3535
/// <summary>
3636
/// Creates 2-color PNG of the QR code, using 1-bit indexed color. Accepts 3-byte RGB colors for normal images and 4-byte RGBA-colors for transparent images.
3737
/// </summary>
38-
public byte[] GetGraphic(int pixelsPerModule, byte[] darkColorRgba, byte[] lightColorRgba)
38+
public byte[] GetGraphic(int pixelsPerModule, byte[] darkColorRgba, byte[] lightColorRgba, bool drawQuietZones = true)
3939
{
4040
using (var png = new PngBuilder())
4141
{
42-
var size = this.QrCodeData.ModuleMatrix.Count * pixelsPerModule;
42+
var size = (this.QrCodeData.ModuleMatrix.Count - (drawQuietZones ? 0 : 8)) * pixelsPerModule;
4343
png.WriteHeader(size, size, 1, PngBuilder.ColorType.Indexed);
4444
png.WritePalette(darkColorRgba, lightColorRgba);
45-
png.WriteScanlines(this.DrawScanlines(pixelsPerModule));
45+
png.WriteScanlines(this.DrawScanlines(pixelsPerModule, drawQuietZones));
4646
png.WriteEnd();
4747
return png.GetBytes();
4848
}
@@ -51,22 +51,23 @@ public byte[] GetGraphic(int pixelsPerModule, byte[] darkColorRgba, byte[] light
5151
/// <summary>
5252
/// Creates a bitmap where each pixel is represented by a single bit, dark = 0 and light = 1.
5353
/// </summary>
54-
private byte[] DrawScanlines(int pixelsPerModule)
54+
private byte[] DrawScanlines(int pixelsPerModule, bool drawQuietZones)
5555
{
5656
var moduleMatrix = this.QrCodeData.ModuleMatrix;
57-
var matrixSize = moduleMatrix.Count;
57+
var matrixSize = moduleMatrix.Count - (drawQuietZones ? 0 : 8);
58+
var quietZoneOffset = (drawQuietZones ? 0 : 4);
5859
var bytesPerScanline = (matrixSize * pixelsPerModule + 7) / 8 + 1; // A monochrome scanline is one byte for filter type then one bit per pixel.
5960
var scanlines = new byte[bytesPerScanline * matrixSize * pixelsPerModule];
6061

6162
for (var y = 0; y < matrixSize; y++)
6263
{
63-
var modules = moduleMatrix[y];
64+
var modules = moduleMatrix[y+quietZoneOffset];
6465
var scanlineOffset = y * pixelsPerModule * bytesPerScanline;
6566

6667
// Draw a scanline with the modules from the QR code.
6768
for (var x = 0; x < matrixSize; x++)
6869
{
69-
if (modules[x])
70+
if (modules[x + quietZoneOffset])
7071
{
7172
continue;
7273
}
@@ -319,22 +320,22 @@ private static uint Crc32(byte[] data, int index, int length)
319320

320321
public static class PngByteQRCodeHelper
321322
{
322-
public static byte[] GetQRCode(string plainText, int pixelsPerModule, byte[] darkColorRgba, byte[] lightColorRgba, ECCLevel eccLevel, bool forceUtf8 = false, bool utf8BOM = false, EciMode eciMode = EciMode.Default, int requestedVersion = -1)
323+
public static byte[] GetQRCode(string plainText, int pixelsPerModule, byte[] darkColorRgba, byte[] lightColorRgba, ECCLevel eccLevel, bool forceUtf8 = false, bool utf8BOM = false, EciMode eciMode = EciMode.Default, int requestedVersion = -1, bool drawQuietZones = true)
323324
{
324325
using (var qrGenerator = new QRCodeGenerator())
325326
using (var qrCodeData = qrGenerator.CreateQrCode(plainText, eccLevel, forceUtf8, utf8BOM, eciMode, requestedVersion))
326327
using (var qrCode = new PngByteQRCode(qrCodeData))
327-
return qrCode.GetGraphic(pixelsPerModule, darkColorRgba, lightColorRgba);
328+
return qrCode.GetGraphic(pixelsPerModule, darkColorRgba, lightColorRgba, drawQuietZones);
328329
}
329330

330331

331332

332-
public static byte[] GetQRCode(string txt, QRCodeGenerator.ECCLevel eccLevel, int size)
333+
public static byte[] GetQRCode(string txt, QRCodeGenerator.ECCLevel eccLevel, int size, bool drawQuietZones = true)
333334
{
334335
using (var qrGen = new QRCodeGenerator())
335336
using (var qrCode = qrGen.CreateQrCode(txt, eccLevel))
336337
using (var qrPng = new PngByteQRCode(qrCode))
337-
return qrPng.GetGraphic(size);
338+
return qrPng.GetGraphic(size, drawQuietZones);
338339
}
339340
}
340341
}

0 commit comments

Comments
 (0)