15,440
社区成员
发帖
与我相关
我的任务
分享
VB实现OCR文字识别
原理: 利用微软OCR控件, 只需要不到10行代码就能够实现自已的OCR文字识别软件.
1. 添加控件,需要安装office2003, 没有安装office2003的可以从别人机子上拷贝相关文件,注册regsvr32.exe mdivwctl.dll,
控件一般在这个目录下:C:\Program Files\Common Files\Microsoft Shared\MODI\11.0, 只需要相关的几个文件就可以了, 此文件夹全部文件大概在21M左右.
工程->部件->添加这个控件:Microsoft Office Document Imaging 11.0 Type Library
2.在按钮的Click事件里:
Dim strLayoutInfo As String, strLPN As String
'初始化并加载文档
Set miDoc = CreateObject("MODI.Document") '创建对象
miDoc.Create "D:\未命名.tif" '加载图片文件(必须是黑白二值图)
Screen.MousePointer = vbHourglass '设置光标忙
'识别
miDoc.Images(0).OCR miLANG_CHINESE_SIMPLIFIED, True, True '有用的就此一句,识别为中文简体
Set modiLayout = miDoc.Images(0).Layout '读出数据
strLayoutInfo = _
"Language: " & modiLayout.Language & vbCrLf & _
"Number of characters: " & modiLayout.NumChars & vbCrLf & _
"Number of fonts: " & modiLayout.NumFonts & vbCrLf & _
"Number of words: " & modiLayout.NumWords & vbCrLf & _
"Beginning of text: " & Left(modiLayout.Text, 50) & vbCrLf & _
"First word of text: " & modiLayout.Words(0).Text
MsgBox strLayoutInfo, vbInformation + vbOKOnly, "Layout Information"
Set modiLayout = Nothing
Set miDoc = Nothing
Screen.MousePointer = vbDefault
3. OK了, 是不是很简单