-
Notifications
You must be signed in to change notification settings - Fork 0
/
OCR.cs
111 lines (98 loc) · 2.84 KB
/
OCR.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
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
using System.Threading;
using System.Windows.Media.Imaging;
using Windows.Graphics.Imaging;
using Windows.Media.Ocr;
using System.Threading.Tasks;
using System.Drawing;
namespace Produire.PImaging
{
public class OCR : IProduireStaticClass
{
/// <summary>
/// 画像から文字認識します。
/// </summary>
/// <param name="image">読み取り画像</param>
/// <param name="lang">対象の言語</param>
/// <returns>読み取った文字列</returns>
[自分で]
public string 読み取る([を]Image image, [として, 省略]string lang)
{
Task<SoftwareBitmap> t1 = null;
Task.Run(() =>
{
t1 = ConvertSoftwareBitmap((Bitmap)image);
}).Wait();
SoftwareBitmap x1 = t1.Result;
if (string.IsNullOrEmpty(lang)) lang = "ja-JP";
Task<OcrResult> t2 = null;
Task.Run(() =>
{
t2 = Recognize(x1, lang);
}).Wait();
OcrResult x2 = t2.Result;
StringBuilder builder = new StringBuilder();
foreach (var line in x2.Lines)
{
if (builder.Length > 0) builder.AppendLine();
var words = line.Words;
bool lastHalf = false;
for (int i = 0; i < words.Count; i++)
{
var word = words[i];
bool isHalf = IsHalf(word.Text);
if (lastHalf != isHalf)
{
if (i > 0) builder.Append(" ");
}
builder.Append(word.Text);
}
}
return builder.ToString();
}
private bool IsHalf(string text)
{
for (int i = 0; i < text.Length; i++)
{
char c = text[i];
if (char.IsDigit(c) || char.IsLower(c) || char.IsUpper(c) || char.IsWhiteSpace(c))
{
}
else
return false;
}
return true;
}
private async Task<SoftwareBitmap> ConvertSoftwareBitmap(Bitmap bitmap)
{
System.Windows.Media.Imaging.BitmapFrame bitmapSource = null;
using (var wfStream = new MemoryStream())
{
bitmap.Save(wfStream, System.Drawing.Imaging.ImageFormat.Bmp);
wfStream.Seek(0, SeekOrigin.Begin);
bitmapSource = System.Windows.Media.Imaging.BitmapFrame.Create(wfStream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
}
SoftwareBitmap sbitmap = null;
using (MemoryStream wpfStream = new MemoryStream())
{
var encoder = new BmpBitmapEncoder();
encoder.Frames.Add(bitmapSource);
encoder.Save(wpfStream);
var irstream = WindowsRuntimeStreamExtensions.AsRandomAccessStream(wpfStream);
var decorder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(irstream);
sbitmap = await decorder.GetSoftwareBitmapAsync();
}
return sbitmap;
}
private async Task<OcrResult> Recognize(SoftwareBitmap sbitmap, string lang)
{
OcrEngine engine = OcrEngine.TryCreateFromLanguage(new Windows.Globalization.Language(lang));
var result = await engine.RecognizeAsync(sbitmap);
return result;
}
}
}