-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathQrCodeController.cs
64 lines (58 loc) · 2.58 KB
/
QrCodeController.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
//
// QR code generator library (.NET)
// https://github.com/manuelbl/QrCodeGenerator
//
// Copyright (c) 2021 Manuel Bleichenbacher
// Licensed under MIT License
// https://opensource.org/licenses/MIT
//
using Microsoft.AspNetCore.Mvc;
using System;
using System.Text;
namespace Net.Codecrete.QrCodeGenerator.Demo
{
/// <summary>
/// Controller for generating QR code as PNG or SVG images
/// </summary>
[ApiController]
public class QrCodeController : ControllerBase
{
private static readonly QrCode.Ecc[] errorCorrectionLevels = { QrCode.Ecc.Low, QrCode.Ecc.Medium, QrCode.Ecc.Quartile, QrCode.Ecc.High };
/// <summary>
/// Generates QR code as PNG image
/// </summary>
/// <param name="text">Text to encode in QR code</param>
/// <param name="ecc">Error correction level (0: low ... 3: high)</param>
/// <param name="borderWidth">Border width in multiples of a module (QR code pixel)</param>
/// <returns>PNG image</returns>
[HttpGet("qrcode/png")]
[ResponseCache(Duration = 2592000)]
public ActionResult<byte[]> GeneratePng([FromQuery(Name = "text")] string text,
[FromQuery(Name = "ecc")] int? ecc, [FromQuery(Name = "border")] int? borderWidth)
{
ecc = Math.Clamp(ecc ?? 1, 0, 3);
borderWidth = Math.Clamp(borderWidth ?? 3, 0, 999999);
var qrCode = QrCode.EncodeText(text, errorCorrectionLevels[(int)ecc]);
byte[] png = qrCode.ToPng(20, (int)borderWidth);
return new FileContentResult(png, "image/png");
}
/// <summary>
/// Generates QR code as SVG image
/// </summary>
/// <param name="text">Text to encode in QR code</param>
/// <param name="ecc">Error correction level (0: low ... 3: high)</param>
/// <param name="borderWidth">Border width in multiples of a module (QR code pixel)</param>
/// <returns>SVG image</returns>
[HttpGet("qrcode/svg")]
[ResponseCache(Duration = 2592000)]
public ActionResult<byte[]> GenerateSvg([FromQuery(Name = "text")] string text,
[FromQuery(Name = "ecc")] int? ecc, [FromQuery(Name = "border")] int? borderWidth)
{
ecc = Math.Clamp(ecc ?? 1, 0, 3);
borderWidth = Math.Clamp(borderWidth ?? 3, 0, 999999);
var qrCode = QrCode.EncodeText(text, errorCorrectionLevels[(int)ecc]);
byte[] svg = Encoding.UTF8.GetBytes(qrCode.ToSvgString((int)borderWidth));
return new FileContentResult(svg, "image/svg+xml; charset=utf-8");
}
}
}