csharp:Optical Character Recognition

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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
using MODI;//Microsoft Office Document Imaging
// 首先用office安装盘这个组件,默认安装office的时候是不会安装的,只要添加这个组件功能就好了安装说明:http://support.microsoft.com/kb/982760
//组件Microsoft Office Document Imaging 12.0 Type Library(office2007)
//或者Microsoft Office Document Imaging 11.0 Type Library(office2003)
//中文简体OCR引擎 http://www.microsoft.com/downloads/thankyou.aspx?familyId=dd172063-9517-41d8-82af-29c38f7437b6&displayLang=zh-hk
 
namespace ToText
{  
    /// <summary>
    /// Optical Character Recognition光学字符识别
    /// 20140507 Geovin Du
    /// 涂聚文
    /// </summary>
    public static class OCRGetstring
    {
        /// <summary>
        /// 语言类型
        /// </summary>
        /// <returns></returns>
        public static DataTable getLanguageList()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ID",typeof(int));
            dt.Columns.Add("LanguageName", typeof(string));
            dt.Columns.Add("LanguageLCID", typeof(string));
            //dt.Rows.Add(1, "", 1);
            dt.Rows.Add(1,"简体中文", "2052");
            dt.Rows.Add(2,"繁体中文", "1028");
            dt.Rows.Add(3,"英语", "9");
            dt.Rows.Add(4,"捷克语", "5");
            dt.Rows.Add(5,"丹麦语", "6");
            dt.Rows.Add(6,"德语", "7");
            dt.Rows.Add(7,"希腊语", "8");
            dt.Rows.Add(8,"西班牙语", "10");
            dt.Rows.Add(9,"芬兰语", "11");
            dt.Rows.Add(10,"法语", "12");
            dt.Rows.Add(11, "匈牙利语", "14");
            dt.Rows.Add(12, "意大利语", "16");
            dt.Rows.Add(13, "日语", "17");
            dt.Rows.Add(14, "韩语", "18");
            dt.Rows.Add(15, "荷兰语", "19");
            dt.Rows.Add(16, "挪威语", "20");
            dt.Rows.Add(17, "波兰语", "21");
            dt.Rows.Add(18, "葡萄牙语", "22");
            dt.Rows.Add(19, "俄语", "25");
            dt.Rows.Add(20,"瑞典语", "29");
            dt.Rows.Add(21,"土耳其语", "31");
            return dt;
        }
 
