Bar code or QR code scanner in Xamarin Android App Development
QR codes are now a big step in manufacturing management, and they’ve gained more revenue in advertising, marketing and networking. QR Codes, barcodes are all systems for conveying large amounts of data in a small format. QR code short Quick Response Code is a two-dimensional matrix type barcode that can store more than 4,000 alphanumeric characters in a limited horizontal and vertical space. A traditional linear barcode can hold roughly 20 horizontal characters. A recent trend for business environment is to use Bar code and QR code which contain information that can be accessed by the Code Reader. Now a days it has become possible to read QR code or Bar code through certain applications used in smart phone. Here comes my contribution in this particular domain. People can easily scan a code with the help ofThis Android application and can get access to a lot of information electronically.
To read Bar code and QR code by Code Scanner and fetch that result to Google for getting information about that product.
IDE: Visual Studio Xamarin or Xamarin Studio.
Plugin required- Acr.Barcode or ZXing.Net.Mobile.
There are some difference between Bar code and QR code scanning.
- By scanning a Bar code you will get some digits of a number which is a unique code of a particular product. If you search that product number in Google you will get information about that particular product. In this blog you will know how to build an app through which you will get that product code from Barcode and search it in Google to get information. After bar code scanning this App will redirect to the Google search platform.
- By scanning QR code you can get some messages or product name along with some URL. After scanning QR code this App will extract only the URL part of that code behind information and redirect to that link for getting information about that product.
At first Add this Plugin –ZXing.Net.Mobile in your xamarin project.
ZXing is an independent library which is basically use to parsing differnt types of barcode and QR code from images.After adding this library one plugin will be automatically updated-'zxing.monoandroid'.
You can add this plugin from following two processes. You can apply any one of them:
1. Reference=> Right click=>Manage Nuget Packages=>Browse=>type in Search : ZXing.Net.Mobile=> Install.
After successfull installation it will show in Installed tabs.
2. Components=> Right click=> Get more components=> type in Search: : ZXing.Net.Mobile=> Add to project.
After successfull installation all required dll will show under References.
In mainactivity you need to add this assembly reference fo scanning code- using ZXing.Mobile;
Be sure that Camera, internet and WIFI option is activated in your Android Manifest files like this:
Create two buttons in your Main Layout. One is Scan barcode button and another is Product Detail button. Scan code button will start immediately to scan the code and store the result. Product details button fetch this result to Google for searching information about that product code.
Declare MobileBarcodeScanner class and initialize it to Mainactivity.cs page. Set a toptext message as you want. In your Scan button click event add this following codes:
scanner = new MobileBarcodeScanner(this);
scanBtn.Clicked += async (sender, args) =>
scanner.TopText = "Scanning QR Code...";
var scanResult = await scanner.Scan();//Scan code response
Toast.MakeText(this,"Sorry!Failed to read the Barcode!, ToastLength.Long).Show());
Regex linkParser = new Regex(@"\b(?:https?://|www\.)\S+\b", RegexOptions.IgnoreCase);//extract only url part from scancode information for QR code
foreach(Match m in linkParser.Matches(totalurl))
lblurl.Text="https://www.google.co.in/search?q="+scanResult.Code;// For Bar code
- Above Code we will get entire scan response in await scanner.scan portion.
- If scanning of that code fails to read barcode then a toast message will be displayed.
- Else we will get a scanresult format. If the code is QRcode, then through Regex it will extract only the URL part of the scan code and save it to a label or any other variable.In above code it is lblurl.Text.
- If the code is Barcode then link the code with google url format and save it to a label or any other variable.
- Be sure that you are using a smart phone with Camera fatures to run this App.
- You need to test this code in real device. If you test this in emulator without perfect settings you may get blank screen. So better to test in real devices.
In your Product details button click event add this following codes:
rdrBtn.Clicked+= async(sender, e) => //Product Details Button
your saved URL open in device for searching information about product code.
In this blog I have explained implementation of Barcode and QR code scanner technique in several steps and also fetch it to Google search to get adequate information about that product. One thing needs to be remembered that the red line must be placed in the middle of the Bar code or QR code and the focus must be given on the current field. You can change UI design of that scanning screen by making it more customize.
My name is Rosina De Palma and I am a technical writer at Nex Mobility. I have more than 6 years of experience to write articles especially for iOS, Android and Xamarin. I want to learn android app development.