使用 Kotlin 和 Tesseract 实现英文数字验证码识别
一、项目概述
本项目目标是通过 Kotlin 构建一个可以读取图像、调用 Tesseract 引擎并输出识别结果的 CLI 工具。我们会进行简单图像预处理(灰度化+二值化),提高验证码识别的准确性。
更多内容访问ttocr.com或联系1436423940
二、准备工作
- 安装 Kotlin
可以使用官方命令行工具或通过 IntelliJ IDEA 快速启动 Kotlin 项目。
- 安装 Tesseract OCR
macOS:brew install tesseract
Ubuntu:sudo apt install tesseract-ocr
Windows:安装 https://github.com/tesseract-ocr/tesseract
- 安装 ImageMagick(图像预处理)
sudo apt install imagemagick
or
brew install imagemagick
三、新建 Kotlin 项目(Gradle)
创建 captcha-ocr 项目并设置如下 build.gradle.kts:
plugins {
kotlin("jvm") version "1.9.0"
application
}
repositories {
mavenCentral()
}
application {
mainClass.set("CaptchaKt")
}
四、识别验证码代码
在 src/main/kotlin/Captcha.kt 中添加如下内容:
import java.io.File
fun main() {
val input = "captcha.png"
val processed = "gray_captcha.png"
// Step 1: 使用 ImageMagick 灰度化并二值化图像
val convertCommand = listOf("convert", input, "-colorspace", "Gray", "-threshold", "50%", processed)
val convertResult = ProcessBuilder(convertCommand)
.inheritIO()
.start()
.waitFor()
if (convertResult != 0) {
println("图像处理失败")
return
}
// Step 2: 使用 Tesseract 识别图像
val tesseractCommand = listOf(
"tesseract",
processed,
"stdout",
"-l", "eng",
"--psm", "7",
"-c", "tessedit_char_whitelist=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
)
val process = ProcessBuilder(tesseractCommand)
.redirectErrorStream(true)
.start()
val output = process.inputStream.bufferedReader().readText()
val clean = output.replace(Regex("[^A-Za-z0-9]"), "")
println("识别的验证码为:$clean")
}
五、准备图像文件
将你要识别的验证码图像命名为 captcha.png,放到项目根目录。
六、运行程序
./gradlew run
输出示例:
识别的验证码为:X7J2D
浙公网安备 33010602011771号