Starshot.OneOcr
0.2.0
dotnet add package Starshot.OneOcr --version 0.2.0
NuGet\Install-Package Starshot.OneOcr -Version 0.2.0
<PackageReference Include="Starshot.OneOcr" Version="0.2.0" />
<PackageVersion Include="Starshot.OneOcr" Version="0.2.0" />
<PackageReference Include="Starshot.OneOcr" />
paket add Starshot.OneOcr --version 0.2.0
#r "nuget: Starshot.OneOcr, 0.2.0"
#:package Starshot.OneOcr@0.2.0
#addin nuget:?package=Starshot.OneOcr&version=0.2.0
#tool nuget:?package=Starshot.OneOcr&version=0.2.0
Starshot.OneOcr
C# bindings for OneOCR — the OCR engine embedded in the Windows Photos app and Snipping Tool. Falls back to the system engine (Windows.Media.Ocr) when OneOCR is unavailable, and squashes the extra spaces that both engines insert between CJK characters.
This package does NOT contain the engine itself. OneOCR is a Microsoft proprietary component; you must obtain the two files yourself and place them next to your app exe:
oneocr.dll(~39 MB)oneocr.onemodel(~56 MB, encrypted — the decryption key is included in this library)Where to get them: copy from the local Windows Snipping Tool package (
%ProgramFiles%\WindowsApps\Microsoft.ScreenSketch_*\SnippingTool\) or the Photos app package root, viaWindows.Management.Deployment.PackageManager. Redistribution of these files is your own responsibility.
Requirements
- .NET 10,
net10.0-windows10.0.26100.0(Windows App SDK projection) - Win2D 1.3.2+ (any compatible build)
- Serilog (used for engine logging)
Quick start
using Starshot.OneOcr;
// 1) On the UI thread (GPU readback must stay on the device's thread):
var (pixels, width, height, scale) = OcrHelper.PreparePixels(canvasBitmap);
// 2) Any thread — oneocr first, Windows.Media.Ocr fallback:
var result = await OcrHelper.RecognizeAsync(pixels, width, height, scale);
if (result is null)
{
// no engine available at all (files missing AND no OCR language pack)
}
else if (result.Lines.Count == 0)
{
// recognized nothing
}
else
{
Console.WriteLine($"engine: {result.Engine}"); // OneOcr or Legacy (incl. fallback)
foreach (var line in result.Lines)
{
Console.WriteLine(line.Text); // full-line text, spaces already squashed
foreach (var word in line.Words)
{
// word.Text + word.Rect (original-image pixel coordinates)
}
}
}
Pass preferOneOcr: false to RecognizeAsync to skip OneOCR and use the system engine directly.
API surface
| Member | Purpose |
|---|---|
OcrHelper.PreparePixels(CanvasBitmap) |
GPU: any pixel format → BGRA8 bytes (HDR float sources converted without tonemap). Must run on the UI thread. |
OcrHelper.RecognizeAsync(bytes, w, h, scale, preferOneOcr) |
CPU-only recognition, thread-pool safe. Returns OcrResult (lines + which engine actually ran), null = no engine. Coordinates are original-image pixels. |
OcrHelper.IsOneOcrReady |
File-existence check for the two engine files. |
OcrHelper.ResetEngineCache() |
Call after fetching/replacing/deleting engine files so init is retried. |
OcrHelper.JoinWords(words) |
Word-sequence joining for text selection (Latin gets spaces, CJK doesn't). |
OcrLine / OcrWord |
Result records: text + original-image pixel rect. |
OcrResult / OcrEngineUsed |
Recognition result wrapper: lines + the engine that actually ran (OneOcr, or Legacy including fallback). |
Notes
- OneOCR failures (missing or corrupt engine files, runtime errors) automatically fall back to the system engine; check
OcrResult.Engineto see which engine actually processed the image. - The OneOCR pipeline is created lazily on first use and cached for the process lifetime;
RunOcrPipelinecalls are serialized with a lock (thread safety of the native engine is unknown). - Both engines' line text is token-space-joined;
SquashCjkSpacesremoves spaces only when neither neighbor is a Latin letter/digit — CJK gets glued, English keeps its spaces. - Coordinates from oversized inputs (legacy engine
MaxImageDimensionlimit) are pre-scaled on the GPU and multiplied back so both engines return original-image pixels.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0-windows10.0.26100 is compatible. |
-
net10.0-windows10.0.26100
- Microsoft.Graphics.Win2D (>= 1.3.2)
- Serilog (>= 4.2.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.