-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
XamlQRCode.cs
94 lines (81 loc) · 4.25 KB
/
XamlQRCode.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
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
94
using System;
using System.Windows;
using System.Windows.Media;
using static QRCoder.QRCodeGenerator;
namespace QRCoder.Xaml
{
public class XamlQRCode : AbstractQRCode, IDisposable
{
/// <summary>
/// Constructor without params to be used in COM Objects connections
/// </summary>
public XamlQRCode() { }
public XamlQRCode(QRCodeData data) : base(data) { }
public DrawingImage GetGraphic(int pixelsPerModule)
{
return this.GetGraphic(pixelsPerModule, true);
}
public DrawingImage GetGraphic(int pixelsPerModule, bool drawQuietZones)
{
var drawableModulesCount = GetDrawableModulesCount(drawQuietZones);
var viewBox = new Size(pixelsPerModule * drawableModulesCount, pixelsPerModule * drawableModulesCount);
return this.GetGraphic(viewBox, new SolidColorBrush(Colors.Black), new SolidColorBrush(Colors.White), drawQuietZones);
}
public DrawingImage GetGraphic(Size viewBox, bool drawQuietZones = true)
{
return this.GetGraphic(viewBox, new SolidColorBrush(Colors.Black), new SolidColorBrush(Colors.White), drawQuietZones);
}
public DrawingImage GetGraphic(int pixelsPerModule, string darkColorHex, string lightColorHex, bool drawQuietZones = true)
{
var drawableModulesCount = GetDrawableModulesCount(drawQuietZones);
var viewBox = new Size(pixelsPerModule * drawableModulesCount, pixelsPerModule * drawableModulesCount);
return this.GetGraphic(viewBox, new SolidColorBrush((Color)ColorConverter.ConvertFromString(darkColorHex)), new SolidColorBrush((Color)ColorConverter.ConvertFromString(lightColorHex)), drawQuietZones);
}
public DrawingImage GetGraphic(Size viewBox, Brush darkBrush, Brush lightBrush, bool drawQuietZones = true)
{
var drawableModulesCount = GetDrawableModulesCount(drawQuietZones);
var qrSize = Math.Min(viewBox.Width, viewBox.Height);
var unitsPerModule = qrSize / drawableModulesCount;
var offsetModules = drawQuietZones ? 0 : 4;
DrawingGroup drawing = new DrawingGroup();
drawing.Children.Add(new GeometryDrawing(lightBrush, null, new RectangleGeometry(new Rect(new Point(0, 0), new Size(qrSize, qrSize)))));
var group = new GeometryGroup();
double x = 0d, y = 0d;
for (int xi = offsetModules; xi < drawableModulesCount + offsetModules; xi++)
{
y = 0d;
for (int yi = offsetModules; yi < drawableModulesCount + offsetModules; yi++)
{
if (this.QrCodeData.ModuleMatrix[yi][xi])
{
group.Children.Add(new RectangleGeometry(new Rect(x, y, unitsPerModule, unitsPerModule)));
}
y += unitsPerModule;
}
x += unitsPerModule;
}
drawing.Children.Add(new GeometryDrawing(darkBrush, null, group));
return new DrawingImage(drawing);
}
private int GetDrawableModulesCount(bool drawQuietZones = true)
{
return this.QrCodeData.ModuleMatrix.Count - (drawQuietZones ? 0 : 8);
}
public double GetUnitsPerModule(Size viewBox, bool drawQuietZones = true)
{
var drawableModulesCount = GetDrawableModulesCount(drawQuietZones);
var qrSize = Math.Min(viewBox.Width, viewBox.Height);
return qrSize / drawableModulesCount;
}
}
public static class XamlQRCodeHelper
{
public static DrawingImage GetQRCode(string plainText, int pixelsPerModule, string darkColorHex, string lightColorHex, ECCLevel eccLevel, bool forceUtf8 = false, bool utf8BOM = false, EciMode eciMode = EciMode.Default, int requestedVersion = -1, bool drawQuietZones = true)
{
using (var qrGenerator = new QRCodeGenerator())
using (var qrCodeData = qrGenerator.CreateQrCode(plainText, eccLevel, forceUtf8, utf8BOM, eciMode, requestedVersion))
using (var qrCode = new XamlQRCode(qrCodeData))
return qrCode.GetGraphic(pixelsPerModule, darkColorHex, lightColorHex, drawQuietZones);
}
}
}