-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMainPage.xaml.cs
135 lines (105 loc) · 4.32 KB
/
MainPage.xaml.cs
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using Microsoft.Extensions.Logging;
using System.Runtime.InteropServices;
using System.Text.Json;
using TesseractOcrMaui;
using TesseractOcrMaui.Enums;
using TesseractOcrMaui.Imaging;
using TesseractOcrMaui.Iterables;
using TesseractOcrMaui.Results;
using TesseractOcrMaui.Tessdata;
#nullable enable
namespace TesseractOcrMauiTestApp;
public partial class MainPage : ContentPage
{
readonly ITessDataProvider _provider;
public MainPage(ITesseract tesseract, ILogger<MainPage> logger, ITessDataProvider provider)
{
InitializeComponent();
Tesseract = tesseract;
_provider = provider;
logger.LogInformation($"--------------------------------");
logger.LogInformation($"- {nameof(TesseractOcrMaui)} Demo -");
logger.LogInformation($"--------------------------------");
var rid = RuntimeInformation.RuntimeIdentifier;
logger.LogInformation("Running on rid '{rid}'", rid);
}
ITesseract Tesseract { get; }
private async void DEMO_Recognize_AsImage(object sender, EventArgs e)
{
// Select image (Not important)
var path = await ImageSelecter.LetUserSelect();
if (path is null)
{
return;
}
// Recognize image
var result = await Tesseract.RecognizeTextAsync(path);
// Show output (Not important)
ShowOutput("FromPath", result);
}
private async void DEMO_Recognize_AsBytes(object sender, EventArgs e)
{
// Select image (Not important)
var path = await ImageSelecter.LetUserSelect();
if (path is null)
{
return;
}
// File to byte array (Use your own way)
using FileStream stream = new(path, FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[stream.Length];
stream.Read(buffer);
// recognize bytes
var result = await Tesseract.RecognizeTextAsync(buffer);
// Show output (Not important)
ShowOutput("FromBytes", result);
}
private async void DEMO_Recognize_AsConfigured(object sender, EventArgs e)
{
// Select image (Not important)
var path = await ImageSelecter.LetUserSelect();
if (path is null)
{
return;
}
Tesseract.EngineConfiguration = (engine) =>
{
// Engine uses DefaultSegmentationMode, if no other is passed as method parameter.
// If ITesseract is injected to page, this is only way of setting PageSegmentationMode.
// PageSegmentationMode defines how ocr tries to look for text, for example singe character or single word.
// By default uses PageSegmentationMode.Auto.
engine.DefaultSegmentationMode = PageSegmentationMode.SingleWord;
engine.SetCharacterWhitelist("abcdefgh"); // These characters ocr is looking for
engine.SetCharacterBlacklist("abc"); // These characters ocr is not looking for
// Now ocr should be only finding characters 'defgh'
// You can also notice that setting character listing will set ocr confidence to 0
};
// You can also set engine mode by uncommenting line below
//Tesseract.EngineMode = TesseractOcrMaui.Enums.EngineMode.TesseractOnly;
// Recognize image
var result = await Tesseract.RecognizeTextAsync(path);
// For this example I reset engine configuration, because same object is used in other examples
Tesseract.EngineConfiguration = null;
// Show output (Not important)
ShowOutput("FromPath, Configured", result);
}
private async void DEMO_GetVersion(object sender, EventArgs e)
{
string version = Tesseract.TryGetTesseractLibVersion() ?? "Failed";
await DisplayAlert("Tesseract version", version, "OK");
}
// Not important for package
private void ShowOutput(string imageMode, RecognizionResult result)
{
// Show output (Not important)
fileModeLabel.Text = $"File mode: {imageMode}";
if (result.NotSuccess())
{
confidenceLabel.Text = $"Confidence: -1";
resultLabel.Text = $"Recognizion failed: {result.Status}";
return;
}
confidenceLabel.Text = $"Confidence: {result.Confidence}";
resultLabel.Text = result.RecognisedText;
}
}