使用 Pascal 和 Tesseract 实现验证码识别

一、准备环境

  1. 安装 Free Pascal 编译器
    Windows / macOS 可从官网下载安装:
    https://www.freepascal.org/download.html
    更多内容访问ttocr.com或联系1436423940
    Ubuntu / Debian:

sudo apt install fp-compiler
2. 安装 Tesseract OCR

Ubuntu / Debian

sudo apt install tesseract-ocr

macOS

brew install tesseract
二、Pascal 程序编写
创建文件 captcha_ocr.pas:

program CaptchaOCR;

uses
SysUtils, Process;

var
ImagePath, Cmd, OutputFile, Line, ResultText: string;
F: Text;
Ch: Char;
begin
if ParamCount < 1 then
begin
WriteLn('用法: captcha_ocr <图片路径>');
Halt(1);
end;

ImagePath := ParamStr(1);
OutputFile := 'ocr_result';

// 构建命令行调用 Tesseract
Cmd := 'tesseract ' + ImagePath + ' ' + OutputFile +
' -l eng -c tessedit_char_whitelist=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
if RunCommand('/bin/sh', ['-c', Cmd], Line) then
WriteLn('识别中...')
else
begin
WriteLn('执行失败');
Halt(1);
end;

// 读取输出文件
AssignFile(F, OutputFile + '.txt');
Reset(F);
ResultText := '';
while not EOF(F) do
begin
ReadLn(F, Line);
for Ch in Line do
if (Ch in ['A'..'Z']) or (Ch in ['0'..'9']) then
ResultText := ResultText + Ch;
end;
CloseFile(F);

WriteLn('识别结果: ', ResultText);
end.
三、编译并运行
编译:

fpc captcha_ocr.pas
运行:

./captcha_ocr code1.png
示例输出:

识别结果: 7HGF

posted @ 2025-06-25 16:55  ttocr、com  阅读(20)  评论(0)    收藏  举报