Skip to content

Commit

Permalink
nuget packing and documentation fun
Browse files Browse the repository at this point in the history
  • Loading branch information
kfrancis committed Mar 28, 2024
1 parent 9138403 commit a9f4784
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 14 deletions.
7 changes: 7 additions & 0 deletions src/Plugin.Maui.OCR/Ocr.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Plugin.Maui.OCR;

/// <summary>
/// OCR API.
/// </summary>
public static class OCR
{
static IOcrService? s_defaultImplementation;
Expand All @@ -12,6 +15,10 @@ public static class OCR
public static IOcrService Default =>
s_defaultImplementation ??= new OcrImplementation();

/// <summary>
/// Sets the default implementation.
/// </summary>
/// <param name="implementation"></param>
internal static void SetDefault(IOcrService? implementation) =>
s_defaultImplementation = implementation;
}
12 changes: 10 additions & 2 deletions src/Plugin.Maui.OCR/OcrImplementation.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ public static OcrResult ProcessOcrResult(Java.Lang.Object result)
return ocrResult;
}

/// <inheritdoc/>
/// <summary>
/// Initialize the OCR on the platform
/// </summary>
/// <param name="ct">An optional cancellation token</param>
public Task InitAsync(System.Threading.CancellationToken ct = default)
{
// Initialization might not be required for ML Kit's on-device text recognition,
Expand All @@ -49,7 +52,12 @@ public Task InitAsync(System.Threading.CancellationToken ct = default)
return Task.CompletedTask;
}

