So few weeks back my boss tasked me to implement a barcode scanning feature in one of our enterprise mobile apps which we were building using Xamarin Cross Platform. I got freaked out, as I have never implemented anything related to barcode scanning, not even in my 3 years of Windows Phone dev experience 😛 ! I was worrying whether it would get much messier when it comes to Cross Platform development. 😦
But luckily I was able to implement the task under 15 mins, thanks to the awesomeness of Xamarin. I came across this epic library called ACR Barcodes Plugin for Xamarin Forms, which allowed me to easily implement the code bar code scanning and reading feature.
So with that experience, now I’m gonna show you how you could build a Barcode Scanner App in just 3 Minutes.. 😀 Prepare to be mind blown ! 😉
First create a Xamarin.Forms project (PCL project) solution in Visual Studio and add the ACR Barcodes Plugin using nuget package manager.
Simple implementing in your PCL…
Create a new class in your PCL project called Page_BarcodeScanner. And replace that Class code with the following code.
Now resolve the necessary imports… 😉 And you just created the Bar code scanner code. So let’s set this page to be executed in the App.cs file. Open up the App.cs code file in the PCL project and replace its App() method with the following.
There you go, but wait… Just a little bit more configuring to be done. 😉
Now we have to set the camera access permission and initiate the library on Android/ iOS and WinPhone projects. Nothing to worry its pretty simple.
Setting up on Android..
In your Android project, replace the OnCreate method in the MainActivity.cs code file with the following. This init() call will initialize the library for the application.
Now we need to set the permission for the app to access the Camera on the device, so double click on Properies -> select tab Android Manifest -> Tick on CAMERA permission.
That’s it ! Now you can even set your Android project as the default project and run the application successfully.. 😉 or else you could go ahead with the rest of the configuration on iOS and WinPhone.. 😀
Setting up on iOS…
In you iOS app, replace the FinishedLaunching() method in the AppDelegate.cs code file with the following code, in order to initialize the library for the usage of the application.
That’s it, oh and not to worry you don’t have to set the permission on the iOS project. 😉
Setting up on Windows Phone…
Finally on Windows Phone Project (WinPhone) replace the MainPage() constructor in the MainPage.xaml.cs file with the following code,
Now let’s set the camera permission on Windows Phone as follows…
Expand Properties -> Double click on WMAppManifest.xml -> Go to Capabilities tab -> Tick on ID_CAP_ISV_CAMERA permission
Finally thats it ! 😀 Now you can build the whole project and run the app in any selected platform as you wish… 😀
TADAAAAAAAA ! 😀
Below are some screenshots I took after I deployed in my Windows Phone device. 🙂 Now its all up to you to make any necessary improvements or implement this in any of your awesome apps as you wish. 😉
Keep up the awesomeness ! ^_^