How To Use Zxing Intent Integrator In Xamarin.Android

How To Use Zxing Intent Integrator In Xamarin.Android
 
Introduction

In this article, we will see how to use Zxing Intent Integrator in Xamarin Android Applications. We have seen a lot of articles about how to integrate Zxing Plugin in Android & iOS Applications. But the integration of Zxing Intent Integrator is available for Java Android Apps only. So, I have created a Xamarin Library for Xamarin.Android to Zxing Intent Integrator.

Zxing Intent Integrator

This plugin is a port of the official Zxing Intent Integrator and it is not just a binding project. I have converted the library code from Java to C#. We will see how to use it in the coding part.

Steps

I have split this article into 3 steps as follows.

  • Step 1: Creating new Xamarin.Android Projects.
  • Step 2: Setting up the plugin for Xamarin.Android Application.
  • Step 3: Implementing Zxing Intent Integrator in Xamarin.Android Application.

Step 1 - Creating new Xamarin.Android Projects

Create New Project by Selecting New - Project - Select Android App and Click OK.

How To Use Zxing Intent Integrator In Xamarin.Android 

Step 2 - Setting up the plugin for Xamarin.Android Application

In this step, we will include the Zxing plugin for Xamarin.Android Project.

  1. You can download the plugin by clicking here.
  2. Right click on the reference and click add reference.
  3. Then click browse and go to the folder to choose the plugin you downloaded.

How To Use Zxing Intent Integrator In Xamarin.Android 

How To Use Zxing Intent Integrator In Xamarin.Android
 
Step 3 - Implementing Zxing Intent Integrator in Xamarin.Android Application
  1. I have created a button and added a click event.
  2. Then added OnActivityResult Override method as shown below.
    1. protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)  
    2.         {  
    3.   
    4.             IntentResult result = IntentIntegrator.ParseActivityResult(requestCode, (int)resultCode, data);  
    5.             if (result != null)  
    6.             {  
    7.                 if (result.Contents == null)  
    8.                 {  
    9.                     Log.Debug("MainActivity""Cancelled scan");  
    10.                     Toast.MakeText(this"Cancelled", ToastLength.Long).Show();  
    11.                 }  
    12.                 else  
    13.                 {  
    14.                     Log.Debug("MainActivity""Scanned");  
    15.                     Toast.MakeText(this"Scanned: " + result.Contents, ToastLength.Long).Show();  
    16.                 }  
    17.             }  
    18.             else  
    19.             {  
    20.                 base.OnActivityResult(requestCode, resultCode, data);  
    21.             }  
    22.         }  
  1. Here we can handle the results returned from Zxing Scanner App.
  2. Then add the following lines in click event, which is help us to call the Zxing app to scan the barcodes.
    1. button.Click += (s, e) =>  
    2. {  
    3.     IntentIntegrator intentIntegrator = new IntentIntegrator(this);  
    4.     intentIntegrator.InitiateScan();  
    5. };  
  1. The working principle is similar to the official Zxing Java Library.

    Full Code

    You can find the full code here.
    1. namespace ZxingSample  
    2. {  
    3.     [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]  
    4.     public class MainActivity : AppCompatActivity  
    5.     {  
    6.         protected override void OnCreate(Bundle savedInstanceState)  
    7.         {  
    8.             base.OnCreate(savedInstanceState);  
    9.   
    10.             LinearLayout ll = new LinearLayout(this);  
    11.             Button button = new Button(this);  
    12.             button.Text = "Click to Scan using Zxing";  
    13.             button.Click += (s, e) =>  
    14.             {  
    15.                 IntentIntegrator intentIntegrator = new IntentIntegrator(this);  
    16.                 intentIntegrator.InitiateScan();  
    17.             };  
    18.             LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(Android.Views.ViewGroup.LayoutParams.MatchParent,  
    19.                 Android.Views.ViewGroup.LayoutParams.WrapContent);  
    20.             ll.AddView(button, lp);  
    21.             SetContentView(ll);  
    22.         }  
    23.   
    24.         protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)  
    25.         {  
    26.   
    27.             IntentResult result = IntentIntegrator.ParseActivityResult(requestCode, (int)resultCode, data);  
    28.             if (result != null)  
    29.             {  
    30.                 if (result.Contents == null)  
    31.                 {  
    32.                     Log.Debug("MainActivity""Cancelled scan");  
    33.                     Toast.MakeText(this"Cancelled", ToastLength.Long).Show();  
    34.                 }  
    35.                 else  
    36.                 {  
    37.                     Log.Debug("MainActivity""Scanned");  
    38.                     Toast.MakeText(this"Scanned: " + result.Contents, ToastLength.Long).Show();  
    39.                 }  
    40.             }  
    41.             else  
    42.             {  
    43.                 base.OnActivityResult(requestCode, resultCode, data);  
    44.             }  
    45.         }  
    46.     }  
    47. }  

Demo

How To Use Zxing Intent Integrator In Xamarin.Android

How To Use Zxing Intent Integrator In Xamarin.Android
 
How To Use Zxing Intent Integrator In Xamarin.Android
 
How To Use Zxing Intent Integrator In Xamarin.Android

Download Code

You can download the full source code from GitHub. If you like the post, do like and share the article and star the repo on GitHub.


Similar Articles