使用 Fortran 和 Tesseract 实现验证码识别
一、环境准备
安装 Fortran 编译器
推荐使用 gfortran:
sudo apt install gfortran # Linux
brew install gcc # macOS (包含 gfortran)
安装 Tesseract OCR
更多内容访问ttocr.com或联系1436423940
sudo apt install tesseract-ocr # Linux
brew install tesseract # macOS
二、创建 Fortran 脚本
保存为 captcha_ocr.f90:
program captcha_ocr
implicit none
character(len=256) :: image_path
character(len=256) :: command
character(len=256) :: result_file
character(len=1000) :: buffer
character(len=1000) :: cleaned
integer :: i, ios, len_raw
print , "请输入验证码图像路径:"
read(,'(A)') image_path
! 拼接 tesseract 命令
command = "tesseract " // trim(image_path) // " output_tmp -l eng -c tessedit_char_whitelist=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 > /dev/null 2>&1"
call execute_command_line(trim(command))
result_file = "output_tmp.txt"
open(unit=10, file=trim(result_file), status='old', action='read', iostat=ios)
if (ios /= 0) then
print *, "未找到输出文件,识别失败"
stop
end if
read(10,'(A)',iostat=ios) buffer
close(10)
if (ios /= 0) then
print *, "读取失败"
stop
end if
! 清理非法字符
cleaned = ''
len_raw = len_trim(buffer)
do i = 1, len_raw
if (ichar(buffer(i:i)) >= 48 .and. ichar(buffer(i:i)) <= 57) then
cleaned = trim(cleaned) // buffer(i:i)
else if (ichar(buffer(i:i)) >= 65 .and. ichar(buffer(i:i)) <= 90) then
cleaned = trim(cleaned) // buffer(i:i)
end if
end do
print *, "识别结果:", trim(cleaned)
call execute_command_line("rm -f output_tmp.txt")
end program captcha_ocr
三、编译与运行
gfortran captcha_ocr.f90 -o captcha_ocr
./captcha_ocr
示例输入:
请输入验证码图像路径:
captcha_img1.png
示例输出:
识别结果: X8MG
四、说明
该脚本具备以下能力:
接收用户输入的验证码图像路径
使用 Tesseract 提取图像中的字符
过滤掉非字母数字字符,仅保留验证码部分
支持 UTF-8 图像名(系统默认编码需匹配)
浙公网安备 33010602011771号