Add a reference to Esponce.QRCode.dll in your project. The latest version of the file can be downloaded here from CodePlex or using NuGet directly from Visual Studio.

Generate a QR Code
using Esponce.QRCode
...
async void Generate()
{
  var client = new QRCodeClient();
  using (var stream = await client.GenerateAsync("Hello World"))
  {
    //Stream contains PNG image
    //TODO: Do something with the stream...
  }
}

Generate blue QR Code in EPS format and save it to a file
var client = new QRCodeClient();
using (var stream = await client.GenerateAsync("Hello World", "eps", 8, 4, null, QREncodeMode.Byte, QRErrorCorrection.M, Colors.Blue, Colors.White))
{
  var file = await ApplicationData.Current.LocalFolder.CreateFileAsync("hello-world.eps", CreationCollisionOption.ReplaceExisting);
  using (var fs = await file.OpenAsync(FileAccessMode.ReadWrite))
  {
    using (var os = fs.GetOutputStreamAt(0))
    {
      using (var writer = new DataWriter(fs))
      {
        int size = 0;
        var buffer = new byte[1024];
        while ((size = stream.Read(buffer, 0, 1024)) != 0)
        {
          if (size == 1024)
          {
            //Write whole buffer to file
            writer.WriteBytes(buffer);
          }
          else
          {
            //Write only part of buffer
            var temp = new byte[size];
            Array.Copy(buffer, temp, size);
            writer.WriteBytes(temp);
          }
        }

        await writer.StoreAsync();
        writer.DetachStream();
      }
    }

    await fs.FlushAsync();
  }
}

Generate QR Code with fixed dimension (256x256) and display it in UI
var client = new QRCodeClient();

//Parameters
var model = new QRCodeModel()
{
  Content = "Hello World",
  Format = "png",
  ImageSize = new Size(256, 256),
  Background = Colors.Transparent
};

//Generate QR Code image
using (var stream = await client.GenerateAsync(model))
{
  using (var ms = new InMemoryRandomAccessStream())
  {
    int size = 0;
    var buffer = new byte[1024];

    //Copy web stream to memory
    var s = ms.AsStreamForWrite();
    while ((size = stream.Read(buffer, 0, 1024)) != 0)
    {
      s.Write(buffer, 0, size);
    }

    //Rewind the stream back to beginning
    s.Position = 0;

    //Create BitmapImage with QR Code
    var image = new BitmapImage();
    image.SetSource(ms);

    //Attach to the UI
    img.Source = image; //<Image x:Name="img" Stretch="None" />
  }
}

Customizing HTTP request
var client = new QRCodeClient();
client.UseSSL = true; //Use HTTPS
client.ApiKey = "d3m04u7h"; //Get API key at esponce.com
client.Format = ApiFormat.Json; //Transfer data in JSON format
client.ApplicationName = "MyFirstApplication"; //Displayed in User-Agent string
...

Decoding a QR Code image
using System.IO;
using Esponce.QRCode;
...
var client = new QRCodeClient();
var result = await client.DecodeAsync(stream); //Get stream (PNG image) from somewhere

Console.WriteLine(result.Content); //Content from QR Code
Console.WriteLine(result.Dimension);
Console.WriteLine(result.Capacity);

Get list of campaigns and QR Codes
var client = new QRCodeClient();
client.UseSSL = true; //Use HTTPS
client.ApiKey = "d3m04u7h"; //Get API key at esponce.com

var client = new QRCodeClient();
var result = await client.GetTrackListAsync();

foreach (var campaign in result.Campaigns)
{
  //Do something with campaigns...
}

foreach (var qrcode in result.QRCodes)
{
  //Do something with QR Codes...
}

Get scans by country
var client = new QRCodeStatistics("api_key_goes_here");

var scans = await client.GetScansByCountry("qr_code_id_goes_here");
foreach (var scan in scans)
{
  //Displayed as "7: JP (Japan)"
  Console.WriteLine(scan.Count + ": " + scan.CountryCode + "(" + LocationHelper.GetCountryName(scan.CountryCode) + ")");
}

Last edited Oct 9, 2012 at 2:12 PM by Avivo, version 1

Comments

No comments yet.