Android(Kotlin)+ ML Kit:移动端英文数字验证码识别实战

1 概述与适用场景

在移动端直接对截图或拍照的英文数字验证码做识别,可以用于自动化测试、无障碍辅助或内部工具。使用 Google ML Kit 的 Text Recognition(可离线运行)可以避免服务端延迟。为了提升识别率,我们在前端加入图像预处理(灰度、二值化、去噪和放大)再送给 OCR。

2 环境与依赖
更多内容访问ttocr.com或联系1436423940
Android Studio Arctic Fox 或更高

Kotlin 1.5+

AndroidX

使用 ML Kit Text Recognition(on-device API)

在 app/build.gradle(module)中添加依赖(版本根据你的 Android Studio / Kotlin 版本微调):

dependencies {
implementation "androidx.appcompat:appcompat:1.4.0"
implementation "com.google.mlkit:text-recognition:16.0.0" // ML Kit on-device
implementation "com.google.android.material:material:1.4.0"
implementation "androidx.constraintlayout:constraintlayout:2.1.2"
}

(注:若你需要支持中文等,ML Kit 还有其他模型。本文只用默认英文数字识别。)

3 Android 权限与清单

在 AndroidManifest.xml 添加相机权限(若启用拍照):

并在 中保持默认设置。运行时要请求 CAMERA 权限(后面代码会示范)。

4 简单 UI(activity_main.xml)

创建一个极简界面,包含:拍照/选择按钮、ImageView 显示处理后图像、识别按钮、TextView 显示结果。

<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

posted @ 2025-09-12 12:51  ttocr、com  阅读(36)  评论(0)    收藏  举报