Introduction
A barcode is the representation of a numeric or alphanumeric data in the form of a symbol consisting of bars and spaces whose thickness varies according to the symbology used and the data thus encoded.
There are two general types of barcodes:
A barcode is the representation of a numeric or alphanumeric data in the form of a symbol consisting of bars and spaces whose thickness varies according to the symbology used and the data thus encoded.
There are two general types of barcodes:
- Unidimensional (1D)
These codes are those represented by a series of parallel lines of variable thickness. Their reading is one-dimensional. Depending on the reading technology used, the decoding may be unidirectional or bi-directional to confirm the first decoding.
So, this is an example of BarCode which contains the information: http://www.c-sharpcorner.com
- Two-dimensional (2D)
These codes use a variety of symbols (rectangles, dots, hexagons, and other geometric shapes). This matrix form makes it possible to record more information which is also called QR codes And this is an example of a QR code which contains the same information of the previous BarCode.
- Create a BarCode / QrCode scanner.
- Generate a QrCode / BarCode.
Prerequisites
- Basic programming knowledge of C#.
- Basic knowledge of Xamarin.
After creating a cross-platform project, we should install the NuGet package in the solution
: ZXing.Net.Mobile.Forms (Right-click on the solution / Manage NuGet Packages for Solution

On the "MainPage" we add two buttons: The first will open the BarCode / QrCode Scanner and the other button will generate a QrCode
We will back later to add button's click events.
- <Button Text="Scan with Custom Page" Clicked="ScanCustomPage"></Button>
- <Button Text="Barcode Generator" Clicked="GenerateBarcode"></Button>
After that, we add a new ContentPage element which named "ScannerPage" On the ScannerPage, you should add a xmlns attribute for the ContentPage Element to load missing assemblies from the installed NuGet.
So, we add this attribute line inside the ContentPage,
xmlns:zxing="clr-namespace:ZXing.Net.Mobile.Forms;assembly=ZXing.Net.Mobile.Forms"
After adding the assembly, we create a Grid element inside our page which contains two elements: the ScannerView and the Overlay
- <Grid>
- <zxing:ZXingScannerView x:Name="zxing" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" OnScanResult="ZXingScannerView_OnOnScanResult" />
- <zxing:ZXingDefaultOverlay x:Name="overlay" TopText="Hold your phone up to the barcode" BottomText="Scanning will happen automatically" ShowFlashButton="True" FlashButtonClicked="Overlay_OnFlashButtonClicked" />
- </Grid>
Now, we move to the code behind of the "ScannerPage" and we add the actions of the scanner and the overlay,
- private async void ZXingScannerView_OnOnScanResult(Result result) {
- zxing.IsAnalyzing = false;
- await DisplayAlert("Scanned Barcode", result.Text, "OK");
- await Navigation.PopAsync();
- }
- private void Overlay_OnFlashButtonClicked(Button sender, EventArgs e) {
- zxing.IsTorchOn = !zxing.IsTorchOn;
- }
And we override OnAppearing and OnDisappearing methods to start and stop the scanner. Finally, we create the last page to generate a QrCode : this page will be named "QrCodePage".
On this page, we also load the Zxing assembly by adding the same xmlns element of the scanner page. Then, we add a "ZXingBarcodeImageView" inside a StackLayout element,
On this page, we also load the Zxing assembly by adding the same xmlns element of the scanner page. Then, we add a "ZXingBarcodeImageView" inside a StackLayout element,
Don't forget to add the button's events of the MainPage,
- <StackLayout HorizontalOptions="Center" VerticalOptions="Center">
- <zxing:ZXingBarcodeImageView BarcodeFormat="QR_CODE" BarcodeValue="http://www.c-sharpcorner.com" />
- </StackLayout>
- private async void ScanCustomPage(object sender, EventArgs e) {
- await Navigation.PushAsync(new ScannerPage());
- }
- private async void GenerateBarcode(object sender, EventArgs e) {
- await Navigation.PushAsync(new QrCodePage());
- }
Now, we should enable Permission for Android application,
We open the AndroidManifest file and add the following permissions inside the manifest Tag,
We open the AndroidManifest file and add the following permissions inside the manifest Tag,
- <uses-permission android:name="android.permission.CAMERA" />
- <uses-permission android:name="android.permission.FLASHLIGHT" />

The last step is opening MainActivity Class on the Android project and adding the following line on the OnCreate method (just before the LoadApplication call ),
- global::ZXing.Net.Mobile.Forms.Android.Platform.Init();
In this way, you can create a BarCode / QrCode Scanner and generator using Xamarin Forms.

akim mizamPosted Feb 20, 2020, 9:19 PM
How to add timestamp and refresh function into the qr code generator? Sorry to disturb i'm newbie in xamarin
Irfan RanaPosted Feb 9, 2018, 8:43 AM
Kindly give complete source code. here
Slim HammamiPosted Feb 7, 2018, 3:09 AM
You can download the project from my GitHub Repository :https://github.com/slim8791/BarCodeScanner-Xamarin-Forms
Sagar Pandurang KapPosted Feb 4, 2018, 10:59 PM
It is helpful.I will try.Good going..
Naresh SinghalPosted Feb 1, 2018, 10:22 PM
Helpful....................thanks..