QR Code Scanner In Xamarin.Forms

Introduction

This article demonstrates how to make a QR CodeScanner Application using C# and XAML in Xamarin.Forms.

QR codes are plastered on advertisements, billboards, business windows, and products. They appear to be very popular among marketers, although it’s rare to see anyone actually scanning one.

These barcodes can be captured with a smartphone camera — for example, a typical QR code may contain an URL. Scan the QR code with a mobile phone and you’ll be taken to the website the QR code specifies.

 

Let’s Start,

Step 1

Open Visual Studio and Go To New Project >> installed >> Visual C# >> Cross-Platform.

Select Cross-Platform App, then give Your Project a Name and Location.

 

Step 2

After Project Creation, add the Following Nuget Packages to Your Project.

• ZXing.Net.Mobile 

Go to Solution Explorer and Select Your Solution. Right-Click and Select "Manage Nuget Packages for Solution".

 

Now, Select the Folllowing Nuget Package and Select your project to Install it.

 

Step 3

Open Solution Explorer >> Project Name >> Mainpage.xaml. Open the Design View of this page.

 

The Code Is Given Below.

 

Xaml Code

  1. <StackLayout Spacing="10">  
  2.         <Button Text="Click"  
  3.                 x:Name="btnScan"  
  4.                 Clicked="btnScan_Clicked"/>  
  5.         <Entry x:Name="txtBarcode"  
  6.                Placeholder="Text Do scan"/>  
  7.     </StackLayout>  
 

Step 4

Next, go to Project Name >> Add >> NewFolder. Select the NewFolder, the Dialogue Box Will Open, Just Give the Folder Name.

 

 Now add a New Interface on Service Folder.

 
Add In The Service Folder
 

Step 5

Next, go to Project Name (Portable) >> Services >> IQrScanningService.cs. Open the Design View Of this Page.

 

The Code is given below

 

C# Code

  1. public interface IQrScanningService  
  2.    {  
  3.        Task<string> ScanAsync();  
  4.    }  

Step 6

Next, go to Project Name (Android) >> Add >> NewFolder. Select the NewFolder, the dialogue box will open, just give the folder name.

 

Now add a New Class on Service Folder.

 

Step 7

Next, go to Project Name (Android) >> Services >> IQrScanningService.cs. Open the design view of this page.

 

The Code Is given below

 

C# Code

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using Android.App;  
  7. using Android.Content;  
  8. using Android.OS;  
  9. using Android.Runtime;  
  10. using Android.Views;  
  11. using Android.Widget;  
  12. using XFBarcode.Services;  
  13. using ZXing.Mobile;  
  14. using Xamarin.Forms;  
  15.   
  16. [assembly: Dependency(typeof(XFBarcode.Droid.Services.QrScanningService))]  
  17.   
  18. namespace QR_Code_Scanner.Droid.Services  
  19. {  
  20.     public class QrScanningService : IQrScanningService  
  21.     {  
  22.         public async Task<string> ScanAsync()  
  23.         {  
  24.             var optionsDefault = new MobileBarcodeScanningOptions();  
  25.             var optionsCustom = new MobileBarcodeScanningOptions();  
  26.   
  27.             var scanner = new MobileBarcodeScanner()  
  28.             {  
  29.                 TopText = "Scan the QR Code",  
  30.                 BottomText = "Please Wait",  
  31.             };  
  32.   
  33.             var scanResult = await scanner.Scan(optionsCustom);  
  34.             return scanResult.Text;  
  35.         }  
  36.     }  
  37. }  
Step 8

Next, Open Solution Exlorer >> Project Name >> MainPage.xaml.cs. Then, click to replace the C# code. 

 
the code Is given below,

 
 
C# Code
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using Xamarin.Forms;  
  7. using XFBarcode.Services;  
  8.   
  9. namespace QR_Code_Scanner  
  10. {  
  11.     public partial class MainPage : ContentPage  
  12.     {  
  13.         public MainPage()  
  14.         {  
  15.             InitializeComponent();  
  16.         }  
  17.   
  18.         private async void btnScan_Clicked(object sender, EventArgs e)  
  19.         {  
  20.             try  
  21.             {  
  22.                 var scanner = DependencyService.Get<IQrScanningService>();  
  23.                 var result = await scanner.ScanAsync();  
  24.                 if (result != null)  
  25.                 {  
  26.                     txtBarcode.Text = result;  
  27.                 }  
  28.             }  
  29.             catch (Exception ex)  
  30.             {  
  31.   
  32.                 throw;  
  33.             }  
  34.         }  
  35.     }  
  36. }  

Step 9

Press F5 or build and run the application

OutPut 1

Run the application and click the button

 

Output 2

Scan the QR Scanner

 

The result will be displayed.

Finally, we have successfully created a Xamarin.Forms QR code scanner app.

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now