Floating FB popout byF5debug

Programmatically Generate a QR code in Windows Phone 8

 

In this article we are going to see a much interesting topic on how to Programmatically create a QR code image in your Windows Phone application, which is a must for any business that runs a show in showcasing the latest and the greatest to the community and the outside world. To give an insight, QR Codes are Quick Response codes as the name speaks its a machine readable language which is unique and which machine reads and do operate based on the information it understands. In common, each and every transaction which needs to be tracked electronically can be accessed with a QR code.

Now with Windows Phone, how we can have a QR Code generated automatically based on some action or to assign a transaction. Lets not over complicate it, lets take a simple scenario of how to first generate a QR code which does the process of electronic responsiveness. For Windows Phone 8 there is a free component called “ZXing.Net” which reduces our hassles of generating a QR code with some simple and easy steps. We are going to see how to use this third party control and generate a QR code easily.

Steps to Generate QR Code:

Open Visual Studio and open the project which needs to have the QR code generated. Let us open a new project and assign a simple button and a image where button is used to trigger the event to generate an action to generate the QR code and the image to show the QR code to the users. Now to use the third party component ZXing.Net, go to http://zxingnet.codeplex.com/ and download the dll and refer it to the project solution file as a reference.

image

Now in the page where you want the QR code to be generated, first add the namespace for ZXing.net and Imaging since we need to generate the QR code and then assign the code as an image to the image control which is placed below the button. So add the below 2 namespaces

using System.Windows.Media.Imaging;
using ZXing;

Now add the below code which is basically a method which is called as and when a QR Code should be generated from an application. If in case we need to have static QR code we can pass a static hard coded value and there is a possible way the QR code remains the same. In case for each of the transaction you need to have different QR codes then you need to assign the value to the method so that you can generate the QR code based on the value passed. In the below code we create an instance of BarcodeWriter class and assign the properties that are very much required in getting the Barcode image generated. The write method has an input value hardcoded (“Karthikeyan Anbarasan”) which we can pass on a dynamic value and get the image as per the passed value. At the background they have different decoders which uses an algorithm (machine learning) to get this operated and understandable by the system.

Code:

private void GenerateQR_Click(object sender, RoutedEventArgs e)
        {
            var image = GenerateQR();
            codeImage.Source = image;
        }

static WriteableBitmap GenerateQR()
        {
            BarcodeWriter BcWriter = new BarcodeWriter();           
            BcWriter.Format = BarcodeFormat.QR_CODE;
            BcWriter.Options.Height = 350;
            BcWriter.Options.Width = 350;
            BcWriter.Options.Margin = 1;

            var barcodeImage = BcWriter.Write("Karthikeyan Anbarasan");

            return barcodeImage;
        }

 

image

 

 

Now lets run the application and try to generate the QR code for the name which is hardcoded, for running the application try using Emulator or the device directly (if device then the device need to be unlocked) and generate the QR Code

imageimage

 

Now we can see the QR code is automatically generated with the help of the third party component, lets not consider much. Now we need to add some more stuffs like we need to change the color of the Bar code that I want to have in some other color to the code. Add the below lines of code in the generate QR method as shown in the screen shot below.

Code:

BcWriter.Renderer = new ZXing.Rendering.WriteableBitmapRenderer()
            {
                Foreground = System.Windows.Media.Color.FromArgb(100, 150, 200, 250)
            };

image

 

Now let us run the application and see the result in very colourful way as shown in the screens below. Likewise we can operate in such a way we can customize and have our own properties and extended method as the third party component is available as a free open source license.

image

About Author: Karthikeyan Anbarasan, Microsoft MVP (Most Valuable Professional) in ASP.NET/IIS Architecture. He is the Founder and Chief Editor of the www.f5debug.net website and has authored books on Windows Phone and Business Intelligence(SSIS). He is also a Passionate Speaker and a Blogger on Microsoft Technologies.

You can Join Me On: Facebook, Twitter, Google Plus, LinkedIn

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Recent Posts

Random Posts

Most Viewed

URL Shortner

Create your own short urls!!!

Site Counter

free counters

Disclaimer

This is a personal weblog. The opinions expressed here represent my own and not those of my employer. For accuracy and official reference refer to MSDN/ TechNet. I have documented my personal experience on this blog.

Protected by Copyscape Web Plagiarism Finder

Site Meter