-
Notifications
You must be signed in to change notification settings - Fork 16
/
QRCodeData.cs
56 lines (49 loc) · 1.48 KB
/
QRCodeData.cs
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
//QRCoder is project by Raffael Herrmann and was first released in 10/2013. It's licensed under the MIT license.
//https://github.com/codebude/QRCoder
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
namespace QRCoder
{
public class QRCodeData
{
public List<BitArray> ModuleMatrix { get; set; }
public QRCodeData(int version)
{
var size = ModulesPerSideFromVersion(version);
ModuleMatrix = new List<BitArray>();
for (var i = 0; i < size; i++)
{
ModuleMatrix.Add(new BitArray(size));
}
}
private static int ModulesPerSideFromVersion(int version)
{
return 21 + (version - 1) * 4;
}
public Bitmap GetBitmap(int pixelsPerModule, Color darkColor, Color lightColor)
{
var size = ModuleMatrix.Count * pixelsPerModule;
var bmp = new Bitmap(size, size);
using (var g = Graphics.FromImage(bmp))
{
using (var darkBrush = new SolidBrush(darkColor))
{
using (var lightBrush = new SolidBrush(lightColor))
{
for (var x = 0; x < size; x += pixelsPerModule)
{
for (var y = 0; y < size; y += pixelsPerModule)
{
var module = ModuleMatrix[(y + pixelsPerModule) / pixelsPerModule - 1][(x + pixelsPerModule) / pixelsPerModule - 1];
var brush = module ? darkBrush : lightBrush;
g.FillRectangle(brush, new Rectangle(x, y, pixelsPerModule, pixelsPerModule));
}
}
}
}
}
return bmp;
}
}
}