下面是整个详解步骤过程
效果图
- 流程:获取assets中的图片显示到页面,提取照片内的文字
一、OCR的含义
ocr是Optical Character Recognition(光学字符识别)是指电子设备(例如扫描仪或数码相机)检查纸上打印的字符,通过检测暗、亮的模式确定其形状,然后用字符识别方法将形状翻译成计算机文字的过程
二、什么是Tesseract
简单地说,Tesseract 就是OCR所说的“识别软件”的具体实现
- 其实看官网已经是5、6年前就开始不维护了
- 这里也指明了,不在维护,可前往 Tesseract Tools 的一个分支Tesseract4Android官网,这里还是写一下Tesseract 的demo吧,做参考
当然你也可以直接去Tesseract4Android的参考文章
Tesseract4Android参考文章
三、前提准备
1、添加依赖
注意:
1、Android 2.3 或更高版本
2、数据文件必须是 复制到 Android 设备的子目录中tessdata(上一级文件夹的名称必须是tessdata,后缀必须是.traineddata)
dependencies {
implementation 'com.rmtheis:tess-two:9.1.0'
}
2、数据文件下载路径
- 数据包下载下来放到assets文件夹下
四、实际代码案例Demo如下:
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:id="@+id/main_iv_image"
android:layout_width="match_parent"
android:layout_height="500dp"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"/>
<Button
android:id="@+id/main_bt_recognize"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_gravity="center_horizontal"
android:text="读取一张图片并识别" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_gravity="center_horizontal"
android:text="识别结果:" />
<TextView
android:id="@+id/main_tv_result"
android:layout_width="match_parent"
android:layout_height=