-
-
Notifications
You must be signed in to change notification settings - Fork 58
/
ImageChartsQrCodeProvider.cs
93 lines (82 loc) · 3.86 KB
/
ImageChartsQrCodeProvider.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
using System;
using System.Net.Security;
namespace TwoFactorAuthNet.Providers.Qr;
/// <summary>
/// Provides QR codes generated by image-charts.com.
/// </summary>
/// <seealso href="https://documentation.image-charts.com/gallery/"/>.
public class ImageChartsQrCodeProvider : BaseHttpQrCodeProvider, IQrCodeProvider
{
/// <summary>
/// Gets the <see cref="ErrorCorrectionLevel"/> for the QR code.
/// </summary>
public ErrorCorrectionLevel ErrorCorrectionLevel { get; private set; }
/// <summary>
/// Gets the width of the white border around the data portion of the code.
/// </summary>
/// <remarks>
/// This is in rows, not in pixels.
/// </remarks>
public int MarginRows { get; private set; }
/// <summary>
/// <see cref="BaseHttpQrCodeProvider.BaseUri"/> for this QR code provider.
/// </summary>
private static readonly Uri _baseuri = new("https://image-charts.com/chart");
/// <summary>
/// Initializes a new instance of a <see cref="ImageChartsQrCodeProvider"/> with the specified
/// <see cref="ErrorCorrectionLevel"/>, <see cref="MarginRows"/> and
/// <see cref="RemoteCertificateValidationCallback"/>.
/// </summary>
/// <param name="errorCorrectionLevel">The <see cref="ErrorCorrectionLevel"/> to use when generating QR codes.</param>
/// <param name="marginRows">The width of the white border around the data portion of the code.</param>
/// <param name="remoteCertificateValidationCallback">
/// The <see cref="RemoteCertificateValidationCallback"/> to use when generating QR codes.
/// </param>
/// <exception cref="ArgumentOutOfRangeException">
/// Thrown when an invalid <see cref="ErrorCorrectionLevel"/> is specified or marginRows is less than 0.
/// </exception>
public ImageChartsQrCodeProvider(
ErrorCorrectionLevel errorCorrectionLevel = ErrorCorrectionLevel.Low,
int marginRows = 1,
RemoteCertificateValidationCallback? remoteCertificateValidationCallback = null
)
: base(_baseuri, remoteCertificateValidationCallback)
{
if (!Enum.IsDefined(typeof(ErrorCorrectionLevel), errorCorrectionLevel))
{
throw new ArgumentOutOfRangeException(nameof(errorCorrectionLevel));
}
ErrorCorrectionLevel = errorCorrectionLevel;
if (marginRows < 0)
{
throw new ArgumentOutOfRangeException(nameof(marginRows));
}
MarginRows = marginRows;
}
/// <summary>
/// Downloads / retrieves / generates a QR code as image.
/// </summary>
/// <param name="text">The text to encode in the QR code.</param>
/// <param name="size">The desired size (width and height equal) for the image.</param>
/// <returns>Returns the binary representation of the image.</returns>
/// <seealso cref="IQrCodeProvider"/>
public byte[] GetQrCodeImage(string text, int size) => DownloadData(GetUri(text, size));
/// <summary>
/// Builds an <see cref="Uri"/> based on the instance's <see cref="BaseHttpQrCodeProvider.BaseUri"/>.
/// </summary>
/// <param name="qrText">The text to encode in the QR code.</param>
/// <param name="size">The desired size of the QR code.</param>
/// <returns>A <see cref="Uri"/> to the QR code.</returns>
private Uri GetUri(string qrText, int size) => new(BaseUri,
"?cht=qr"
+ "&chs=" + (size / 2) + "x" + (size / 2) // Size isn't really reliable; 100 gives an image of 172, 200 gives an image of 387 and 300 gives an image of 602...
+ "&chld=" + (char)ErrorCorrectionLevel + "|" + MarginRows
+ "&chl=" + Uri.EscapeDataString(qrText)
);
/// <summary>
/// Gets the MIME type of the image.
/// </summary>
/// <returns>Returns the MIME type of the image.</returns>
/// <seealso cref="IQrCodeProvider"/>
public string GetMimeType() => "image/png";
}