Skip to content

Commit

Permalink
add example file to read in maui sample
Browse files Browse the repository at this point in the history
  • Loading branch information
kfrancis committed Mar 26, 2024
1 parent 07fdf70 commit c07755b
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 33 deletions.
22 changes: 16 additions & 6 deletions samples/Plugin.Maui.Feature.Sample/MainPage.xaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Plugin.Maui.Feature.Sample.MainPage"
Title="Feature Plugin">
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
x:Class="Plugin.Maui.Feature.Sample.MainPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
Title="Feature Plugin">

<!-- Add something that makes sense for your plugin here -->
<!-- Add something that makes sense for your plugin here -->
<Grid Padding="20">
<Label Text="Hello" />
<Button
x:Name="RunOcrBtn"
Clicked="RunOcrBtn_Clicked"
MaximumHeightRequest="100"
MaximumWidthRequest="200"
Text="Run" />
</Grid>
</ContentPage>
19 changes: 19 additions & 0 deletions samples/Plugin.Maui.Feature.Sample/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,23 @@ public MainPage(IOcrService feature)

_feature = feature;
}

protected override async void OnAppearing()
{
base.OnAppearing();

await _feature.InitAsync();
}

private async void RunOcrBtn_Clicked(object sender, EventArgs e)
{
// load test.jpg and then run OCR on it
using var fileStream = await FileSystem.Current.OpenAppPackageFileAsync("test.jpg");

// read all bytes of the image from fileStream
var imageData = new byte[fileStream.Length];
await fileStream.ReadAsync(imageData, 0, imageData.Length);

var result = await _feature.RecognizeTextAsync(imageData);
}
}
31 changes: 15 additions & 16 deletions samples/Plugin.Maui.Feature.Sample/MauiProgram.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
using Microsoft.Extensions.DependencyInjection;
using Plugin.Maui.OCR;
using Plugin.Shared.OCR;
using Plugin.Maui.OCR;

namespace Plugin.Maui.Feature.Sample;

public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
}).UseOcr();

builder.Services.AddTransient<MainPage>();
builder.Services.AddTransient<MainPage>();

return builder.Build();
}
}
return builder.Build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
<MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" />
</ItemGroup>

<ItemGroup>
<None Remove="Resources\Raw\test.jpg" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Plugin.Maui.OCR\Plugin.Maui.OCR.csproj" />
<ProjectReference Include="..\..\src\Plugin.Shared.OCR\Plugin.Shared.OCR.csproj" />
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 10 additions & 11 deletions src/Plugin.Maui.OCR/BuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
using Plugin.Shared.OCR;
using Plugin.Shared.OCR;

namespace Plugin.Maui.OCR
namespace Plugin.Maui.OCR;

public static class OcrServiceExtensions
{
public static class OcrServiceExtensions
public static MauiAppBuilder UseOcr(this MauiAppBuilder builder)
{
public static MauiAppBuilder UseOcr(this MauiAppBuilder builder)
{
// Register the IOcrService implementation with the DI container.
// This ensures that whenever IOcrService is injected, the specific platform implementation is provided.
builder.Services.AddSingleton<IOcrService, OcrImplementation>();
// Register the IOcrService implementation with the DI container.
// This ensures that whenever IOcrService is injected, the specific platform implementation is provided.
builder.Services.AddSingleton<IOcrService, OcrImplementation>();

return builder;
}
return builder;
}
}
}

0 comments on commit c07755b

Please sign in to comment.