Skip to content

Commit

Permalink
add a static list of supported languages for droid
Browse files Browse the repository at this point in the history
  • Loading branch information
kfrancis committed Jul 16, 2024
1 parent c69a207 commit d62faab
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 50 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ csharp_prefer_simple_using_statement = true:suggestion
csharp_style_namespace_declarations = block_scoped:silent
csharp_style_prefer_method_group_conversion = true:silent
csharp_style_prefer_top_level_statements = true:silent
csharp_style_prefer_primary_constructors = true:suggestion
csharp_style_prefer_primary_constructors = false:suggestion
csharp_style_expression_bodied_lambdas = true:silent
csharp_style_expression_bodied_local_functions = false:silent

Expand Down
19 changes: 18 additions & 1 deletion src/Plugin.Maui.OCR/OcrImplementation.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,24 @@ internal class OcrImplementation : IOcrService
{
public event EventHandler<OcrCompletedEventArgs> RecognitionCompleted;

public IReadOnlyCollection<string> SupportedLanguages => throw new NotImplementedException();
// Define the supported languages
private static readonly IReadOnlyCollection<string> s_cloudSupportedLanguages = new List<string>
{
"ar", "zh-Hans", "zh-Hant", "da", "nl", "en", "fi", "fr", "de", "el", "hi", "hu", "it", "ja", "ko",
"no", "pl", "pt", "ru", "es", "sv", "th", "tr", "vi"
};

// On-device recognizer typically only supports Latin-based scripts
private static readonly IReadOnlyCollection<string> s_onDeviceSupportedLanguages = new List<string>
{
"en", "es", "fr", "de", "it", "pt" // Adjust this list as per the actual support
};

// Implement the SupportedLanguages property
public IReadOnlyCollection<string> SupportedLanguages => s_onDeviceSupportedLanguages;

// Adjust the property dynamically based on options
public static IReadOnlyCollection<string> GetSupportedLanguages(bool tryHard) => tryHard ? s_cloudSupportedLanguages : s_onDeviceSupportedLanguages;

public static OcrResult ProcessOcrResult(Java.Lang.Object result, OcrOptions options)
{
Expand Down
79 changes: 47 additions & 32 deletions src/Plugin.Maui.OCR/Plugin.Maui.OCR.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@
<MinVerTagPrefix>v</MinVerTagPrefix>
<TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);CopyProjectReferencesToPackage</TargetsForTfmSpecificBuildOutput>
</PropertyGroup>
<PropertyGroup>
<!-- Enable NuGet package auditing -->
<NuGetAudit>true</NuGetAudit>

<!-- Audit direct and transitive packages -->
<NuGetAuditMode>all</NuGetAuditMode>

<!-- Report low, moderate, high and critical advisories -->
<NuGetAuditLevel>low</NuGetAuditLevel>

<!-- Fails the build on CI or on release when a vulnerability is detected -->
<WarningsAsErrors Condition="$(ContinuousIntegrationBuild) == 'true' OR '$(Configuration)' == 'Release'">
(WarningsAsErrors);NU1900;NU1901;NU1902;NU1903;NU1904
</WarningsAsErrors>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net8.0-android|AnyCPU'">
<DebugType>portable</DebugType>
</PropertyGroup>
Expand Down Expand Up @@ -71,55 +86,55 @@
</PropertyGroup>
<!-- iOS & MacCatalyst -->
<ItemGroup Condition="$(TargetFramework.StartsWith('net')) == true AND $(TargetFramework.Contains('-ios')) != true AND $(TargetFramework.Contains('-maccatalyst')) != true ">
<Compile Remove="**\*.ios.cs"/>
<None Include="**\*.ios.cs" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)"/>
<Compile Remove="**\ios\**\*.cs"/>
<None Include="**\ios\**\*.cs" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)"/>
<Compile Remove="**\*.macios.cs"/>
<None Include="**\*.macios.cs" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)"/>
<Compile Remove="**\macios\**\*.cs"/>
<None Include="**\macios\**\*.cs" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)"/>
<Compile Remove="**\*.ios.cs" />
<None Include="**\*.ios.cs" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)" />
<Compile Remove="**\ios\**\*.cs" />
<None Include="**\ios\**\*.cs" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)" />
<Compile Remove="**\*.macios.cs" />
<None Include="**\*.macios.cs" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)" />
<Compile Remove="**\macios\**\*.cs" />
<None Include="**\macios\**\*.cs" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)" />
</ItemGroup>
<!-- Android -->
<ItemGroup Condition="$(TargetFramework.StartsWith('net')) == true AND $(TargetFramework.Contains('-android')) != true">
<Compile Remove="**\*.android.cs"/>
<None Include="**\*.android.cs" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)"/>
<Compile Remove="**\android\**\*.cs"/>
<None Include="**\android\**\*.cs" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)"/>
<Compile Remove="**\*.android.cs" />
<None Include="**\*.android.cs" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)" />
<Compile Remove="**\android\**\*.cs" />
<None Include="**\android\**\*.cs" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)" />
</ItemGroup>
<!-- Windows -->
<ItemGroup Condition="$(TargetFramework.StartsWith('net')) == true AND $(TargetFramework.Contains('-windows')) != true">
<Compile Remove="**\*.windows.cs"/>
<None Include="**\*.windows.cs" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)"/>
<Compile Remove="**\windows\**\*.cs"/>
<None Include="**\windows\**\*.cs" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)"/>
<Compile Remove="**\*.windows.cs" />
<None Include="**\*.windows.cs" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)" />
<Compile Remove="**\windows\**\*.cs" />
<None Include="**\windows\**\*.cs" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)" />
</ItemGroup>
<!-- .NET (generic) -->
<ItemGroup Condition="!($(TargetFramework.StartsWith('net')) == true AND $(TargetFramework.EndsWith('.0')) == true AND $(TargetFramework.Contains('-')) != true)">
<!-- e.g net6.0 or net8.0 (and higher) -->
<Compile Remove="**\*.net.cs"/>
<None Include="**\*.net.cs" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)"/>
<Compile Remove="**\*.net.cs" />
<None Include="**\*.net.cs" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)"/>
<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="$(MauiVersion)"/>
<PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)" />
<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="$(MauiVersion)" />
<PackageReference Include="MinVer" Version="5.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="System.Management" Version="8.0.0" Condition="$(TargetFramework.Contains('-windows')) == true"/>
<PackageReference Include="DotNet.ReproducibleBuilds" Version="1.1.1">
<PackageReference Include="System.Management" Version="8.0.0" Condition="$(TargetFramework.Contains('-windows')) == true" />
<PackageReference Include="DotNet.ReproducibleBuilds" Version="1.2.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Xamarin.AndroidX.Camera.Camera2" Version="1.3.2" Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'"/>
<PackageReference Include="Xamarin.AndroidX.Camera.Lifecycle" Version="1.3.2" Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'"/>
<PackageReference Include="Xamarin.AndroidX.Camera.Camera2" Version="1.3.2" Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'" />
<PackageReference Include="Xamarin.AndroidX.Camera.Lifecycle" Version="1.3.2" Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'" />
<PackageReference Include="Xamarin.AndroidX.Camera.View" Version="1.3.2" Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'" />
<PackageReference Include="Xamarin.Google.MLKit.TextRecognition" Version="116.0.0.6" Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'"/>
<PackageReference Include="Xamarin.AndroidX.Fragment.Ktx" Version="1.6.2.2" Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'"/>
<PackageReference Include="Xamarin.Google.MLKit.TextRecognition" Version="116.0.0.7" Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'" />
<PackageReference Include="Xamarin.AndroidX.Fragment.Ktx" Version="1.7.0.2" Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'" />
</ItemGroup>
<ItemGroup Condition=" '$(Configuration)' == 'Debug' ">
<PackageReference Include="Roslynator.Analyzers" Version="4.12.2">
<PackageReference Include="Roslynator.Analyzers" Version="4.12.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand All @@ -128,19 +143,19 @@
</ItemGroup>
<!-- Package additions -->
<ItemGroup>
<None Include="..\..\nuget.png" PackagePath="icon.png" Pack="true"/>
<None Include="..\..\README.md" Pack="true" PackagePath="\"/>
<None Include="..\..\nuget.png" PackagePath="icon.png" Pack="true" />
<None Include="..\..\README.md" Pack="true" PackagePath="\" />
</ItemGroup>
<Target Name="CopyProjectReferencesToPackage" DependsOnTargets="BuildOnlySettings;ResolveReferences">
<ItemGroup>
<!-- Filter out unnecessary files -->
<_ReferenceCopyLocalPaths Include="@(ReferenceCopyLocalPaths-&gt;WithMetadataValue('ReferenceSourceTarget', 'ProjectReference')-&gt;WithMetadataValue('PrivateAssets', 'All'))"/>
<_ReferenceCopyLocalPaths Include="@(ReferenceCopyLocalPaths-&gt;WithMetadataValue('ReferenceSourceTarget', 'ProjectReference')-&gt;WithMetadataValue('PrivateAssets', 'All'))" />
</ItemGroup>
<!-- Print batches for debug purposes -->
<Message Text="Batch for .nupkg: ReferenceCopyLocalPaths = @(_ReferenceCopyLocalPaths), ReferenceCopyLocalPaths.DestinationSubDirectory = %(_ReferenceCopyLocalPaths.DestinationSubDirectory) Filename = %(_ReferenceCopyLocalPaths.Filename) Extension = %(_ReferenceCopyLocalPaths.Extension)" Importance="High" Condition="'@(_ReferenceCopyLocalPaths)' != ''"/>
<Message Text="Batch for .nupkg: ReferenceCopyLocalPaths = @(_ReferenceCopyLocalPaths), ReferenceCopyLocalPaths.DestinationSubDirectory = %(_ReferenceCopyLocalPaths.DestinationSubDirectory) Filename = %(_ReferenceCopyLocalPaths.Filename) Extension = %(_ReferenceCopyLocalPaths.Extension)" Importance="High" Condition="'@(_ReferenceCopyLocalPaths)' != ''" />
<ItemGroup>
<!-- Add file to package with consideration of sub folder. If empty, the root folder is chosen. -->
<BuildOutputInPackage Include="@(_ReferenceCopyLocalPaths)" TargetPath="%(_ReferenceCopyLocalPaths.DestinationSubDirectory)"/>
<BuildOutputInPackage Include="@(_ReferenceCopyLocalPaths)" TargetPath="%(_ReferenceCopyLocalPaths.DestinationSubDirectory)" />
</ItemGroup>
</Target>
</Project>
19 changes: 18 additions & 1 deletion src/Plugin.Xamarin.OCR/Platforms/Android/OcrImplementation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,24 @@ namespace Plugin.Xamarin.OCR
{
public class OcrImplementation : IOcrService
{
public IReadOnlyCollection<string> SupportedLanguages => throw new NotImplementedException();
// Define the supported languages
private static readonly IReadOnlyCollection<string> s_cloudSupportedLanguages = new List<string>
{
"ar", "zh-Hans", "zh-Hant", "da", "nl", "en", "fi", "fr", "de", "el", "hi", "hu", "it", "ja", "ko",
"no", "pl", "pt", "ru", "es", "sv", "th", "tr", "vi"
};

// On-device recognizer typically only supports Latin-based scripts
private static readonly IReadOnlyCollection<string> s_onDeviceSupportedLanguages = new List<string>
{
"en", "es", "fr", "de", "it", "pt" // Adjust this list as per the actual support
};

// Implement the SupportedLanguages property
public IReadOnlyCollection<string> SupportedLanguages => s_onDeviceSupportedLanguages;

// Adjust the property dynamically based on options
public static IReadOnlyCollection<string> GetSupportedLanguages(bool tryHard) => tryHard ? s_cloudSupportedLanguages : s_onDeviceSupportedLanguages;

public event EventHandler<OcrCompletedEventArgs> RecognitionCompleted;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ private static OcrResult ProcessOcrResult(VNRequest request, CGSize imageSize, O
}
}

options.CustomCallback?.Invoke(ocrResult.AllText);
options?.CustomCallback?.Invoke(ocrResult.AllText);

ocrResult.Success = true;

Expand Down
47 changes: 33 additions & 14 deletions src/Plugin.Xamarin.OCR/Plugin.Xamarin.OCR.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,33 @@
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
<DebugType>portable</DebugType>
<PackageIcon>icon.png</PackageIcon>
<PackageIcon>icon.png</PackageIcon>
<PackageReadmeFile>README.md</PackageReadmeFile>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<MinVerAutoIncrement>minor</MinVerAutoIncrement>
<MinVerBuildMetadata>build</MinVerBuildMetadata>
<MinVerTagPrefix>v</MinVerTagPrefix>
</PropertyGroup>
<PropertyGroup>
<!-- Enable NuGet package auditing -->
<NuGetAudit>true</NuGetAudit>

<!-- Audit direct and transitive packages -->
<NuGetAuditMode>all</NuGetAuditMode>

<!-- Report low, moderate, high and critical advisories -->
<NuGetAuditLevel>low</NuGetAuditLevel>

<!-- Fails the build on CI or on release when a vulnerability is detected -->
<WarningsAsErrors Condition="$(ContinuousIntegrationBuild) == 'true' OR '$(Configuration)' == 'Release'">
(WarningsAsErrors);NU1900;NU1901;NU1902;NU1903;NU1904
</WarningsAsErrors>
</PropertyGroup>
<!-- Package additions -->
<ItemGroup>
<Compile Include="*.cs"/>
<None Include="..\..\README.md" Pack="true" PackagePath="."/>
<PackageReference Include="DotNet.ReproducibleBuilds" Version="1.1.1">
<Compile Include="*.cs" />
<None Include="..\..\README.md" Pack="true" PackagePath="." />
<PackageReference Include="DotNet.ReproducibleBuilds" Version="1.2.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand All @@ -42,25 +57,25 @@
</PackageReference>
</ItemGroup>
<ItemGroup Condition=" $(TargetFramework.StartsWith('netstandard')) ">
<Compile Include="netstandard\*.cs"/>
<Compile Include="netstandard\*.cs" />
</ItemGroup>
<!-- iOS -->
<ItemGroup Condition=" $(TargetFramework.StartsWith('Xamarin.iOS')) ">
<Compile Include="Platforms\Apple\**\*.cs"/>
<Compile Include="Platforms\Apple\**\*.cs" />
</ItemGroup>
<PropertyGroup Condition=" $(TargetFramework.StartsWith('Xamarin.iOS')) ">
<DefineConstants>$(DefineConstants);IOS;</DefineConstants>
</PropertyGroup>
<!-- macOS -->
<ItemGroup Condition=" $(TargetFramework.StartsWith('Xamarin.Mac')) ">
<Compile Include="Platforms\Apple\**\*.cs"/>
<Compile Include="Platforms\Apple\**\*.cs" />
</ItemGroup>
<PropertyGroup Condition=" $(TargetFramework.StartsWith('Xamarin.Mac')) ">
<DefineConstants>$(DefineConstants);MACOS;</DefineConstants>
</PropertyGroup>
<!-- Android -->
<ItemGroup Condition=" $(TargetFramework.StartsWith('MonoAndroid')) ">
<Compile Include="Platforms\Android\**\*.cs"/>
<Compile Include="Platforms\Android\**\*.cs" />
<PackageReference Include="Xamarin.AndroidX.Camera.Camera2" Version="1.3.0">
</PackageReference>
<PackageReference Include="Xamarin.AndroidX.Camera.Lifecycle" Version="1.3.0">
Expand All @@ -73,33 +88,37 @@
<Version>116.0.0.5</Version>
</PackageReference>
<PackageReference Include="Xamarin.AndroidX.Fragment.Ktx">
<Version>1.6.2.2</Version>
<Version>1.6.2</Version>
</PackageReference>
</ItemGroup>
<PropertyGroup Condition=" $(TargetFramework.StartsWith('MonoAndroid')) ">
<DefineConstants>$(DefineConstants);ANDROID;</DefineConstants>
</PropertyGroup>
<!-- UWP -->
<ItemGroup Condition=" $(TargetFramework.StartsWith('uap10.0')) ">
<Compile Include="Platforms\UWP\**\*.cs"/>
<Compile Include="Platforms\UWP\**\*.cs" />
</ItemGroup>
<PropertyGroup Condition=" $(TargetFramework.StartsWith('uap10.0')) ">
<!--<TargetPlatformMinVersion>10.0.10240.0</TargetPlatformMinVersion>-->
<DefineConstants>$(DefineConstants);WINDOWS_UWP;</DefineConstants>
</PropertyGroup>
<ItemGroup Condition=" $(TargetFramework.StartsWith('net4')) OR $(TargetFramework.StartsWith('Xamarin.TVOS')) OR $(TargetFramework.StartsWith('Xamarin.WatchOS')) OR $(TargetFramework.StartsWith('Tizen')) ">
<Compile Include="Platforms\NotImplemented\**\*.cs"/>
<Compile Include="Platforms\NotImplemented\**\*.cs" />
</ItemGroup>
<ItemGroup Condition=" '$(Configuration)' == 'Debug' ">
<PackageReference Include="Roslynator.Analyzers" Version="4.12.3">
<PackageReference Include="Roslynator.Analyzers" Version="4.12.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<None Include="..\..\nuget.png" PackagePath="icon.png" Pack="true" />
<None Include="..\..\README.md" Pack="true" PackagePath="\" />
</ItemGroup>
<ItemGroup>
<None Remove="Abstractions\IOcrService.cs"/>
<None Remove="Abstractions\IOcrService.cs" />
</ItemGroup>
<ItemGroup>
<Compile Include="Abstractions\IOcrService.cs"/>
<Compile Include="Abstractions\IOcrService.cs" />
</ItemGroup>
</Project>

0 comments on commit d62faab

Please sign in to comment.