Geeks With Blogs

News Please visit me at my new blog!!

profile for Aligned at Stack Overflow, Q&A for professional and enthusiast programmers
"free in Christ Jesus from the law of sin and death." Romans 8:2 (ESV) Check out the Falling Plates video on YouTube.
more about the Gospel
And then listen to Francis Chan speaking at LifeLight in SD.



Programming and Learning from SD

It took awhile to find the right NuGet package to use to create and decode a QR code, but we found it.

The ZXing.Net port of the Java version turned out to be quick and easy to use. Scott Hanselman had an example of decoding a QR code in his blog, and I adapted the code from the ZXing.Net site and his to create this LinqPad demo. Fire up the great LinqPad program, change to C# statements, add in the Nuget package and the correct namespaces. Then hit F5. You can now generated and decode that information from QR codes in .Net!

// ZXing.Net
// http://zxingnet.codeplex.com/
// http://www.hanselman.com/blog/HowToDisplayAQRCodeInASPNETAndWPF.aspx
// create the bar code/qr code
var fileLocation = @"C:\temp\qr.png";
 var qrValue = "whatever data you want to put in here 121313213";
 var height = 200;
 var width = 200;
 var margin = 1;
var barcodeWriter = new BarcodeWriter
{
  Format = BarcodeFormat.QR_CODE,
  Options = new EncodingOptions
  {
      Height = height,
      Width = width,
      Margin = margin
  }
};

using (var bitmap = barcodeWriter.Write(qrValue))
using (var stream = new FileStream(fileLocation, FileMode.Create))
{
  bitmap.Save(stream, ImageFormat.Png);
}

// read from the bar code
// create a barcode reader instance
var reader = new BarcodeReader();
// load a bitmap
var barcodeBitmap = (Bitmap)Bitmap.FromFile(fileLocation);
// detect and decode the barcode inside the bitmap
var result = reader.Decode(barcodeBitmap);
// do something with the result
if (result != null)
{
   result.BarcodeFormat.ToString().Dump();
   result.Text.Dump();
}
Posted on Friday, March 27, 2015 8:53 PM Tools , .Net | Back to top


Comments on this post: QR codes in .Net

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Aligned | Powered by: GeeksWithBlogs.net