Skip to content

Instantly share code, notes, and snippets.

@Cheesebaron
Created October 18, 2013 12:25
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Cheesebaron/7040789 to your computer and use it in GitHub Desktop.
Google Charts QR Code generation.
async void Main()
{
var content = "Hello, World!";
var size = QRCodeSize.Large;
var errorCorrection = QRErrorCorrection.H;
var qrUrl = GenerateQRCodeUrl(content, size, errorCorrection);
qrUrl.Dump();
var image = await GetQRCodeImage(qrUrl);
image.Dump();
}
public enum QRCodeSize
{
Small = 120,
Medium = 230,
Large = 350
}
public enum QRErrorCorrection
{
L,
M,
Q,
H
}
private string GenerateQRCodeUrl(string content, QRCodeSize size, QRErrorCorrection errorCorrection)
{
var baseUrl = "http://chart.apis.google.com/chart?cht=qr&chs={0}x{0}&chld={1}&choe=UTF-8&chl={2}";
return string.Format(baseUrl, (int)size, errorCorrection.ToString(), Uri.EscapeDataString(content));
}
private async Task<Image> GetQRCodeImage(string url)
{
using (var client = new HttpClient())
{
var msg = await client.GetAsync(url);
if (msg.IsSuccessStatusCode)
{
using (var stream = await msg.Content.ReadAsStreamAsync())
{
var image = Image.FromStream(stream);
return image;
}
}
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment