BarcodeScanner using GoogleVision API for Xamarin Form Works on Android and iOS 10+
For Android, it use Xamarin.GooglePlayServices.Vision For iOS, it use GoogleMobileVision under MLKit library
Please feel free to improve my source code.
- CameraView is not shown in Android 10 device
- Added ask permission methods
- Added IsTorchOn methods
- Added DefaultTorchOn property
- Moved some code on iOS to UIThread
- Added VibrationOnDetected property
- Run OnDetected event on UIThread
- Added 1 second interval on iOS scanning to solve double scanning problem
- Allow continuous scanning
- Auto switch off flashlight when page dispose
- Install Nuget package to Forms, Android and iOS project
Install-Package BarcodeScanner.XF
- Manifest.xml
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-permission android:name="android.permission.CAMERA" />
- Init the library in MainActivity.cs
base.OnCreate(savedInstanceState);
...
GoogleVisionBarCodeScanner.Droid.RendererInitializer.Init();
Plugin.CurrentActivity.CrossCurrentActivity.Current.Init(this, savedInstanceState);
...
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
Plugin.Permissions.PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
- Edit Info.plist, add camera rights
<key>NSCameraUsageDescription</key>
<string>Require to use camera</string>
-
Create an project in Google Firebase Console, download GoogleService-Info.plist https://console.firebase.google.com/
-
Put GoogleService-Info.plist into Resources folder of iOS project, set Build Action as BundleResource
-
Init project and firebase on AppDelegate.cs
global::Xamarin.Forms.Forms.Init();
LoadApplication(new App());
....
GoogleVisionBarCodeScanner.iOS.Initializer.Init();
Firebase.Core.App.Configure();
....
return base.FinishedLaunching(app, options);
- Set support barcode format (Default is all), call it before you start to init CameraView
GoogleVisionBarCodeScanner.Methods.SetSupportBarcodeFormat(BarcodeFormats.QRCode);
- It is all about the camera view, use it on the page.xaml. For now, it will spend your whole width of the screen and the height will be equal to width.
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:gv="clr-namespace:GoogleVisionBarCodeScanner;assembly=GoogleVisionBarCodeScanner"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="SampleApp.Page1">
<ContentPage.Content>
<ScrollView HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<!--If true on DefaultTorchOn, Torch will be on when the UI loaded-->
<gv:CameraView HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" OnDetected="CameraView_OnDetected" Grid.Row="1"
DefaultTorchOn="True" VirbationOnDetected="False"/>
</ScrollView>
</ContentPage.Content>
</ContentPage>
- Once barcode detected, "OnDetected" event will be triggered, do the stuff you want with the barcode, it will contains type and display value
private async void CameraView_OnDetected(object sender, GoogleVisionBarCodeScanner.OnDetectedEventArg e)
{
List<GoogleVisionBarCodeScanner.BarcodeResult> obj = e.BarcodeResults;
string result = string.Empty;
for(int i = 0; i < obj.Count; i++)
{
result += $"{i + 1}. Type : {obj[i].BarcodeType}, Value : {obj[i].DisplayValue}{Environment.NewLine}";
}
Device.BeginInvokeOnMainThread(async() =>
{
await DisplayAlert("Result", result, "OK");
await Navigation.PopModalAsync();
});
}
- To use torch, please call
GoogleVisionBarCodeScanner.Methods.ToggleFlashlight();
- To ask for permission
bool allowed = await GoogleVisionBarCodeScanner.Methods.AskForRequiredPermission();
- To check the condition of torch
GoogleVisionBarCodeScanner.Methods.IsTorchOn();
- Continuous scanning
1. Do not dispose the page after onDetected.
2. Call GoogleVisionBarCodeScanner.Methods.Reset() to scan next.