-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathProgram.cs
More file actions
93 lines (82 loc) · 4.23 KB
/
Program.cs
File metadata and controls
93 lines (82 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System;
using System.IO;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.PixelFormats;
const int QrCodeSize = 25;
bool[,] pattern = GetQrPattern();
// L8 is a grayscale pixel format storing a single 8-bit (1 byte) channel of luminance per pixel.
// Make sure to Dispose() the image in your app! We do it by a using statement in this example:
using Image<L8> image = RenderQrCodeToImage(pattern, 10);
string fileName = Path.Combine(Directory.GetCurrentDirectory(), "qr.png");
// Store the result as a grayscale 1 bit per pixel png for maximum compression:
image.SaveAsPng(fileName, new PngEncoder()
{
BitDepth = PngBitDepth.Bit1,
ColorType = PngColorType.Grayscale
});
Console.WriteLine($"Saved to: {fileName}");
static Image<L8> RenderQrCodeToImage(bool[,] pattern, int pixelSize)
{
int imageSize = pixelSize * QrCodeSize;
Image<L8> image = new(imageSize, imageSize);
L8 black = new(0);
L8 white = new(255);
image.ProcessPixelRows(pixelAccessor =>
{
// Scan the QR pattern row-by-row
for (int yQr = 0; yQr < QrCodeSize; yQr++)
{
// Fill 'pixelSize' number image rows that correspond to the current QR pattern row:
for (int y = yQr * pixelSize; y < (yQr + 1) * pixelSize; y++)
{
// Get a Span<L8> of pixels for the current image row:
Span<L8> pixelRow = pixelAccessor.GetRowSpan(y);
// Loop through the values for the current QR pattern row:
for (int xQr = 0; xQr < QrCodeSize; xQr++)
{
L8 color = pattern[xQr, yQr] ? white : black;
// Fill 'pixelSize' number of image pixels corresponding to the current QR pattern value:
for (int x = xQr * pixelSize; x < (xQr + 1) * pixelSize; x++)
{
pixelRow[x] = color;
}
}
}
}
});
return image;
}
static bool[,] GetQrPattern()
{
const bool _ = true;
const bool x = false;
return new[,]
{
{ x, x, x, x, x, x, x, _, x, x, _, _, _, x, _, x, x, _, x, x, x, x, x, x, x },
{ x, _, _, _, _, _, x, _, _, _, _, _, _, _, _, _, _, _, x, _, _, _, _, _, x },
{ x, _, x, x, x, _, x, _, x, _, _, x, x, x, _, x, x, _, x, _, x, x, x, _, x },
{ x, _, x, x, x, _, x, _, x, x, _, x, _, x, x, x, _, _, x, _, x, x, x, _, x },
{ x, _, x, x, x, _, x, _, x, x, x, x, _, _, x, _, x, _, x, _, x, x, x, _, x },
{ x, _, _, _, _, _, x, _, _, x, _, _, x, _, x, _, _, _, x, _, _, _, _, _, x },
{ x, x, x, x, x, x, x, _, x, _, x, _, x, _, x, _, x, _, x, x, x, x, x, x, x },
{ _, _, _, _, _, _, _, _, _, x, _, x, _, _, x, x, _, _, _, _, _, _, _, _, _ },
{ x, x, x, x, _, _, x, _, x, _, _, _, _, x, x, _, _, x, _, _, x, x, x, _, x },
{ x, x, _, x, _, _, _, x, x, x, _, _, _, _, x, _, _, _, _, x, _, _, _, x, _ },
{ _, _, _, _, x, _, x, x, x, _, _, _, _, x, x, _, x, x, x, _, _, _, _, _, _ },
{ _, x, _, _, _, x, _, _, _, _, _, x, x, x, _, x, _, x, _, x, _, x, x, _, _ },
{ _, x, _, _, _, _, x, x, _, x, _, x, _, x, x, _, _, x, x, _, x, _, x, x, x },
{ _, x, _, x, _, _, _, _, x, _, x, x, _, _, _, x, _, _, x, x, x, _, _, _, x },
{ _, x, _, _, _, x, x, x, _, x, x, _, x, _, _, _, _, x, _, _, x, _, x, x, _ },
{ x, _, x, _, x, _, _, _, x, _, _, _, x, _, _, x, _, _, x, x, x, _, _, _, x },
{ _, _, x, _, _, _, x, _, _, _, x, x, _, _, _, x, x, x, x, x, x, x, x, x, x },
{ _, _, _, _, _, _, _, _, x, _, x, _, x, _, x, x, x, _, _, _, x, _, x, _, x },
{ x, x, x, x, x, x, x, _, _, _, _, x, x, x, _, _, x, _, x, _, x, _, x, x, x },
{ x, _, _, _, _, _, x, _, _, _, x, _, x, _, x, x, x, _, _, _, x, _, _, x, x },
{ x, _, x, x, x, _, x, _, _, x, x, x, x, x, _, _, x, x, x, x, x, x, _, x, _ },
{ x, _, x, x, x, _, x, _, x, _, _, _, x, x, _, x, _, _, x, _, x, x, x, x, x },
{ x, _, x, x, x, _, x, _, x, x, x, x, x, x, _, x, x, x, x, _, x, _, x, x, _ },
{ x, _, _, _, _, _, x, _, x, _, x, _, _, _, _, _, _, x, x, _, x, _, x, _, _ },
{ x, x, x, x, x, x, x, _, x, x, _, _, x, _, x, _, _, _, _, x, x, x, x, x, x },
};
}