Pages

Wednesday, 9 January 2019

Using 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”.





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


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


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



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.



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


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




  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).


  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.

Now your app is ready for work.

3 comments:

  1. Much obliged for giving us such astonishing data on the scanner. I am a businessman and I was looking for this kind of data to shape my improvement. Thank you so much. Get lidar Vancouver, BC

    ReplyDelete
  2. This is a brilliant blog! I'm very happy with the comments!.. photo scanner reviews

    ReplyDelete
  3. Nice coding techniques. It is a great way to explain. Keep sharing more articles soon. Now it's time to avail Chukka Boots for more information.

    ReplyDelete