Skip to content

Commit

Permalink
explicitly call mlkit initialize in case provider doesn't start
Browse files Browse the repository at this point in the history
  • Loading branch information
jamie-mh committed Aug 5, 2023
1 parent 927353e commit 07efcb8
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
5 changes: 4 additions & 1 deletion AuthenticatorPro.Droid/Properties/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false"/>
<application android:allowBackup="false" android:label="@string/appName" android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true"
android:theme="@style/AppTheme"></application>
android:theme="@style/AppTheme">
<provider android:name="com.google.mlkit.common.internal.MlKitInitProvider"
android:authorities="me.jmh.authenticatorpro.mlkitinitprovider" tools:node="remove" />
</application>
<supports-screens android:smallScreens="true" android:largeScreens="true" android:xlargeScreens="true"
android:normalScreens="true" android:anyDensity="true"/>
</manifest>
2 changes: 1 addition & 1 deletion AuthenticatorPro.Droid/src/Activity/ScanActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected override async void OnCreate(Bundle savedInstanceState)
.SetBackpressureStrategy(ImageAnalysis.StrategyKeepOnlyLatest)
.Build();

var analyser = new MlKitQrCodeImageAnalyser();
var analyser = new MlKitQrCodeImageAnalyser(this);
analyser.QrCodeScanned += OnQrCodeScanned;
analysis.SetAnalyzer(ContextCompat.GetMainExecutor(this), analyser);
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@

#if !FDROID

using Android.Content;
using Android.Gms.Extensions;
using Android.Runtime;
using Android.Util;
using AndroidX.Camera.Core;
using Java.Lang;
using System;
using System.Linq;
using System.Threading.Tasks;
using Xamarin.Google.MLKit.Common;
using Xamarin.Google.MLKit.Vision.BarCode;
using Xamarin.Google.MLKit.Vision.Barcode.Common;
using Xamarin.Google.MLKit.Vision.Common;
Expand All @@ -26,8 +29,17 @@ public class MlKitQrCodeImageAnalyser : Java.Lang.Object, ImageAnalysis.IAnalyze
private long _lastScanMillis;
private readonly IBarcodeScanner _barcodeScanner;

public MlKitQrCodeImageAnalyser()
public MlKitQrCodeImageAnalyser(Context context)
{
try
{
MlKit.Initialize(context);
}
catch (IllegalStateException e)
{
Logger.Warn("MlKit already initialised", e);
}

var options = new BarcodeScannerOptions.Builder()
.SetBarcodeFormats(Barcode.FormatQrCode)
.Build();
Expand Down
13 changes: 12 additions & 1 deletion AuthenticatorPro.Droid/src/QrCode/Reader/MlKitQrCodeReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
using Android.Content;
using Android.Gms.Extensions;
using Android.Runtime;
using System;
using Java.Lang;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Xamarin.Google.MLKit.Common;
using Xamarin.Google.MLKit.Vision.BarCode;
using Xamarin.Google.MLKit.Vision.Barcode.Common;
using Xamarin.Google.MLKit.Vision.Common;
using Exception = System.Exception;
using Uri = Android.Net.Uri;

namespace AuthenticatorPro.Droid.QrCode.Reader
Expand All @@ -21,6 +23,15 @@ public class MlKitQrCodeReader : IQrCodeReader
{
public async Task<string> ScanImageFromFileAsync(Context context, Uri uri)
{
try
{
MlKit.Initialize(context);
}
catch (IllegalStateException e)
{
Logger.Warn("MlKit already initialised", e);
}

InputImage image;

try
Expand Down

0 comments on commit 07efcb8

Please sign in to comment.