赞
踩
本文这里是通过WinForm实现的测试效果
1、首先创建一个winform窗体程序,然后将其属性修改为【×64】,【Spire.OCR】只支持【×64】,所以这一步不能少
2、通过管理Nuget包添加引用
3、安装完成后将包中dll复制到项目bin文件夹下
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...】 //我这里实际上只是学习测试使用,所以并不在乎版本水印问题,也就没有特地去找免费版的DLL,所以只需要替换掉这句话,那么对我来说效果就已经实现了 //当然在实际项目当中使用的话个人建议还是不要参照我这个处理方式,还是选择购买正版或者找下那种免费的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; } }
5、运行结果
待提取文字图片如下:
提取结果如下:
目前来说提取文字效果以达成,不过提取后一些特殊的标点符号,还是会出现错误或未提取出来的情况。
个人记录,不喜勿喷
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。