/// <inheritdoc/>
/// <summary>
/// Takes an image and returns the text found in the image.
/// </summary>
/// <param name="imageData">The image data</param>
/// <param name="ct">An optional cancellation token</param>
/// <returns>The OCR result</returns>
public async Task<OcrResult> RecognizeTextAsync(byte[] imageData, System.Threading.CancellationToken ct = default)
{
var image = BitmapFactory.DecodeByteArray(imageData, 0, imageData.Length);
Expand Down
12 changes: 10 additions & 2 deletions src/Plugin.Maui.OCR/OcrImplementation.macios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ partial class OcrImplementation : IOcrService
private static readonly object s_initLock = new();
private bool _isInitialized;

/// <inheritdoc/>
/// <summary>
/// Initialize the OCR on the platform
/// </summary>
/// <param name="ct">An optional cancellation token</param>
public Task InitAsync(CancellationToken ct = default)
{
lock (s_initLock)
Expand All @@ -25,7 +28,12 @@ public Task InitAsync(CancellationToken ct = default)
return Task.CompletedTask;
}

/// <inheritdoc/>
/// <summary>
/// Takes an image and returns the text found in the image.
/// </summary>
/// <param name="imageData">The image data</param>
/// <param name="ct">An optional cancellation token</param>
/// <returns>The OCR result</returns>
public async Task<OcrResult> RecognizeTextAsync(byte[] imageData, CancellationToken ct = default)
{
if (!_isInitialized)
Expand Down
12 changes: 10 additions & 2 deletions src/Plugin.Maui.OCR/OcrImplementation.net.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,21 @@ 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/>
/// <summary>
/// Initialize the OCR on the platform
/// </summary>
/// <param name="ct">An optional cancellation token</param>
public Task InitAsync(CancellationToken ct = default)
{
throw new NotImplementedException();
}

/// <inheritdoc/>
/// <summary>
/// Takes an image and returns the text found in the image.
/// </summary>
/// <param name="imageData">The image data</param>
/// <param name="ct">An optional cancellation token</param>
/// <returns>The OCR result</returns>
public Task<OcrResult> RecognizeTextAsync(byte[] imageData, CancellationToken ct = default)
{
throw new NotImplementedException();
Expand Down
12 changes: 10 additions & 2 deletions src/Plugin.Maui.OCR/OcrImplementation.windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,22 @@ namespace Plugin.Maui.OCR;

partial class OcrImplementation : IOcrService
{
/// <inheritdoc/>
/// <summary>
/// Initialize the OCR on the platform
/// </summary>
/// <param name="ct">An optional cancellation token</param>
public Task InitAsync(CancellationToken ct = default)
{
// Windows OCR doesn't require explicit initialization.
return Task.CompletedTask;
}

/// <inheritdoc/>
/// <summary>
/// Takes an image and returns the text found in the image.
/// </summary>
/// <param name="imageData">The image data</param>
/// <param name="ct">An optional cancellation token</param>
/// <returns>The OCR result</returns>
public async Task<Shared.OCR.OcrResult> RecognizeTextAsync(byte[] imageData, CancellationToken ct = default)
{
var ocrEngine = OcrEngine.TryCreateFromUserProfileLanguages() ?? throw new NotSupportedException("OCR not supported on this device or no languages are installed.");
Expand Down
57 changes: 51 additions & 6 deletions src/Plugin.Maui.OCR/Plugin.Maui.OCR.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,38 @@
<DebugType>portable</DebugType>
<PackageIcon>icon.png</PackageIcon>
<PackageReadmeFile>README.md</PackageReadmeFile>

<TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);CopyProjectReferencesToPackage</TargetsForTfmSpecificBuildOutput>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net8.0-android|AnyCPU'">
<DebugType>embedded</DebugType>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net8.0-ios|AnyCPU'">
<DebugType>embedded</DebugType>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net8.0-maccatalyst|AnyCPU'">
<DebugType>embedded</DebugType>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net8.0|AnyCPU'">
<DebugType>embedded</DebugType>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net8.0-windows10.0.19041.0|AnyCPU'">
<DebugType>embedded</DebugType>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net8.0-android|AnyCPU'">
<DebugType>embedded</DebugType>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net8.0-ios|AnyCPU'">
<DebugType>embedded</DebugType>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net8.0-maccatalyst|AnyCPU'">
<DebugType>embedded</DebugType>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net8.0|AnyCPU'">
<DebugType>embedded</DebugType>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net8.0-windows10.0.19041.0|AnyCPU'">
<DebugType>embedded</DebugType>
</PropertyGroup>

<!-- iOS & MacCatalyst -->
Expand Down Expand Up @@ -89,6 +121,9 @@
</PackageReference>
<PackageReference Include="Xamarin.Google.MLKit.BarcodeScanning" Version="117.2.0.3">
</PackageReference>
<PackageReference Include="Xamarin.Google.MLKit.TextRecognition">
<Version>116.0.0.4</Version>
</PackageReference>
</ItemGroup>

<ItemGroup Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">
Expand All @@ -100,12 +135,22 @@
<None Include="..\..\README.md" Pack="true" PackagePath="\" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Plugin.Shared.OCR\Plugin.Shared.OCR.csproj" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0-android'">
<PackageReference Include="Xamarin.Google.MLKit.TextRecognition">
<Version>116.0.0.4</Version>
</PackageReference>
<ProjectReference Include="..\Plugin.Shared.OCR\Plugin.Shared.OCR.csproj" PrivateAssets="All" />
</ItemGroup>

<Target Name="CopyProjectReferencesToPackage" DependsOnTargets="BuildOnlySettings;ResolveReferences">
<ItemGroup>
<!-- Filter out unnecessary files -->
<_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)' != ''" />

<ItemGroup>
<!-- Add file to package with consideration of sub folder. If empty, the root folder is chosen. -->
<BuildOutputInPackage Include="@(_ReferenceCopyLocalPaths)" TargetPath="%(_ReferenceCopyLocalPaths.DestinationSubDirectory)" />
</ItemGroup>
</Target>

</Project>
7 changes: 7 additions & 0 deletions src/Plugin.Shared.OCR/IOcrService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@

namespace Plugin.Shared.OCR;

/// <summary>
/// OCR API.
/// </summary>
public interface IOcrService
{
/// <summary>
/// Initialize the OCR on the platform
/// </summary>
/// <param name="ct">An optional cancellation token</param>
Task InitAsync(CancellationToken ct = default);

/// <summary>
Expand Down
8 changes: 8 additions & 0 deletions src/Plugin.Shared.OCR/Plugin.Shared.OCR.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,12 @@
<LangVersion>latest</LangVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DebugType>embedded</DebugType>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DebugType>embedded</DebugType>
</PropertyGroup>

</Project>

0 comments on commit a9f4784

Please sign in to comment.