[疑难] 关于图形识别。

hurd 2009-02-21
想做个识别验证码的软件, 找了个tesseract-ocr, 写了个符号导入模块。
module tessdll;

import 
	tango.stdc.time,
	tango.sys.Sharedlib,
	tango.stdc.stringz;

alias ubyte	uinT8;
alias byte	inT8;
alias ushort	uinT16;
alias short	inT16;
alias uint	uinT32;
alias int	inT32;

alias ubyte	unsigned_char;
alias short	c_int;


//bool (*CANCEL_FUNC)(void* cancel_this, int words);
alias bool function(void*,c_int) CANCEL_FUNC;

struct EANYCODE_CHAR {                  /*single character */
	// It should be noted that the format for char_code for version 2.0 and beyond is UTF8
	// which means that ASCII characters will come out as one structure but other characters
	// will be returned in two or more instances of this structure with a single byte of the
	// UTF8 code in each, but each will have the same bounding box.
	// Programs which want to handle languagues with different characters sets will need to
	// handle extended characters appropriately, but *all* code needs to be prepared to
	// receive UTF8 coded characters for characters such as bullet and fancy quotes.
	uinT16 char_code;              /*character itself */
	inT16 left;                    /*of char (-1) */
	inT16 right;                   /*of char (-1) */
	inT16 top;                     /*of char (-1) */
	inT16 bottom;                  /*of char (-1) */
	inT16 font_index;              /*what font (0) */
	uinT8 confidence;              /*0=perfect, 100=reject (0/100) */
	uinT8 point_size;              /*of char, 72=i inch, (10) */
	inT8 blanks;                   /*no of spaces before this char (1) */
}

struct ETEXT_DESC{			//ETEXT_STRUCT    output header 
	inT16 count;                   /*chars in this buffer(0) */
	inT16 progress;                /*percent complete increasing (0-100) */
	inT8 more_to_come;             /*true if not last */
	inT8 ocr_alive;                /*ocr sets to 1, HP 0 */
	inT8 err_code;                 /*for errcode use */
	CANCEL_FUNC cancel;            /*returns true to cancel */
	void* cancel_this;             /*this or other data for cancel*/
	clock_t end_time;              /*time to stop if not 0*/
	EANYCODE_CHAR text[1];         /*character data */
}


extern(C){
	//TESSDLL_API int __cdecl TessDllBeginPage(uinT32 xsize,uinT32 ysize,  unsigned char *buf);
	c_int function(uinT32, uinT32, ubyte*) TessDllBeginPage;
	
	//TESSDLL_API int __cdecl TessDllBeginPageLang(uinT32 xsize,uinT32 ysize, unsigned char *buf, const char* lang);
	c_int function(uinT32, uinT32, ubyte*, char*) TessDllBeginPageLang;
	
	//TESSDLL_API int __cdecl TessDllBeginPageUpright(uinT32 xsize,uinT32 ysize, unsigned char *buf,  const char* lang);
	c_int function(uinT32, uinT32, ubyte*, char*) TessDllBeginPageUpright;
	
	//Added in version 2.0 to allow users to specify bytes per pixel to do
	//1 for binary biptmap
	//8 for gray
	//24 bit for color RGB
	//TESSDLL_API int __cdecl TessDllBeginPageBPP(uinT32 xsize, uinT32 ysize, unsigned char *buf, uinT8 bpp);
	c_int function(uinT32, uinT32, ubyte*, uinT8) TessDllBeginPageBPP;
	
	//TESSDLL_API int __cdecl TessDllBeginPageLangBPP(uinT32 xsize,uinT32 ysize, unsigned char *buf, const char* lang,uinT8 bpp);
	c_int function(uinT32, uinT32, ubyte*, char*, uinT8) TessDllBeginPageLangBPP;
	
	//TESSDLL_API int __cdecl TessDllBeginPageUprightBPP(uinT32 xsize,uinT32 ysize, unsigned char *buf, const char* lang,uinT8 bpp);
	c_int function(uinT32, uinT32, ubyte*, char*, uinT8) TessDllBeginPageUprightBPP;
	
	// TESSDLL_API void __cdecl TessDllEndPage(void);
	void function(ubyte) TessDllEndPage;
	
	//This allows you to extract one word or section from the bitmap or
	//the whole page
	//To extract the whole page just enter zeros for left, right, top, bottom
	//Note: getting one word at time is not yet optimized for speed.
	//limit of 32000 character can be returned
	//see ocrclass.h for a decription of the ETEXT_DESC file
	//TESSDLL_API ETEXT_DESC * __cdecl TessDllRecognize_a_Block(uinT32 left, uinT32 right,  uinT32 top, uinT32 bottom);
	ETEXT_DESC* function(uinT32, uinT32, uinT32, uinT32) TessDllRecognize_a_Block;
	
	//TESSDLL_API ETEXT_DESC * __cdecl TessDllRecognize_all_Words();
	ETEXT_DESC* function() TessDllRecognize_all_Words;
	
	//This will release any memory associated with the recognize class object
	//TESSDLL_API void __cdecl TessDllRelease();
	void function() TessDllRelease;
}

void bind(alias T)(SharedLib lib){
	//pragma(msg, T.stringof);
	T	= cast( typeof(T) )	lib.getSymbol(toStringz(T.stringof));
	assert(T !is null);
}

static this(){
	auto lib = SharedLib.load(`tessdll.dll`);
	if( lib is null){
		throw new Exception(`load tessdll.dll error`);
	}else{
		bind!(TessDllBeginPage)(lib);
		bind!(TessDllBeginPageLang)(lib);
		bind!(TessDllBeginPageBPP)(lib);
		bind!(TessDllBeginPageBPP)(lib);
		bind!(TessDllBeginPageLangBPP)(lib);
		bind!(TessDllBeginPageUprightBPP)(lib);
		bind!(TessDllEndPage)(lib);
		bind!(TessDllRecognize_a_Block)(lib);
		bind!(TessDllRecognize_all_Words)(lib);
		bind!(TessDllRelease)(lib);
	}
}


因为基础太差,下一步不知道怎么做了。
各位有没有这方面的经验? 不知道还有哪个工具识别起来比较好。
hurd 2009-02-22
用它识别win下控制台字体, 错误一大半。 没实用性。。。
Global site tag (gtag.js) - Google Analytics