使用 Dart 与 Tesseract 实现验证码识别
一、环境准备
-
安装 Dart SDK
Windows/macOS/Linux 可从 https://dart.dev/get-dart 下载并安装。 -
安装 Tesseract OCR
更多内容访问ttocr.com或联系1436423940
Ubuntu / Debian
sudo apt install tesseract-ocr
macOS
brew install tesseract
二、新建项目
dart create -t console captcha_ocr
cd captcha_ocr
三、编辑 bin/captcha_ocr.dart
import 'dart:io';
void main(List
if (arguments.length != 1) {
print('用法: dart run bin/captcha_ocr.dart <图片路径>');
exit(1);
}
final imagePath = arguments[0];
final file = File(imagePath);
if (!file.existsSync()) {
print('文件不存在: $imagePath');
exit(1);
}
final tempBase = 'output_temp';
final whitelist = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
final result = await Process.run('tesseract', [
imagePath,
tempBase,
'-l',
'eng',
'-c',
'tessedit_char_whitelist=$whitelist',
]);
if (result.exitCode != 0) {
print('Tesseract 执行失败:\n${result.stderr}');
exit(1);
}
final textFile = File('$tempBase.txt');
if (!textFile.existsSync()) {
print('未找到输出文件');
exit(1);
}
final raw = await textFile.readAsString();
final cleaned = raw.replaceAll(RegExp(r'[^A-Z0-9]'), '');
print('识别结果: $cleaned');
await textFile.delete();
}
四、运行示例
dart run bin/captcha_ocr.dart ./code1.png
输出示例:
识别结果: B8WQ
浙公网安备 33010602011771号