-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathOcrService.cs
More file actions
27 lines (23 loc) · 879 Bytes
/
OcrService.cs
File metadata and controls
27 lines (23 loc) · 879 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Globalization;
using Windows.Graphics.Imaging;
using Windows.Media.Ocr;
namespace NoteUI;
internal static class OcrService
{
public static async Task<string> ExtractTextAsync(byte[] pixels, int width, int height)
{
var engine = OcrEngine.TryCreateFromUserProfileLanguages()
?? OcrEngine.TryCreateFromLanguage(new Language("fr-FR"))
?? OcrEngine.TryCreateFromLanguage(new Language("en-US"));
if (engine == null) return "";
var bitmap = new SoftwareBitmap(
BitmapPixelFormat.Bgra8,
width,
height,
BitmapAlphaMode.Premultiplied);
bitmap.CopyFromBuffer(pixels.AsBuffer());
var result = await engine.RecognizeAsync(bitmap);
return (result.Text ?? "").Trim();
}
}