Create a Barcode Scanner Using Xamarin.Forms

Barcode Scanner

For creating a Barcode Scanner, I will use the NuGet package Arc.BarCodes, a cross-platform creator built on top of ZXing.Net.Mobile to allow for easy cross-platform access from Shared/PCL core libraries.

ArcBarCodes install

Now we can set some permissions for Android and Windows to get access to the camera. This is for taking the clear picture of the barcode that we want to scan.

For Android open the AndroidManifest.xml and select CAMERA from the Required Permissions.

camera

Now open WMAppManifest.xml for setting permissions to Windows Phone then select ID_CAP_ISV_CAMERA from Capabilities.

mark

Now we want to initialize the corresponding NuGet package in each platform-specific startup classes. For example, AppDelegate.cs for iOS, MainActivity.cs for Android and MainPage.xaml.cs for Windows Phone by calling global::Acr.BarCodes.BarCodes.Init(); before calling LoadApplication();

Now create a button on the page for the Barcode Scanner. Write the following code in the click event of the button so that we can scan the barcode on that button click.

  1. Button scanBtn = new Button   
  2. {  
  3.     Text = "Scan Barcode", HorizontalOptions = LayoutOptions.FillAndExpand,  
  4. };  
  5. scanBtn.Clicked += async(sender, args) = >   
  6. {  
  7.     var scanResult = await Acr.BarCodes.BarCodes.Instance.Read();  
  8.     if (!scanResult.Success)   
  9.     {  
  10.         await this.DisplayAlert("Alert ! ""Sorry ! \n Failed to read the Barcode !""OK");  
  11.     }   
  12.     else   
  13.     {  
  14.         await this.DisplayAlert("Scan Successful !", String.Format("Barcode Format : {0} \n Barcode Value : {1}", scanResult.Format, scanResult.Code), "OK");  
  15.     }  
  16. };  
You can refer to the sample project attached with this for more details.

Up Next
    Ebook Download
    View all
    Learn
    View all