        /// <summary>
        ///
        /// </summary>
        /// <param name="sValue"></param>
        /// <returns></returns>
        private static MODI.MiLANGUAGES GetLanuageType(string sValue)
        {
            switch (sValue)
            {
                case "2052":
                    return MODI.MiLANGUAGES.miLANG_CHINESE_SIMPLIFIED;
                case "5":
                    return MODI.MiLANGUAGES.miLANG_CZECH;
                case "6":
                    return MODI.MiLANGUAGES.miLANG_DANISH;
                case "7":
                    return MODI.MiLANGUAGES.miLANG_GERMAN;
                case "8":
                    return MODI.MiLANGUAGES.miLANG_GREEK;
                case "9":
                    return MODI.MiLANGUAGES.miLANG_ENGLISH;
                case "10":
                    return MODI.MiLANGUAGES.miLANG_SPANISH;
                case "11":
                    return MODI.MiLANGUAGES.miLANG_FINNISH;
                case "12":
                    return MODI.MiLANGUAGES.miLANG_FRENCH;
                case "14":
                    return MODI.MiLANGUAGES.miLANG_HUNGARIAN;
                case "16":
                    return MODI.MiLANGUAGES.miLANG_ITALIAN;
                case "17":
                    return MODI.MiLANGUAGES.miLANG_JAPANESE;
                case "18":
                    return MODI.MiLANGUAGES.miLANG_KOREAN;
                case "19":
                    return MODI.MiLANGUAGES.miLANG_DUTCH;
                case "20":
                    return MODI.MiLANGUAGES.miLANG_NORWEGIAN;
                case "21":
                    return MODI.MiLANGUAGES.miLANG_POLISH;
                case "22":
                    return MODI.MiLANGUAGES.miLANG_PORTUGUESE;
                case "25":
                    return MODI.MiLANGUAGES.miLANG_RUSSIAN;
                case "29":
                    return MODI.MiLANGUAGES.miLANG_SWEDISH;
                case "31":
                    return MODI.MiLANGUAGES.miLANG_TURKISH;
                case "1028":
                    return MODI.MiLANGUAGES.miLANG_CHINESE_TRADITIONAL;
                default:
                    return MODI.MiLANGUAGES.miLANG_ENGLISH;
            }
        }
 
 
        /// <summary>
        ///  Images轉換文字
        /// </summary>
        /// <param name="image">Image</param>
        /// <param name="language">语言类型</param>
        /// <returns></returns>
        public static string ExtractText(this System.Drawing.Image image,string language)
        {
            var tmpFile = Path.GetTempFileName();
            StringBuilder sb = new StringBuilder();
            //string text;
            try
            {
                var bmp = new Bitmap(Math.Max(image.Width, 1024), Math.Max(image.Height, 768));
                var gfxResize = Graphics.FromImage(bmp);
                gfxResize.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height));
                bmp.Save(tmpFile + ".bmp", ImageFormat.Bmp);
                var doc = new MODI.Document();
                doc.Create(tmpFile + ".bmp");
                // doc.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, true, true);
                doc.OCR(GetLanuageType(language), true, true);  // 识别文字类型
                var img = (MODI.Image)doc.Images[0];
                var layout = img.Layout;
                sb.Append(layout.Text);
                //text = sb.ToString();// layout.Text;
            }
            finally
            {
                File.Delete(tmpFile);
                File.Delete(tmpFile + ".bmp");
            }
 
            return sb.ToString();// text;
        }
        /// <summary>
        /// 来源图片文件轉換文字
        /// </summary>
        /// <param name="fileToOCR">file文件</param>
        /// <param name="language">语言类型</param>
        /// <returns></returns>
        public static string getFileToOCR(string fileToOCR, string language)
        {
            StringBuilder sb = new StringBuilder();
            if (File.Exists(fileToOCR))
            {
 
                MODI.Document md = new MODI.Document();
                md.Create(fileToOCR);
                md.OCR(GetLanuageType(language), true, true);
                MODI.Image img;
                MODI.Layout layout;
                for (int i = 0; i < md.Images.Count; i++)
                {
                    img = (MODI.Image)md.Images[i];
                    layout = img.Layout;
                    sb.Append(layout.Text);
                }
 
                md.Close(false);
                 
            }
            else
            {
                sb.Append("");
            }
             return sb.ToString();
        }
    }
}

 

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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
 
namespace ToText
{
 
    /// <summary>
    /// geovindu
    /// </summary>
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        #region DllImport
        [DllImport("AspriseOCR.dll", EntryPoint = "OCR", CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr OCR(string file, int type);
 
        [DllImport("AspriseOCR.dll", EntryPoint = "OCRpart", CallingConvention = CallingConvention.Cdecl)]
        static extern IntPtr OCRpart(string file, int type, int startX, int startY, int width, int height);
 
        [DllImport("AspriseOCR.dll", EntryPoint = "OCRBarCodes", CallingConvention = CallingConvention.Cdecl)]
        static extern IntPtr OCRBarCodes(string file, int type);
 
        [DllImport("AspriseOCR.dll", EntryPoint = "OCRpartBarCodes", CallingConvention = CallingConvention.Cdecl)]
        static extern IntPtr OCRpartBarCodes(string file, int type, int startX, int startY, int width, int height);
        #endregion
 
        #region 转换按钮事件
        /// <summary>
        /// 转换按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            int startX = 0;
            int startY = 0;
            int width = -1;
            int height = -1;
 
            string img_path = txt_imgpath.Text; // 图片路径
            if (String.IsNullOrEmpty(img_path)) // 图片非空验证
            {
                MessageBox.Show("请先选择图片!");
                return;
            }
            try
            {
                Image img = Image.FromFile(img_path);
                width = img.Width;
                height = img.Height;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace);
            }
            txt_result.Text = Marshal.PtrToStringAnsi(OCRpart(img_path, -1, startX, startY, width, height));
        }
        #endregion
 
        #region 浏览事件
        /// <summary>
        /// 浏览事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_imgpath_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            txt_imgpath.Text = openFileDialog1.FileName;
        }
        /// <summary>
        /// 浏览图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txt_imgpath_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            txt_imgpath.Text = openFileDialog1.FileName;
        }
        #endregion
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
 
        }
       
 
    }
}

  

 

posted @   ®Geovin Du Dream Park™  阅读(855)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
点击右上角即可分享
微信分享提示