C#通过Spire.OCR读取图片文字

发布时间 2023-05-05 14:38:33作者: 流纹

1、项目属性修改

首先创建一个winform窗体程序,然后将其目标平台属性修改为【×64】,【Spire.OCR】只支持【×64】,所以这一步不能少

  2、添加引用

通过管理Nuget包实现添加引用步骤

 3、包安装完成后将包中dll复制到项目文件夹>bin>Debug目录下

 

4、代码中实现选择图片并提取文字效果实现

/// <summary>
        /// 图片文字提取
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_PicWordsExtract_Click(object sender, EventArgs e)
        {
            try
            {
                //选择图片
                OpenFileDialog fileDialog = new OpenFileDialog();
                fileDialog.Multiselect = false;
                fileDialog.DefaultExt = "*.jpg";
                fileDialog.Filter = "图片|*.jpg";
                if (fileDialog.ShowDialog() == DialogResult.OK)
                {
                    if (fileDialog.FileNames != null && fileDialog.FileNames.Any())
                    {
                        foreach (var file in fileDialog.FileNames)
                        {
                            Spire.OCR.OcrScanner ocr = new Spire.OCR.OcrScanner();
                            //将图片地址添加到OCR程序并运行
                            ocr.Scan(file);
                            //读取图片文字
                            string text = ocr.Text.ToString();
                            if (!string.IsNullOrWhiteSpace(text))
                            {
                                //由于版本问题所以提取出的文字后会附加上【Evaluation Warning : The version can be used only for evaluation purpose...】
                                //这里需要替换掉这句话
                                text = text.Replace("Evaluation Warning : The version can be used only for evaluation purpose...", "");
                                //将读取结果赋值
                                txt_WordsExtract.Text = text;
                                //保存扫描获取的文字为.txt文档
                                System.IO.File.WriteAllText("C:\\Users\\Administrator\\Desktop\\新建文本文档.txt", text);
                                System.Diagnostics.Process.Start("C:\\Users\\Administrator\\Desktop\\新建文本文档.txt");
                            }
                            else
                            {
                                txt_WordsExtract.Text = "读取失败";
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                txt_WordsExtract.Text = "读取失败:" + ex.Message;
            }
        }
View Code

 

5、界面调用

 try
            {
                //选择图片
                OpenFileDialog fileDialog = new OpenFileDialog();
                fileDialog.Multiselect = false;
                fileDialog.DefaultExt = "*.jpg";
                fileDialog.Filter = "图片|*.jpg";
                if (fileDialog.ShowDialog() == DialogResult.OK)
                {
                    if (fileDialog.FileNames != null && fileDialog.FileNames.Any())
                    {
                        foreach (var file in fileDialog.FileNames)
                        {
                            Spire.OCR.OcrScanner ocr = new Spire.OCR.OcrScanner();
                            //将图片地址添加到OCR程序并运行
                            ocr.Scan(file);
                            //读取图片文字
                            string text = ocr.Text.ToString();
                            if (!string.IsNullOrWhiteSpace(text))
                            {
                                //由于版本问题所以提取出的文字后会附加上【Evaluation Warning : The version can be used only for evaluation purpose...】
                                //我这里只是测试使用一下所以就没特地去找免费版dll,只是替换掉版本问题这句话
                                //如需在实际项目中使用可以找一下,或者自行购买正版
                                text = text.Replace("Evaluation Warning : The version can be used only for evaluation purpose...", "");
                                //将读取结果赋值
                                txt_WordsExtract.Text = text;
                                //保存扫描获取的文字为.txt文档
                                System.IO.File.WriteAllText("C:\\Users\\Administrator\\Desktop\\新建文本文档.txt", text);
                                System.Diagnostics.Process.Start("C:\\Users\\Administrator\\Desktop\\新建文本文档.txt");
                            }
                            else
                            {
                                txt_WordsExtract.Text = "读取失败";
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                txt_WordsExtract.Text = "读取失败:" + ex.Message;
            }
View Code

 

6、运行结果

 目前来说提取文字效果以达成,不过提取后一些特殊的标点符号,还是会出现错误或未提取出来的情况。