Using ZXing (Code 128 Scanner) In Xamarin Forms

Code to scan barcode code 128. In this blog, you will learn ZXing (Code 128 Scanner) in Xamarin Forms.

Steps to create a demo to scan barcode code 128.

Open a new shared Xamarin App. First update Xamarin Forms NuGet package form “Manage NuGet Packages For Solution”. 

Using ZXing (Code 128 Scanner) in Xamarin Forms

 

Using ZXing (Code 128 Scanner) in Xamarin Forms

For using Zxing in Xamarin Forms we have to first add NuGet to both the projects, which are Android and iOS.

Using ZXing (Code 128 Scanner) in Xamarin Forms

After adding NuGet to our project in the Android project open “MainActivity.cs” and add the below code to the class.

Using ZXing (Code 128 Scanner) in Xamarin Forms

For iOS projects open AppDelegate.cs and add the below code to the class.

Using ZXing (Code 128 Scanner) in Xamarin Forms

Now we can use ZXing in our project.

Add a new content page (C#). We can create the  UI in C#.

For this go to the solution and right click and go to the new items.

Using ZXing (Code 128 Scanner) in Xamarin Forms

Choose Content Page(C#) and give the name to the page by entering it in the text box and clicking Add button.

Using ZXing (Code 128 Scanner) in Xamarin Forms

Add Zxing.Net.Mobile.Forms namespace into the project page. And write the following code into your page.

Using ZXing (Code 128 Scanner) in Xamarin Forms
 
Using ZXing (Code 128 Scanner) in Xamarin Forms

  1. publicclassscanBarCode: ContentPage {  
  2.     StackLayout stkMainlayout;  
  3.     public scanBarCode() {  
  4.         stkMainlayout = new StackLayout {  
  5.             HorizontalOptions = LayoutOptions.FillAndExpand,  
  6.                 VerticalOptions = LayoutOptions.FillAndExpand,  
  7.                 Orientation = StackOrientation.Vertical  
  8.         };  
  9.         ZXingScannerPage scanPage;  
  10.         Button btnScan = new Button {  
  11.             Text = "Scan", HorizontalOptions = LayoutOptions.Center, VerticalOptions = LayoutOptions.Center  
  12.         };  
  13.         btnScan.Clicked += async (a, s) => {  
  14.             scanPage = new ZXingScannerPage();  
  15.             scanPage.OnScanResult += (result) => {  
  16.                 scanPage.IsScanning = false;  
  17.                 Device.BeginInvokeOnMainThread(async () => {  
  18.                     await Navigation.PopModalAsync();  
  19.                     await DisplayAlert("Scanned Barcode", result.Text + " , " + result.BarcodeFormat + " ," + result.ResultPoints[0].ToString(), "OK");  
  20.                 });  
  21.             };  
  22.             await Navigation.PushModalAsync(scanPage);  
  23.         };  
  24.         stkMainlayout.Children.Add(btnScan);  
  25.         Content = stkMainlayout;  
  26.     }  
  27. }  
  28. }  

After adding this code in your page go your AndroidManifest.xml file from properties in your Android project. And add both permissions (CAMERA and FLASHLIGHT).

Using ZXing (Code 128 Scanner) in Xamarin Forms

  1. <uses-permissionandroid:name="android.permission.CAMERA" />  
  2. <uses-permissionandroid:name="android.permission.FLASHLIGHT" />  

After adding this permission run your project . Now, your app is ready for a scan bar code.

Using ZXing (Code 128 Scanner) in Xamarin FormsUsing ZXing (Code 128 Scanner) in Xamarin Forms
 
Now your app is ready for work.