Introduction
With Smart Phones we have many sensors and technologies. Either they can be a Proximity Sensor, Geo Sensor or your Accelerometer. All these are integrated under a single smart phone to make our life truly Smart.
You have done it many times when you are ticketing on the IRCTC or downloading an app by scanning the QR code.
Actually, these are all made to make our life simpler. We will now learn how to make these codes ourselves and implement then in a Windows Phone 8 App prototype.
Background
We are implementing this in a Windows Phone8 App. For the demonstration we need:
- Visual Studio 2013, that supports Windows Phone App development.
- Zxing.dll [http://zxingnet.codeplex.com/] or, get it from NuGet
And the rest you need is basic C# knowledge and patience.
Agenda for this Demonstration
- First, we add a Button and an image from the tool box on Mainpage.xaml page.
- Inside, MainPage.cs we try to make a GenerateQRCode() method that returns the image.
- Before we call the GenearteQRCode() method, we first call the ContactChooser chooser and select a contact for further processing.
- And then select the phone number of that contact and it as a parameter to the GenerateQRCode() method.
- Last, put the returned value of the GenerateQRCode() to source property of the Image.
Procedure
Step 1: Try to make a look like a layout in your Windows Phone Project.
Step 2 : Now, raise the click event of the button. Put these lines of code.
- private void Button_Click(object sender, RoutedEventArgs e)
- {
-
- PhoneNumberChooserTask contacts = new PhoneNumberChooserTask();
- contacts.Completed += contacts_Completed;
- contacts.Show();
- }
-
- void contacts_Completed(object sender, PhoneNumberResult e)
- {
- var image= GenerateQRcode(e.PhoneNumber.ToString());
-
-
- codeImage.Source = image;
- }
Actually, here we are calling the ContactChooser chooser. And, when the task is completed, then we call the GenerateQRcode() method with the selected PhoneNumber in the parameter.
So, how the GenerateQRcode() method behaves. This we will see in the next step.
Step 3
- static WriteableBitmap GenerateQRcode(string phoneNumber)
- {
- BarcodeWriter _writer = new BarcodeWriter();
- _writer.Renderer = new ZXing.Rendering.WriteableBitmapRenderer()
- {
-
- Foreground = System.Windows.Media.Color.FromArgb(255, 0, 0, 255)
- };
- _writer.Format = BarcodeFormat.QR_CODE;
- _writer.Options.Height = 400;
- _writer.Options.Width = 400;
- _writer.Options.Margin = 1;
-
- var barcodeImage = _writer.Write("tel:"+phoneNumber);
-
- return barcodeImage;
- }
Here, we create the BarcodeWriter instance. So, ensure you have ‘using ZXing;" in your namespace list.
Then, assign the Render property with WritableBitmap image.
Confirm the color of QR code in ARGB value. For black you can try (255, 0, 0, 0).
Then, what kind of code format do you want?
So, we want the QR Code here. And the rest is self-explanatory.
In the last, we return the image.
Final Output
ConclusionYou can try to implement more features to it. However, there are not many complications. Still, you have some then get in the project that I have enclosed with this article.