-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
102 lines (98 loc) · 4.54 KB
/
Program.cs
File metadata and controls
102 lines (98 loc) · 4.54 KB
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
using System;
using System.Threading;
using Windows.ApplicationModel.DataTransfer;
using Windows.Graphics.Imaging;
using Windows.Media.Ocr;
using System.Linq;
using Windows.ApplicationModel.Core;
using Windows.Storage.Streams;
using Windows.Foundation.Metadata;
namespace cliocr {
public class Program {
private static async void Go(string[] args) {
SoftwareBitmap bmp = null;
try {
if (args.Contains("-i")) {
var stdin = Console.OpenStandardInput();
System.IO.MemoryStream ms = new System.IO.MemoryStream();
int read;
byte[] buffer = new byte[16*1024];
while ((read = stdin.Read(buffer, 0, buffer.Length)) > 0) ms.Write(buffer, 0, read);
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(System.IO.WindowsRuntimeStreamExtensions.AsRandomAccessStream(ms));
bmp = await decoder.GetSoftwareBitmapAsync();
}
else {
var dataPackage = Clipboard.GetContent();
if (dataPackage.Contains(StandardDataFormats.Bitmap)) {
var image = await dataPackage.GetBitmapAsync();
var imageStream = await image.OpenReadAsync();
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(imageStream);
bmp = await decoder.GetSoftwareBitmapAsync();
} else {
Console.Error.WriteLine("Not Bitmap");
Environment.Exit(-1);
}
}
//Console.WriteLine("bmp {0}x{1}@{2}", bmp.PixelWidth, bmp.PixelHeight, bmp.BitmapPixelFormat);
}
catch (Exception ex)
{
Console.Error.WriteLine("Something bad happened while reading image:\n"+ex);
Environment.Exit(-1);
}
try {
OcrEngine ocrEngine = OcrEngine.TryCreateFromUserProfileLanguages();
String lang = args.SkipWhile (s => s != "-l").Skip(1).DefaultIfEmpty("").First().ToLower();
if (lang!="") ocrEngine = OcrEngine.TryCreateFromLanguage(OcrEngine.AvailableRecognizerLanguages.FirstOrDefault(l => l.LanguageTag.ToLower().StartsWith(lang)));
OcrResult ocrResult = await ocrEngine.RecognizeAsync(bmp);
string extractedText = "FAILURE";
extractedText = ocrResult.Text;
if (args.Contains("-v")) {
Console.WriteLine("text:\t{0}", ocrResult.Text);
Console.WriteLine("angle:\t{0}",ocrResult.TextAngle);
Console.WriteLine("lines:\t{0}",ocrResult.Lines.Count);
}
if (args.Contains("-c")) {
Thread thread = new Thread(() => {
if (!String.IsNullOrWhiteSpace(extractedText)) {
var dp = new DataPackage();
dp.SetText(extractedText);
Clipboard.SetContent(dp);
Clipboard.Flush();
}
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
} else {
Console.WriteLine(extractedText);
}
}
catch (Exception ex) {
Console.Error.WriteLine("Something bad happened while running OCR:\n"+ex);
Environment.Exit(-1);
}
resetEvent.Set();
}
static ManualResetEvent resetEvent = new ManualResetEvent(false);
[STAThreadAttribute]
static void Main(string[] args) {
if (args.Contains("--help")||args.Contains("-h")||args.Contains("/?")) {
Console.WriteLine("cliocr [command]");
Console.WriteLine(" -c copy recognized text to clipboard");
Console.WriteLine(" -s show avaiable languages");
Console.WriteLine(" -i read image from stdin (bmp, jpg, ...)");
Console.WriteLine(" -v verbose OcrResult");
Console.WriteLine(" -l LANG set lanugage");
Console.WriteLine(" -h --help /? this help");
Environment.Exit(0);
}
if (args.Contains("-s")) {
foreach(var l in OcrEngine.AvailableRecognizerLanguages) Console.WriteLine(l.LanguageTag+"\t"+l.DisplayName);
Environment.Exit(0);
}
Go(args);
resetEvent.WaitOne();
}
}
}