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.
Step 2 - Setting up the plugin for Xamarin.Android Application
In this step, we will include the Zxing plugin for Xamarin.Android Project.
- You can download the plugin by clicking here.
- Right click on the reference and click add reference.
- Then click browse and go to the folder to choose the plugin you downloaded.
Step 3 - Implementing Zxing Intent Integrator in Xamarin.Android Application
- I have created a button and added a click event.
- Then added OnActivityResult Override method as shown below.
- protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
- {
-
- IntentResult result = IntentIntegrator.ParseActivityResult(requestCode, (int)resultCode, data);
- if (result != null)
- {
- if (result.Contents == null)
- {
- Log.Debug("MainActivity", "Cancelled scan");
- Toast.MakeText(this, "Cancelled", ToastLength.Long).Show();
- }
- else
- {
- Log.Debug("MainActivity", "Scanned");
- Toast.MakeText(this, "Scanned: " + result.Contents, ToastLength.Long).Show();
- }
- }
- else
- {
- base.OnActivityResult(requestCode, resultCode, data);
- }
- }
- Here we can handle the results returned from Zxing Scanner App.
- Then add the following lines in click event, which is help us to call the Zxing app to scan the barcodes.
- button.Click += (s, e) =>
- {
- IntentIntegrator intentIntegrator = new IntentIntegrator(this);
- intentIntegrator.InitiateScan();
- };
- The working principle is similar to the official Zxing Java Library.
Full Code
You can find the full code here.
- namespace ZxingSample
- {
- [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
- public class MainActivity : AppCompatActivity
- {
- protected override void OnCreate(Bundle savedInstanceState)
- {
- base.OnCreate(savedInstanceState);
-
- LinearLayout ll = new LinearLayout(this);
- Button button = new Button(this);
- button.Text = "Click to Scan using Zxing";
- button.Click += (s, e) =>
- {
- IntentIntegrator intentIntegrator = new IntentIntegrator(this);
- intentIntegrator.InitiateScan();
- };
- LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(Android.Views.ViewGroup.LayoutParams.MatchParent,
- Android.Views.ViewGroup.LayoutParams.WrapContent);
- ll.AddView(button, lp);
- SetContentView(ll);
- }
-
- protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
- {
-
- IntentResult result = IntentIntegrator.ParseActivityResult(requestCode, (int)resultCode, data);
- if (result != null)
- {
- if (result.Contents == null)
- {
- Log.Debug("MainActivity", "Cancelled scan");
- Toast.MakeText(this, "Cancelled", ToastLength.Long).Show();
- }
- else
- {
- Log.Debug("MainActivity", "Scanned");
- Toast.MakeText(this, "Scanned: " + result.Contents, ToastLength.Long).Show();
- }
- }
- else
- {
- base.OnActivityResult(requestCode, resultCode, data);
- }
- }
- }
- }
Demo
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.