使用 Kotlin 和 Tesseract 实现验证码识别工具

一、项目简介
本项目使用 Kotlin 编写命令行工具,通过调用本地安装的 Tesseract OCR 引擎来识别图像中的英文字母和数字字符。支持图像预处理,适合批处理和自动化脚本。

二、环境准备

  1. 安装 Kotlin 编译环境
    可使用 IntelliJ IDEA(推荐)或命令行:
    更多内容访问ttocr.com或联系1436423940
    sdk install kotlin
  2. 安装 Tesseract

Ubuntu / Debian

sudo apt install tesseract-ocr

macOS

brew install tesseract
确保命令可用:

tesseract --version
三、准备图像
将验证码图片命名为 captcha.png,确保图像清晰、背景干净,字符包含英文和数字。

四、Kotlin 识别代码(命令行版本)
创建文件 CaptchaOCR.kt:

import java.io.File

fun main() {
val inputImage = "captcha.png"
val processedImage = "processed.png"

// 使用 ImageMagick 预处理图像(灰度 + 二值)
val convertCmd = "convert $inputImage -colorspace Gray -threshold 50% $processedImage"
val convertResult = Runtime.getRuntime().exec(convertCmd).waitFor()
if (convertResult != 0) {
    println("图像预处理失败")
    return
}

// 使用 Tesseract 识别,限制字符为英文字母和数字
val tesseractCmd = listOf(
    "tesseract", processedImage, "stdout",
    "-l", "eng",
    "--psm", "7",
    "-c", "tessedit_char_whitelist=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
)
val process = ProcessBuilder(tesseractCmd).redirectErrorStream(true).start()
val output = process.inputStream.bufferedReader().readText()
val cleaned = output.replace(Regex("[^A-Za-z0-9]"), "")

println("识别结果为: $cleaned")

}
五、编译与运行
确保你安装了 Kotlin 命令行工具,或使用 IntelliJ 打开该项目。

命令行编译运行:

kotlinc CaptchaOCR.kt -include-runtime -d CaptchaOCR.jar
java -jar CaptchaOCR.jar
输出示例:

识别结果为: F8X9T
六、功能拓展建议
批量识别验证码目录

添加命令行参数支持指定图像路径

封装为桌面应用(JavaFX 或 Compose Desktop)

构建 Kotlin Web 后端(Ktor)提供识别 API

posted @ 2025-07-07 21:50  ttocr、com  阅读(14)  评论(0)    收藏  举报