接楼下的代码
preprocessing.h如下:
/*
* preprocessing.h
*
*
* Created by damiles on 18/11/08.
* Copyright 2008 __MyCompanyName__. All rights reserved.
*
*/
#ifdef _CH_
#pragma package <opencv>
#endif
#ifndef _EiC
#include "cv.h"
#include "highgui.h"
#include <stdio.h>
#include <ctype.h>
#endif
IplImage preprocessing(IplImage* imgSrc,int new_width, int new_height);
preprocessing.c如下:
/*
* preprocessing.c
*
*
* Created by damiles on 18/11/08.
* Copyright 2008 __MyCompanyName__. All rights reserved.
*
*/
#include "preprocessing.h"
#ifdef _CH_
#pragma package <opencv>
#endif
#ifndef _EiC
#include "cv.h"
#include "highgui.h"
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#endif
/*****************************************************************
*
* Find the min box. The min box respect original aspect ratio image
* The image is a binary data and background is white.
*
*******************************************************************/
void findX(IplImage* imgSrc,int* min, int* max){
int i;
int minFound=0;
CvMat data;
CvScalar maxVal=cvRealScalar(imgSrc->width * 255);
CvScalar val=cvRealScalar(0);
//For each col sum, if sum < width*255 then we find the min
//then continue to end to search the max, if sum< width*255 then is new max
for (i=0; i< imgSrc->width; i++){
cvGetCol(imgSrc, &data, i);
val= cvSum(&data);
if(val.val[0] < maxVal.val[0]){
*max= i;
if(!minFound){
*min= i;
minFound= 1;
}
}
}
}
void findY(IplImage* imgSrc,int* min, int* max){
int i;
int minFound=0;
CvMat data;
CvScalar maxVal=cvRealScalar(imgSrc->width * 255);
CvScalar val=cvRealScalar(0);
//For each col sum, if sum < width*255 then we find the min
//then continue to end to search the max, if sum< width*255 then is new max
for (i=0; i< imgSrc->height; i++){
cvGetRow(imgSrc, &data, i);
val= cvSum(&data);
if(val.val[0] < maxVal.val[0]){
*max=i;
if(!minFound){
*min= i;
minFound= 1;
}
}
}
}
CvRect findBB(IplImage* imgSrc){
CvRect aux;
int xmin, xmax, ymin, ymax;
xmin=xmax=ymin=ymax=0;
findX(imgSrc, &xmin, &xmax);
findY(imgSrc, &ymin, &ymax);
aux=cvRect(xmin, ymin, xmax-xmin, ymax-ymin);
return aux;
}
IplImage preprocessing(IplImage* imgSrc,int new_width, int new_height){
IplImage* result;
IplImage* scaledResult;
CvMat data;
CvMat dataA;
CvRect bb;//bounding box
CvRect bba;//boundinb box maintain aspect ratio
//Find bounding box
bb=findBB(imgSrc);
//Get bounding box data and no with aspect ratio, the x and y can be corrupted
cvGetSubRect(imgSrc, &data, cvRect(bb.x, bb.y, bb.width, bb.height));
//Create image with this data with width and height with aspect ratio 1
//then we get highest size betwen width and height of our bounding box
int size=(bb.width>bb.height)?bb.width:bb.height;
result=cvCreateImage( cvSize( size, size ), 8, 1 );
cvSet(result,CV_RGB(255,255,255),NULL);
//Copy de data in center of image
int x=(int)floor((float)(size-bb.width)/2.0f);
int y=(int)floor((float)(size-bb.height)/2.0f);
cvGetSubRect(result, &dataA, cvRect(x,y,bb.width, bb.height));
cvCopy(&data, &dataA, NULL);
//Scale result
scaledResult=cvCreateImage( cvSize( new_width, new_height ), 8, 1 );
cvResize(result, scaledResult, CV_INTER_NN);
//Return processed data
return *scaledResult;
}
6 个解决方案
#1
我找到的代码,但是看的不是很懂,求大神指导,编译能通过,加头文件一共5个文件
main.c
main.c
#ifdef _CH_
#pragma package <opencv>
#endif
#ifndef _EiC
#include "cv.h"
#include "highgui.h"
#include <stdio.h>
#include <ctype.h>
#include "basicOCR.h"
#endif
IplImage* imagen;
int red,green,blue;
IplImage* screenBuffer;
int drawing;
int r,last_x, last_y;
void draw(int x,int y){
//Draw a circle where is the mouse
cvCircle(imagen, cvPoint(x,y), r, CV_RGB(red,green,blue), -1, 4, 0);
//Get clean copy of image
screenBuffer=cvCloneImage(imagen);
cvShowImage( "Demo", screenBuffer );
}
void drawCursor(int x, int y){
//Get clean copy of image
screenBuffer=cvCloneImage(imagen);
//Draw a circle where is the mouse
cvCircle(screenBuffer, cvPoint(x,y), r, CV_RGB(0,0,0), 1, 4, 0);
}
/*************************
* Mouse CallBack
* event:
* #define CV_EVENT_MOUSEMOVE 0
* #define CV_EVENT_LBUTTONDOWN 1
* #define CV_EVENT_RBUTTONDOWN 2
* #define CV_EVENT_MBUTTONDOWN 3
* #define CV_EVENT_LBUTTONUP 4
* #define CV_EVENT_RBUTTONUP 5
* #define CV_EVENT_MBUTTONUP 6
* #define CV_EVENT_LBUTTONDBLCLK 7
* #define CV_EVENT_RBUTTONDBLCLK 8
* #define CV_EVENT_MBUTTONDBLCLK 9
*
* x, y: mouse position
*
* flag:
* #define CV_EVENT_FLAG_LBUTTON 1
* #define CV_EVENT_FLAG_RBUTTON 2
* #define CV_EVENT_FLAG_MBUTTON 4
* #define CV_EVENT_FLAG_CTRLKEY 8
* #define CV_EVENT_FLAG_SHIFTKEY 16
* #define CV_EVENT_FLAG_ALTKEY 32
*
**************************/
void on_mouse( int event, int x, int y, int flags, void* param )
{
last_x=x;
last_y=y;
drawCursor(x,y);
//Select mouse Event
if(event==CV_EVENT_LBUTTONDOWN)
{
drawing=1;
draw(x,y);
}
else if(event==CV_EVENT_LBUTTONUP)
{
//drawing=!drawing;
drawing=0;
}
else if(event == CV_EVENT_MOUSEMOVE && flags & CV_EVENT_FLAG_LBUTTON)
{
if(drawing)
draw(x,y);
}
}
int main( int argc, char** argv )
{
printf( "Basic OCR by David Millan Escriva | Damiles\n"
"Hot keys: \n"
"\tr - reset image\n"
"\t+ - cursor radio ++\n"
"\t- - cursor radio --\n"
"\ts - Save image as out.png\n"
"\tc - Classify image, the result in console\n"
"\tESC - quit the program\n");
drawing=0;
r=10;
red=green=blue=0;
last_x=last_y=red=green=blue=0;
//Create image
imagen=cvCreateImage(cvSize(128,128),IPL_DEPTH_8U,1);
//Set data of image to white
cvSet(imagen, CV_RGB(255,255,255),NULL);
//Image we show user with cursor and other artefacts we need
screenBuffer=cvCloneImage(imagen);
//Create window
cvNamedWindow( "Demo", 0 );
cvResizeWindow("Demo", 128,128);
//Create mouse CallBack
cvSetMouseCallback("Demo",&on_mouse, 0 );
//////////////////
//My OCR
//////////////////
basicOCR ocr;
//Main Loop
for(;;)
{
int c;
cvShowImage( "Demo", screenBuffer );
c = cvWaitKey(10);
if( (char) c == 27 )
break;
if( (char) c== '+' ){
r++;
drawCursor(last_x,last_y);
}
if( ((char)c== '-') && (r>1) ){
r--;
drawCursor(last_x,last_y);
}
if( (char)c== 'r'){
cvSet(imagen, cvRealScalar(255),NULL);
drawCursor(last_x,last_y);
}
if( (char)c== 's'){
cvSaveImage("out.png", imagen);
}
if( (char)c=='c'){
ocr.classify(imagen,1);
}
}
cvDestroyWindow("Demo");
return 0;
}
#ifdef _EiC
main(1,"mouseEvent.c");
#endif
#2
basicORC.h如下:
/*
* preprocessing.h
*
*
* Created by damiles on 18/11/08.
* Copyright 2008 __MyCompanyName__. All rights reserved.
*
*/
#ifdef _CH_
#pragma package <opencv>
#endif
#ifndef _EiC
#include "cv.h"
#include "highgui.h"
#include "ml.h"
#include <stdio.h>
#include <ctype.h>
#endif
class basicOCR{
public:
float classify(IplImage* img,int showResult);
basicOCR ();
void test();
private:
char file_path[255];
int train_samples;
int classes;
CvMat* trainData;
CvMat* trainClasses;
int size;
static const int K=10;
CvKNearest *knn;
void getData();
void train();
};
#3
basicORC.CPP如下:
/*
* basicOCR.c
*
*
* Created by damiles on 18/11/08.
* Copyright 2008 Damiles. GPL License
*
*/
#ifdef _CH_
#pragma package <opencv>
#endif
#ifndef _EiC
#include "cv.h"
#include "highgui.h"
#include "ml.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#endif
#include "preprocessing.h"
#include "basicOCR.h"
/*
char file_path[] = "../OCR/";
int train_samples = 50;
int classes= 10;
CvMat* trainData;
CvMat* trainClasses;
int size=40;
const int K=10;
CvKNearest *knn;
*/
void basicOCR::getData()
{
IplImage* src_image;
IplImage prs_image;
CvMat row,data;
char file[255];
int i,j;
for(i =0; i<classes; i++){
for( j = 0; j< train_samples; j++){
//Load file
if(j<10)
sprintf(file,"%s%d/%d0%d.pbm",file_path, i, i , j);
else
sprintf(file,"%s%d/%d%d.pbm",file_path, i, i , j);
src_image = cvLoadImage(file,0);
if(!src_image){
printf("Error: Cant load image %s\n", file);
//exit(-1);
}
//process file
prs_image = preprocessing(src_image, size, size);
//Set class label
cvGetRow(trainClasses, &row, i*train_samples + j);
cvSet(&row, cvRealScalar(i));
//Set data
cvGetRow(trainData, &row, i*train_samples + j);
IplImage* img = cvCreateImage( cvSize( size, size ), IPL_DEPTH_32F, 1 );
//convert 8 bits image to 32 float image
cvConvertScale(&prs_image, img, 0.0039215, 0);
cvGetSubRect(img, &data, cvRect(0,0, size,size));
CvMat row_header, *row1;
//convert data matrix sizexsize to vecor
row1 = cvReshape( &data, &row_header, 0, 1 );
cvCopy(row1, &row, NULL);
}
}
}
void basicOCR::train()
{
knn=new CvKNearest( trainData, trainClasses, 0, false, K );
}
float basicOCR::classify(IplImage* img, int showResult)
{
IplImage prs_image;
CvMat data;
CvMat* nearest=cvCreateMat(1,K,CV_32FC1);
float result;
//process file
prs_image = preprocessing(img, size, size);
//Set data
IplImage* img32 = cvCreateImage( cvSize( size, size ), IPL_DEPTH_32F, 1 );
cvConvertScale(&prs_image, img32, 0.0039215, 0);
cvGetSubRect(img32, &data, cvRect(0,0, size,size));
CvMat row_header, *row1;
row1 = cvReshape( &data, &row_header, 0, 1 );
result=knn->find_nearest(row1,K,0,0,nearest,0);
int accuracy=0;
for(int i=0;i<K;i++){
if( nearest->data.fl[i] == result)
accuracy++;
}
float pre=100*((float)accuracy/(float)K);
if(showResult==1){
printf("|\t%.0f\t| \t%.2f%% \t| \t%d of %d \t| \n",result,pre,accuracy,K);
printf(" ---------------------------------------------------------------\n");
}
return result;
}
void basicOCR::test(){
IplImage* src_image;
IplImage prs_image;
CvMat row,data;
char file[255];
int i,j;
int error=0;
int testCount=0;
for(i =0; i<classes; i++){
for( j = 50; j< 50+train_samples; j++){
sprintf(file,"%s%d/%d%d.pbm",file_path, i, i , j);
src_image = cvLoadImage(file,0);
if(!src_image){
printf("Error: Cant load image %s\n", file);
//exit(-1);
}
//process file
prs_image = preprocessing(src_image, size, size);
float r=classify(&prs_image,0);
if((int)r!=i)
error++;
testCount++;
}
}
float totalerror=100*(float)error/(float)testCount;
printf("System Error: %.2f%%\n", totalerror);
}
basicOCR::basicOCR()
{
//initial
sprintf(file_path , "../OCR/");
train_samples = 50;
classes= 10;
size=40;
trainData = cvCreateMat(train_samples*classes, size*size, CV_32FC1);
trainClasses = cvCreateMat(train_samples*classes, 1, CV_32FC1);
//Get data (get images and process it)
getData();
//train
train();
//Test
test();
printf(" ---------------------------------------------------------------\n");
printf("|\tClass\t|\tPrecision\t|\tAccuracy\t|\n");
printf(" ---------------------------------------------------------------\n");
}
#4
编译完运行后,能不能实现功能呀?
#5
楼主最终怎么做的,我最近也在做这样的项目
#6
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了, 是不是很简单
#1
我找到的代码,但是看的不是很懂,求大神指导,编译能通过,加头文件一共5个文件
main.c
main.c
#ifdef _CH_
#pragma package <opencv>
#endif
#ifndef _EiC
#include "cv.h"
#include "highgui.h"
#include <stdio.h>
#include <ctype.h>
#include "basicOCR.h"
#endif
IplImage* imagen;
int red,green,blue;
IplImage* screenBuffer;
int drawing;
int r,last_x, last_y;
void draw(int x,int y){
//Draw a circle where is the mouse
cvCircle(imagen, cvPoint(x,y), r, CV_RGB(red,green,blue), -1, 4, 0);
//Get clean copy of image
screenBuffer=cvCloneImage(imagen);
cvShowImage( "Demo", screenBuffer );
}
void drawCursor(int x, int y){
//Get clean copy of image
screenBuffer=cvCloneImage(imagen);
//Draw a circle where is the mouse
cvCircle(screenBuffer, cvPoint(x,y), r, CV_RGB(0,0,0), 1, 4, 0);
}
/*************************
* Mouse CallBack
* event:
* #define CV_EVENT_MOUSEMOVE 0
* #define CV_EVENT_LBUTTONDOWN 1
* #define CV_EVENT_RBUTTONDOWN 2
* #define CV_EVENT_MBUTTONDOWN 3
* #define CV_EVENT_LBUTTONUP 4
* #define CV_EVENT_RBUTTONUP 5
* #define CV_EVENT_MBUTTONUP 6
* #define CV_EVENT_LBUTTONDBLCLK 7
* #define CV_EVENT_RBUTTONDBLCLK 8
* #define CV_EVENT_MBUTTONDBLCLK 9
*
* x, y: mouse position
*
* flag:
* #define CV_EVENT_FLAG_LBUTTON 1
* #define CV_EVENT_FLAG_RBUTTON 2
* #define CV_EVENT_FLAG_MBUTTON 4
* #define CV_EVENT_FLAG_CTRLKEY 8
* #define CV_EVENT_FLAG_SHIFTKEY 16
* #define CV_EVENT_FLAG_ALTKEY 32
*
**************************/
void on_mouse( int event, int x, int y, int flags, void* param )
{
last_x=x;
last_y=y;
drawCursor(x,y);
//Select mouse Event
if(event==CV_EVENT_LBUTTONDOWN)
{
drawing=1;
draw(x,y);
}
else if(event==CV_EVENT_LBUTTONUP)
{
//drawing=!drawing;
drawing=0;
}
else if(event == CV_EVENT_MOUSEMOVE && flags & CV_EVENT_FLAG_LBUTTON)
{
if(drawing)
draw(x,y);
}
}
int main( int argc, char** argv )
{
printf( "Basic OCR by David Millan Escriva | Damiles\n"
"Hot keys: \n"
"\tr - reset image\n"
"\t+ - cursor radio ++\n"
"\t- - cursor radio --\n"
"\ts - Save image as out.png\n"
"\tc - Classify image, the result in console\n"
"\tESC - quit the program\n");
drawing=0;
r=10;
red=green=blue=0;
last_x=last_y=red=green=blue=0;
//Create image
imagen=cvCreateImage(cvSize(128,128),IPL_DEPTH_8U,1);
//Set data of image to white
cvSet(imagen, CV_RGB(255,255,255),NULL);
//Image we show user with cursor and other artefacts we need
screenBuffer=cvCloneImage(imagen);
//Create window
cvNamedWindow( "Demo", 0 );
cvResizeWindow("Demo", 128,128);
//Create mouse CallBack
cvSetMouseCallback("Demo",&on_mouse, 0 );
//////////////////
//My OCR
//////////////////
basicOCR ocr;
//Main Loop
for(;;)
{
int c;
cvShowImage( "Demo", screenBuffer );
c = cvWaitKey(10);
if( (char) c == 27 )
break;
if( (char) c== '+' ){
r++;
drawCursor(last_x,last_y);
}
if( ((char)c== '-') && (r>1) ){
r--;
drawCursor(last_x,last_y);
}
if( (char)c== 'r'){
cvSet(imagen, cvRealScalar(255),NULL);
drawCursor(last_x,last_y);
}
if( (char)c== 's'){
cvSaveImage("out.png", imagen);
}
if( (char)c=='c'){
ocr.classify(imagen,1);
}
}
cvDestroyWindow("Demo");
return 0;
}
#ifdef _EiC
main(1,"mouseEvent.c");
#endif
#2
basicORC.h如下:
/*
* preprocessing.h
*
*
* Created by damiles on 18/11/08.
* Copyright 2008 __MyCompanyName__. All rights reserved.
*
*/
#ifdef _CH_
#pragma package <opencv>
#endif
#ifndef _EiC
#include "cv.h"
#include "highgui.h"
#include "ml.h"
#include <stdio.h>
#include <ctype.h>
#endif
class basicOCR{
public:
float classify(IplImage* img,int showResult);
basicOCR ();
void test();
private:
char file_path[255];
int train_samples;
int classes;
CvMat* trainData;
CvMat* trainClasses;
int size;
static const int K=10;
CvKNearest *knn;
void getData();
void train();
};
#3
basicORC.CPP如下:
/*
* basicOCR.c
*
*
* Created by damiles on 18/11/08.
* Copyright 2008 Damiles. GPL License
*
*/
#ifdef _CH_
#pragma package <opencv>
#endif
#ifndef _EiC
#include "cv.h"
#include "highgui.h"
#include "ml.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#endif
#include "preprocessing.h"
#include "basicOCR.h"
/*
char file_path[] = "../OCR/";
int train_samples = 50;
int classes= 10;
CvMat* trainData;
CvMat* trainClasses;
int size=40;
const int K=10;
CvKNearest *knn;
*/
void basicOCR::getData()
{
IplImage* src_image;
IplImage prs_image;
CvMat row,data;
char file[255];
int i,j;
for(i =0; i<classes; i++){
for( j = 0; j< train_samples; j++){
//Load file
if(j<10)
sprintf(file,"%s%d/%d0%d.pbm",file_path, i, i , j);
else
sprintf(file,"%s%d/%d%d.pbm",file_path, i, i , j);
src_image = cvLoadImage(file,0);
if(!src_image){
printf("Error: Cant load image %s\n", file);
//exit(-1);
}
//process file
prs_image = preprocessing(src_image, size, size);
//Set class label
cvGetRow(trainClasses, &row, i*train_samples + j);
cvSet(&row, cvRealScalar(i));
//Set data
cvGetRow(trainData, &row, i*train_samples + j);
IplImage* img = cvCreateImage( cvSize( size, size ), IPL_DEPTH_32F, 1 );
//convert 8 bits image to 32 float image
cvConvertScale(&prs_image, img, 0.0039215, 0);
cvGetSubRect(img, &data, cvRect(0,0, size,size));
CvMat row_header, *row1;
//convert data matrix sizexsize to vecor
row1 = cvReshape( &data, &row_header, 0, 1 );
cvCopy(row1, &row, NULL);
}
}
}
void basicOCR::train()
{
knn=new CvKNearest( trainData, trainClasses, 0, false, K );
}
float basicOCR::classify(IplImage* img, int showResult)
{
IplImage prs_image;
CvMat data;
CvMat* nearest=cvCreateMat(1,K,CV_32FC1);
float result;
//process file
prs_image = preprocessing(img, size, size);
//Set data
IplImage* img32 = cvCreateImage( cvSize( size, size ), IPL_DEPTH_32F, 1 );
cvConvertScale(&prs_image, img32, 0.0039215, 0);
cvGetSubRect(img32, &data, cvRect(0,0, size,size));
CvMat row_header, *row1;
row1 = cvReshape( &data, &row_header, 0, 1 );
result=knn->find_nearest(row1,K,0,0,nearest,0);
int accuracy=0;
for(int i=0;i<K;i++){
if( nearest->data.fl[i] == result)
accuracy++;
}
float pre=100*((float)accuracy/(float)K);
if(showResult==1){
printf("|\t%.0f\t| \t%.2f%% \t| \t%d of %d \t| \n",result,pre,accuracy,K);
printf(" ---------------------------------------------------------------\n");
}
return result;
}
void basicOCR::test(){
IplImage* src_image;
IplImage prs_image;
CvMat row,data;
char file[255];
int i,j;
int error=0;
int testCount=0;
for(i =0; i<classes; i++){
for( j = 50; j< 50+train_samples; j++){
sprintf(file,"%s%d/%d%d.pbm",file_path, i, i , j);
src_image = cvLoadImage(file,0);
if(!src_image){
printf("Error: Cant load image %s\n", file);
//exit(-1);
}
//process file
prs_image = preprocessing(src_image, size, size);
float r=classify(&prs_image,0);
if((int)r!=i)
error++;
testCount++;
}
}
float totalerror=100*(float)error/(float)testCount;
printf("System Error: %.2f%%\n", totalerror);
}
basicOCR::basicOCR()
{
//initial
sprintf(file_path , "../OCR/");
train_samples = 50;
classes= 10;
size=40;
trainData = cvCreateMat(train_samples*classes, size*size, CV_32FC1);
trainClasses = cvCreateMat(train_samples*classes, 1, CV_32FC1);
//Get data (get images and process it)
getData();
//train
train();
//Test
test();
printf(" ---------------------------------------------------------------\n");
printf("|\tClass\t|\tPrecision\t|\tAccuracy\t|\n");
printf(" ---------------------------------------------------------------\n");
}
#4
编译完运行后,能不能实现功能呀?
#5
楼主最终怎么做的,我最近也在做这样的项目
#6
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了, 是不是很简单