This article will cover the following.
- Basic programming knowledge of C#.
- Basic knowledge of Xamarin.
Implementation
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
- <Button Text="Scan with Custom Page" Clicked="ScanCustomPage"></Button>
- <Button Text="Barcode Generator" Clicked="GenerateBarcode"></Button>
We will back later to add button's click events.
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,
- <StackLayout HorizontalOptions="Center" VerticalOptions="Center">
- <zxing:ZXingBarcodeImageView BarcodeFormat="QR_CODE" BarcodeValue="http://www.c-sharpcorner.com" />
- </StackLayout>
Don't forget to add the button's events of the MainPage,
- 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,
- <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.