http://www.pudn.com/downloads92/sourcecode/delphi_control/detail361286.htmlunit Unit1;
interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, ComCtrls;
const iOcrTypeMultiLine = $0;
//要识别为多行文字 iOcrTypeOneLine = $10000;
//仅识别为一行文字 iOcrTypeOneCharacter = $20000;
//仅识别为一个字 InfoAssociate = 1;
//使用联想词库 InfoAutoRotateBeforeRecognize = 2;
//自动倾斜更正 InfoSameFont = 4;
//图片中字体大小只有一种 InfoReservePositionCannotRecognize = 8;
//不能识别的文字显示为? InfoUniteSameXInOneLine = $10;
//强制合并同一行中的X坐标相同的点阵(主要用于文字中有大量白斑或断线的情况,其它情况下一般不要设置此值) InfoRotate180 = $20;
//尝试旋转180度来识别 InfoRecognizeTable = $40;
//是否分析表格 iGrayAuto = $10000;
//灰度转为单色时自动设置阀值 HzDigit = $10;
//数字 HzEnglish = $20;
//英文 HzInterpunction = $40;
//其它符号 HzChinese = $80;
//中文 HzOnlyone = $100;
//仅单字节,不允许双字节英文、数字 HZAll = HZDigit or HZEnglish or HzInterpunction or HzChinese;
//所有字 HZEDI = HZDigit or HZEnglish or HzInterpunction;
//所有数字、英文、符号 HZEDIOnlyone = HZEDI or HzOnlyone;
//所有数字、英文、符号(单字节,不允许双字节英文、数字) HZDI = HZDigit or HzInterpunction;
//所有数字、符号 MaxSuspect = 15;
type TResult = record Chinese: array[0..3] of char;
Precision: word;
CountSuspect: word;
ChineseSuspect: array[0..MaxSuspect * 2 + 1] of char;
PrecisionSuspect: array[0..MaxSuspect] of word;
Rect: TRect;
end;
PResult = ^TResult;
TOCRResult = packed record Count: integer;
Result: array[0..1] of TResult;
end;
POCRResult = ^TOCRResult;
TForm1 = class(TForm) PageControl1: TPageControl;
TabSheet1: TTabSheet;
Image1: TImage;
Panel1: TPanel;
Button1: TButton;
TabSheet2: TTabSheet;
Memo1: TMemo;
Button2: TButton;
Buttontif2bmp: TButton;
ButtonScan: TButton;
ButtonScanAll: TButton;
Button3: TButton;
Button4: TButton;
OpenDialog1: TOpenDialog;
Button5: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Buttontif2bmpClick(Sender: TObject);
procedure ButtonScanClick(Sender: TObject);
procedure ButtonScanAllClick(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
private { Private declarations } public { Public declarations } end;
function WOcrInit(iType, iInfo, iCharacterSet: integer;
res1: integer): BOOL;
stdcall;
external 'OcrWencui.dll';
function WOcrStart(HBmp: HBitmap;
rRect: PRect;
iGray, res: integer;
var pOCRResult: POCRResult;
var TextResult: pchar;
OutputFile: Pchar): BOOL;
stdcall;
external 'OcrWencui.dll';
function WOcrStartFile(Filename: pchar;
iPage: integer;
rRect: PRect;
iGray, res: integer;
var pOCRResult: POCRResult;
var TextResult: pchar;
OutputFile: Pchar): BOOL;
stdcall;
external 'OcrWencui.dll';
procedure WOcrStop;
stdcall;
external 'OcrWencui.dll';
function WConvertToBmp(FileSrc, BmpFileDst: pchar;
iPage: integer): bool;
stdcall;
external 'OcrWencui.dll';
function WConvertColors(FileSrc, BmpFileDst: pchar;
iPage, Colors, Value: integer): bool;
stdcall;
external 'OcrWencui.dll';
function WConvertKeepColor(FileSrc, BmpFileDst: pchar;
iPage: integer;
cColor: TColor;
RedValue, GreenValue, BlueValue: byte): boolean;
stdcall;
external 'OcrWencui.dll';
function WScanerSelect(Scanname: pchar): bool;
stdcall;
external 'OcrWencui.dll';
function WScanParameter(Scanname: pchar;
bInvert, bDouble: bool;
ImageType, ImageRes, ImageBright, ImageContrast: integer;
rect: PRect): bool;
stdcall;
external 'OcrWencui.dll';
//Rect的单位:毫米 function WScanStartOnePage(filename: pchar): integer;
stdcall;
external 'OcrWencui.dll';
function WScanStartMultiPage(bShowGUI: boolean;
iFileType: integer;
Filename: pchar;
bOverwrite: bool;
iMaxScanPageCount, iMultiFirstPage, iStep: integer): integer;
stdcall;
external 'OcrWencui.dll';
var Form1: TForm1;
implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject);
var p: POCRResult;
i: integer;
s: string;
Ptext: pchar;
begin
// if WOcrStartFile('test.bmp',0, nil, iGrayAuto, 0, p, Ptext, // pchar(extractfilepath(paramstr(0)) + '1.pdf')) then
if WOcrStart(image1.Picture.Bitmap.Handle, nil, iGrayAuto, 0, p, Ptext, pchar(extractfilepath(paramstr(0)) + '1.rtf')) then
begin
//显示每个汉字 memo1.Lines.Clear;
for i := 0 to p^.Count - 1 do
with p.Result do
begin
setlength(s, CountSuspect * 2);
move(ChineseSuspect[0], s[1], CountSuspect * 2);
memo1.Lines.Add(format('%-4s 相似度:%3d%% Left:%-4d Right:%-4d Top:%-4d Bottom:%-4d 候选字:%s', [string(Chinese), Precision, Rect.left, Rect.right, Rect.top, Rect.bottom, s]));
end;
//显示全部结果 memo1.Lines.Add(strpas(ptext));
if PageControl1.ActivePageIndex <> 1 then
PageControl1.ActivePageIndex := 1;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
image1.Picture.LoadFromFile(extractfilepath(paramstr(0)) + 'test.bmp');
if not WOcrInit(iOcrTypeMultiLine or HZAll, //多行文字,所有字符 InfoAssociate or InfoRecognizeTable, //使用联想词库,分析表格 0, //字符集 0 //保留未用 ) then
showmessage('初始化dll出错,请检查使用许可是否过期或与作者联系.');
end;
procedure TForm1.Button2Click(Sender: TObject);
var p: POCRResult;
i: integer;
s: string;
r: trect;
Ptext: pchar;
begin
r.left := 156;
r.Right := 507;
r.Top := 40;
r.Bottom := 100;
if WOcrStart(image1.Picture.Bitmap.Handle, @r, iGrayAuto, 0, p, Ptext, '') then
begin
memo1.Lines.Clear;
for i := 0 to p^.Count - 1 do
with p.Result do
begin
setlength(s, CountSuspect * 2);
move(ChineseSuspect[0], s[1], CountSuspect * 2);
memo1.Lines.Add(format('%-4s 相似度:%3d%% Left:%-4d Right:%-4d Top:%-4d Bottom:%-4d 候选字:%s', [string(Chinese), Precision, Rect.left, Rect.right, Rect.top, Rect.bottom, s]));
end;
memo1.Lines.Add(strpas(ptext));
if PageControl1.ActivePageIndex <> 1 then
PageControl1.ActivePageIndex := 1;
end;
end;
procedure TForm1.Buttontif2bmpClick(Sender: TObject);
var path: string;
begin
path := extractfilepath(paramstr(0));
if WConvertToBmp(pchar(path + 'test.tif'), pchar(path + 'bak.bmp'), 0) then
showmessage('test.tif已转为bak.bmp');
end;
procedure TForm1.ButtonScanClick(Sender: TObject);
var scaner: array[0..1024] of char;
filename: string;
begin
filename := extractfilepath(paramstr(0)) + 'bak.bmp';
if WScanerSelect(@Scaner[0]) then
if WScanParameter(@scaner, false, false, 0, 200, -127, 0, nil) then
if WScanStartOnePage(pchar(filename)) <> 0 then
begin
showmessage('扫描完成,保存为' + filename);
image1.Picture.LoadFromFile(filename);
end;
end;
procedure TForm1.ButtonScanAllClick(Sender: TObject);
var scaner: array[0..1024] of char;
filename: string;
iPage: integer;
begin
filename := extractfilepath(paramstr(0)) + 'bak';
if WScanerSelect(@Scaner[0]) then
if WScanParameter(@scaner, false, false, 0, 60, 0, 0, nil) then
begin
iPage := WScanStartMultiPage(false, 0, pchar(filename), true, maxint, 1, 2);
if iPage <> 0 then
begin
showmessagefmt('扫描完成%d页', [iPage]);
image1.Picture.LoadFromFile(filename + '001.bmp');
end;
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
if WConvertColors('GLITE.BMP', 'bak.bmp', 0, 1, 128) then
showmessage('ok');
end;
procedure TForm1.Button4Click(Sender: TObject);
var color: TColor;
begin
color := $0404FC;
//04蓝 04绿 红FC if WConvertKeepColor('GLITE.BMP', 'bak.bmp', 0, color, 20, 20, 20) then
showmessage('ok');
end;
procedure TForm1.Button5Click(Sender: TObject);
begin
if OpenDialog1.Execute then
Image1.Picture.LoadFromFile(OpenDialog1.FileName);
end;
end.