Skip to content

Commit

Permalink
misc
Browse files Browse the repository at this point in the history
  • Loading branch information
kfrancis committed Mar 27, 2024
1 parent 2822708 commit 495ea92
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 21 deletions.
14 changes: 7 additions & 7 deletions samples/Plugin.Maui.Feature.Sample/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Plugin.Maui.Feature.Sample;

public partial class MainPage : ContentPage
{
readonly IOcrService _ocr;
private readonly IOcrService _ocr;

public MainPage(IOcrService feature)
{
Expand All @@ -20,6 +20,12 @@ protected override async void OnAppearing()
await _ocr.InitAsync();
}

private void ClearBtn_Clicked(object sender, EventArgs e)
{
ResultLbl.Text = string.Empty;
ClearBtn.IsEnabled = false;
}

private async void OpenFromCameraBtn_Clicked(object sender, EventArgs e)
{
if (MediaPicker.Default.IsCaptureSupported)
Expand Down Expand Up @@ -72,10 +78,4 @@ private async void OpenFromFileBtn_Clicked(object sender, EventArgs e)
ClearBtn.IsEnabled = true;
}
}

private void ClearBtn_Clicked(object sender, EventArgs e)
{
ResultLbl.Text = string.Empty;
ClearBtn.IsEnabled = false;
}
}
4 changes: 2 additions & 2 deletions samples/Plugin.Maui.Feature.Sample/MauiProgram.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Plugin.Shared.OCR;
using Plugin.Maui.OCR;

namespace Plugin.Maui.Feature.Sample;
Expand All @@ -14,7 +13,8 @@ public static MauiApp CreateMauiApp()
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
}).UseOcr();
})
.UseOcr();

builder.Services.AddTransient<MainPage>();

Expand Down
16 changes: 7 additions & 9 deletions src/Plugin.Maui.OCR/OcrImplementation.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal partial class OcrImplementation : IOcrService
public static OcrResult ProcessOcrResult(Java.Lang.Object result)
{
var ocrResult = new OcrResult();
var textResult = (Xamarin.Google.MLKit.Vision.Text.Text)result;
var textResult = (Text)result;

ocrResult.AllText = textResult.GetText();
foreach (var block in textResult.TextBlocks)
Expand Down Expand Up @@ -46,7 +46,6 @@ public Task InitAsync(System.Threading.CancellationToken ct = default)
// Initialization might not be required for ML Kit's on-device text recognition,
// but you can perform any necessary setup here.


return Task.CompletedTask;
}

Expand All @@ -59,21 +58,21 @@ public async Task<OcrResult> RecognizeTextAsync(byte[] imageData, System.Threadi
using var textScanner = TextRecognition.GetClient(TextRecognizerOptions.DefaultOptions);

MlKitException? lastException = null;
var maxRetries = 5;
const int MaxRetries = 5;

for (var retry = 0; retry < maxRetries; retry++)
for (var retry = 0; retry < MaxRetries; retry++)
{
try
{
// Try to perform the OCR operation
// Try to perform the OCR operation. We should be installing the model necessary when this app is installed, but just in case ..
return ProcessOcrResult(await ToAwaitableTask(textScanner.Process(inputImage).AddOnSuccessListener(new OnSuccessListener()).AddOnFailureListener(new OnFailureListener())));
}
catch (MlKitException ex) when (ex.Message.Contains("Waiting for the text optional module to be downloaded"))
catch (MlKitException ex) when ((ex.Message ?? string.Empty).Contains("Waiting for the text optional module to be downloaded"))
{
// If the specific exception is caught, log it and wait before retrying
lastException = ex;
Debug.WriteLine($"OCR model is not ready. Waiting before retrying... Attempt {retry + 1}/{maxRetries}");
await Task.Delay(5000);
Debug.WriteLine($"OCR model is not ready. Waiting before retrying... Attempt {retry + 1}/{MaxRetries}");
await Task.Delay(5000, ct);
}
}

Expand All @@ -86,7 +85,6 @@ public async Task<OcrResult> RecognizeTextAsync(byte[] imageData, System.Threadi
{
throw new InvalidOperationException("OCR operation failed without an exception.");
}

}

private static Task<Java.Lang.Object> ToAwaitableTask(global::Android.Gms.Tasks.Task task)
Expand Down
4 changes: 1 addition & 3 deletions src/Plugin.Maui.OCR/OcrImplementation.net.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Plugin.Maui.OCR;

// This usually is a placeholder as .NET MAUI apps typically don't run on .NET generic targets unless through unit tests and such
partial class OcrImplementation : IOcrService
{
/// <inheritdoc/>
Expand All @@ -10,9 +11,6 @@ public Task InitAsync(CancellationToken ct = default)
throw new NotImplementedException();
}

// TODO Implement your .NET specific code.
// This usually is a placeholder as .NET MAUI apps typically don't run on .NET generic targets unless through unit tests and such

/// <inheritdoc/>
public Task<OcrResult> RecognizeTextAsync(byte[] imageData, CancellationToken ct = default)
{
Expand Down

0 comments on commit 495ea92

Please sign in to comment.