-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QRCodeGen.cs
62 lines (52 loc) · 1.95 KB
/
QRCodeGen.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
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Net.Codecrete.QrCodeGenerator;
using System.Net.Http;
using System.Net;
using System.Net.Http.Headers;
namespace QRCodeGen
{
public static class QRCodeGen
{
[FunctionName("Form")]
public static HttpResponseMessage Form(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req, ILogger log, ExecutionContext context)
{
// read in text from a hosted file
string indexPage = File.ReadAllText(context.FunctionAppDirectory + "/www/index.html");
// create a new HTTP Response Message
var result = new HttpResponseMessage(HttpStatusCode.OK);
result.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");
// get all the data, and add it to content
result.Content = new ByteArrayContent(System.Text.Encoding.UTF8.GetBytes(indexPage));
return result;
}
[FunctionName("GenerateQRCode")]
public static async Task<IActionResult> GenerateQRCode(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req, ILogger log)
{
// get QR text from query string
string qrtext = req.Query["qrtext"];
log.LogInformation("Generating QR Code for {0}", qrtext);
var qr = QrCode.EncodeText(qrtext, QrCode.Ecc.Medium);
// convert it into a byte array for PNG output
var pngout = qr.ToPng(10, 1, SkiaSharp.SKColors.Black, SkiaSharp.SKColors.White);
// create a new return object
var ourResult = new ReturnObject { };
// store our byte array as a string
ourResult.Image = Convert.ToBase64String(pngout);
// send it as JSON
return new JsonResult(ourResult);
}
}
public class ReturnObject
{
public string Image { get; set; }
}
}