For creating a Barcode Scanner, I will use the NuGet package Arc.BarCodes, a cross-platform creator built on top of ZXing.Net.Mobile to allow for easy cross-platform access from Shared/PCL core libraries.
Now we can set some permissions for Android and Windows to get access to the camera. This is for taking the clear picture of the barcode that we want to scan.
For Android open the AndroidManifest.xml and select CAMERA from the Required Permissions.
Now open WMAppManifest.xml for setting permissions to Windows Phone then select ID_CAP_ISV_CAMERA from Capabilities.
Now we want to initialize the corresponding NuGet package in each platform-specific startup classes. For example, AppDelegate.cs for iOS, MainActivity.cs for Android and MainPage.xaml.cs for Windows Phone by calling global::Acr.BarCodes.BarCodes.Init(); before calling LoadApplication();
Now create a button on the page for the Barcode Scanner. Write the following code in the click event of the button so that we can scan the barcode on that button click.
- Button scanBtn = new Button
- {
- Text = "Scan Barcode", HorizontalOptions = LayoutOptions.FillAndExpand,
- };
- scanBtn.Clicked += async(sender, args) = >
- {
- var scanResult = await Acr.BarCodes.BarCodes.Instance.Read();
- if (!scanResult.Success)
- {
- await this.DisplayAlert("Alert ! ", "Sorry ! \n Failed to read the Barcode !", "OK");
- }
- else
- {
- await this.DisplayAlert("Scan Successful !", String.Format("Barcode Format : {0} \n Barcode Value : {1}", scanResult.Format, scanResult.Code), "OK");
- }
- };
You can refer to the sample project attached with this for more details.