使用 VBScript 与 Tesseract 实现验证码识别
一、准备工作
安装 Tesseract OCR
下载地址:https://github.com/tesseract-ocr/tesseract
安装后确保 tesseract.exe 所在目录加入了系统的 PATH 环境变量
更多内容访问ttocr.com或联系1436423940
准备验证码图像,例如:D:\captcha\test1.png
二、VBScript 脚本代码(保存为 captcha_ocr.vbs)
Dim imagePath, outputBase, cmd, fso, resultFile, resultText, cleanedText
' 图像路径
imagePath = "D:\captcha\test1.png"
outputBase = "D:\captcha\output"
' 组装命令行
cmd = "cmd /c tesseract """ & imagePath & """ """ & outputBase & """ -l eng -c tessedit_char_whitelist=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 >nul 2>&1"
CreateObject("WScript.Shell").Run cmd, 0, True
' 读取结果文件
Set fso = CreateObject("Scripting.FileSystemObject")
resultFile = outputBase & ".txt"
If fso.FileExists(resultFile) Then
resultText = fso.OpenTextFile(resultFile, 1).ReadAll
resultText = Replace(resultText, vbCrLf, "")
' 仅保留大写字母和数字
cleanedText = ""
For i = 1 To Len(resultText)
ch = Mid(resultText, i, 1)
If ch Like "[A-Z0-9]" Then
cleanedText = cleanedText & ch
End If
Next
MsgBox "识别结果: " & cleanedText
fso.DeleteFile resultFile
Else
MsgBox "识别失败:未生成输出文件"
End If
三、运行方法
将脚本保存为 captcha_ocr.vbs
双击运行,或使用命令行:
cscript //nologo captcha_ocr.vbs
结果会通过弹窗提示,例如:
识别结果: 7KZ3
四、可选:处理多个验证码图像
可以扩展如下功能批量处理文件夹下所有 .png 文件:
Set folder = fso.GetFolder("D:\captcha")
For Each file In folder.Files
If LCase(fso.GetExtensionName(file.Name)) = "png" Then
imagePath = file.Path
outputBase = fso.GetParentFolderName(file) & "\out_" & fso.GetBaseName(file)
' 调用识别函数...
End If
Next
浙公网安备 33010602011771号