- 环境准备
1.1 安装 Rust
Rust 可通过官方的 rustup 进行安装:
bash
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
安装完成后,检查 Rust 是否可用:
bash
rustc --version
1.2 安装 Tesseract OCR
Linux(Ubuntu)
bash
sudo apt update
sudo apt install tesseract-ocr libtesseract-dev
macOS(Homebrew)
bash
brew install tesseract
Windows
从 Tesseract GitHub 下载并安装。
检查 Tesseract 是否安装成功:
bash
tesseract --version
2. 创建 Rust 项目
使用 Cargo 创建新项目:
bash
cargo new captcha_reader
cd captcha_reader
在 Cargo.toml 中添加 Tesseract 相关依赖:
toml
[dependencies]
tesseract = "0.14"
image = "0.24" # 用于处理验证码图片
3. 代码实现
修改 src/main.rs,写入以下代码:
rust
use std::process::Command;
use image::{DynamicImage, GenericImageView, GrayImage, Luma};
use tesseract::Tesseract;
fn preprocess_image(input_path: &str, output_path: &str) {
let img = image::open(input_path).expect("无法打开图片");
let gray_img = img.to_luma8(); // 转换为灰度图像
// 二值化处理
let binary_img = GrayImage::from_fn(gray_img.width(), gray_img.height(), |x, y| {if gray_img.get_pixel(x, y)[0] > 128 {Luma([255]) // 白色} else {Luma([0]) // 黑色}
});binary_img.save(output_path).expect("无法保存处理后的图片");
}
fn main() {
let input_image = "captcha.png"; // 替换为你的验证码图片
let processed_image = "processed_captcha.png";
// 预处理图片
preprocess_image(input_image, processed_image);// 使用 Tesseract OCR 解析验证码
let text = Tesseract::new(None, "eng").expect("无法初始化 Tesseract").set_image(processed_image).recognize().expect("OCR 失败");println!("识别出的验证码: {}", text.trim());
}
4. 代码解析
4.1 图像预处理
rust
fn preprocess_image(input_path: &str, output_path: &str)
读取验证码图像,转换为灰度图像,并进行二值化处理,以提高 OCR 识别率。
4.2 OCR 识别
rust
let text = Tesseract::new(None, "eng")
.expect("无法初始化 Tesseract")
.set_image(processed_image)
.recognize()
.expect("OCR 失败");
调用 Tesseract 进行验证码解析。
4.3 输出识别结果
rust
println!("识别出的验证码: {}", text.trim());
去除空格,输出 OCR 识别结果。
5. 运行程序
将 captcha.png 图片放入项目目录,然后运行:
bash
cargo run
示例输出:
makefile
识别出的验证码: X7G9H
6. 提高 OCR 识别率
6.1 选择不同的 Tesseract PSM 模式
rust
let text = Tesseract::new(None, "eng")
.expect("无法初始化 Tesseract")
.set_variable("tessedit_pageseg_mode", "6") // 单行文本模式
.set_image(processed_image)
.recognize()
.expect("OCR 失败");
6.2 限制识别字符集
rust
let text = Tesseract::new(None, "eng")
.expect("无法初始化 Tesseract")
.set_variable("tessedit_char_whitelist", "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")
.set_image(processed_image)
.recognize()
.expect("OCR 失败");