OCR Tesseract tess-two文字识别Android Studio实现

1、简介

OCR (Optical Character Recognition,光学字符识别)是指电子设备(例如扫描仪或数码相机)检查纸上打印的字符,通过检测暗、亮的模式确定其形状,然后用字符识别方法将形状翻译成计算机文字的过程
Tesseract是Ray Smith于1985到1995年间在惠普布里斯托实验室开发的一个OCR引擎,曾经在1995 UNLV精确度测试中名列前茅。但1996年后基本停止了开发。2006年,Google邀请Smith加盟,重启该项目。目前项目的许可证是Apache 2.0。该项目目前支持Windows、Linux和Mac OS等主流平台。但作为一个引擎,它只提供命令行工具。
因为Tesseract使用C++实现的,在Android中不能直接使用,需要封装JavaAPI才能在Android平台中进行调用,这里我们直接使用TessTwo项目,tess-two是TesseraToolsForAndroid的一个git分支,使用简单,切集成了leptonica,在使用之前需要先从git上下载源码进行编译。

2、下载

在Android平台上使用Tesseract OCR首先要下载Tess-two工程,它是专门针对Android平台编译出来的,下载地址如下:https://github.com/rmtheis/tess-two
文字识别还需要下载相应到tessdata语言包,本项目以英文包为例,下载地址如下:
https://github.com/tesseract-ocr/tessdata
选中需要用到到语言包,英文包为eng.traineddata,简体中文包为chi_sim.traineddata。

3、导入配置

1、解压下载下来的tess-two-master文件,解压后目录如下图所示:

2、在android studio新建一个空项目,将上图中的tess-two(第三个)文件夹作为module导入到项目中(File->New->Import Module)。
3、项目必须是支持NDK的,所以要在Project Structure中指明NDK的路径。如果没NDK就需要先下载,一般在android studio下载都自动帮你配好。原因是tess-two是个NDK项目,没有NDK支持无法完成编译

4、你还需要安装两个工具,CMake和LLDB。

5、你可能还会遇到没有android-maven的错误,在tess-two的build.gradle文件到最前面添加以下脚本:

buildscript {repositories {jcenter()}dependencies {classpath 'com.android.tools.build:gradle:2.1.2'classpath 'org.codehaus.groovy:groovy-backports-compat23:2.3.5'classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.0'classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'}
}


6、在tess-two的build.gradle文件里还有3处需要修改,如下图,修改为自己到版本号即可:

如我的修改后为:


7、你有可能会报如下错误:

Error:(81) Android NDK: Application targets deprecated ABI(s): mips64 armeabi mips
Error:(82) Android NDK: Support for these ABIs will be removed in a future NDK release.

现在只需要在tess-two->jni下的Application.mk文件里删掉APP_ABI里到mips即可

如果还报类似到错误,如:

Error:(81) Android NDK: Application targets deprecated ABI(s): armeabi
Error:(82) Android NDK: Support for these ABIs will be removed in a future NDK release.

就把armeabi删掉。
我的项目只留下两个:

8、将下载到语言包拷贝进手机存储里,可在任意位置创建一个tesseract文件夹,在tesseract文件夹下创建一个tessdata文件夹,然后将语言包如eng.traineddata拷进tessdata文件夹里,如果不创建tessdata文件夹会报如下错误:

Data path must contain subfolder tessdata!

9、记得在项目到AndroidManifest.xml里配置读写权限,否则会报以下错误

E/Tesseract(native): Could not initialize Tesseract API with language=eng!

权限如下:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

10、记得给tess-two module配置依赖项:

4、代码

MainActivity

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;import com.googlecode.tesseract.android.TessBaseAPI;import java.io.File;public class MainActivity extends AppCompatActivity {TessBaseAPI mTess;Button button;Bitmap bitmap;String result;ImageView imgView;TextView txtView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button=findViewById(R.id.btnShot);imgView = (ImageView)this.findViewById(R.id.imageView);txtView = (TextView)this.findViewById(R.id.idCard_textView);bitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.textimage);//识别图片源imgView.setImageBitmap(bitmap);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {mTess.setImage(bitmap);result = mTess.getUTF8Text();txtView.setText("结果为:" + result);}});initTessBaseData();}private void initTessBaseData() {mTess = new TessBaseAPI();String datapath = "/storage/emulated/0/JW7129/tesseract/"; //语言包目录String language = "eng";File dir = new File(datapath + "tessdata/");if (!dir.exists()){Log.e("tag","文件不存在");}mTess.init(datapath, language);}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"tools:context="com.zhd.test8.test10.MainActivity"><Button
        android:id="@+id/btnShot"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginEnd="8dp"android:layout_marginStart="8dp"android:layout_marginTop="16dp"android:text="识别"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /><TextView
        android:id="@+id/idCard_textView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Hello World!"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent"app:layout_constraintVertical_bias="0.168" /><ImageView
        android:id="@+id/imageView"android:layout_width="match_parent"android:layout_height="200dp"android:layout_marginTop="140dp"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

至此,项目已能成功运行,由于ndk原因,项目build需要较长时间,本人做了简单的测试,截屏的文字基本能识别。

Android文字识别tess-two OCR相关推荐

  1. 【PC工具】更新在线图片文字识别工具,OCR免费文字识别工具

    微信关注 "DLGG创客DIY" 设为"星标",重磅干货,第一时间送达. 之前分享过两个windows上的OCR文字识别工具: [PC工具]更新!windows ...

  2. python调用qq识别图片文字_Python3使用腾讯云文字识别(腾讯OCR)提取图片中的文字内容实例详解...

    百度OCR体验地址: 腾讯OCR体验地址: 测试结果是:腾讯的效果要比百度的好 腾讯云目前额度是: 每个接口 1,000次/月免费,有6个文字识别的接口,一共是6,000次/月 百度接口调用之前写过文 ...

  3. android自动识别文字,Android文字识别tesseract ocr -训练样本库 识别字库

    目录 安装tesseract ocr引擎和jTessBoxEditor 安装jTessBoxEditor 开始制作box 准备好训练的图片 将图片转为tif格式的样本图片 合并样本图片 修改box文件 ...

  4. Android图片文字识别(阿里OCR接口)

    最近使用了阿里云的OCR文字识别API 先来看看效果 我使用的是通用类文字识别,具体实现过程如下: 1.购买阿里云的通用类文字识别 目前是0元免费的,可以使用500次.购买成功后到->控制台-& ...

  5. 【华为云技术分享】文字识别服务(OCR)基于对抗样本的模型可信安全威胁分析初析

    [摘要] 文字识别作为计算机视觉的重要分支之一,面临着视觉方向同样的安全威胁分析.随着人工智能的普及和文字识别服务大规模的商业落地,解决好相关的模型安全威胁问题已经刻不容缓. OCR处理流程融合了多种 ...

  6. 按键精灵使用百度文字识别(百度ocr)教程

    按键精灵通过实践,也可以接入百度的OCR,百度OCR免费额度为,每日50000次普通调用,对于日常应用已经足够,返回识别内容时间在1s以内(免费版有一秒内请求次数限制,不充钱也无法达到更短的时间).百 ...

  7. 按键精灵 百度文字识别(百度ocr)OCRSpace文字识别

    目录 1. 申请百度OCR服务 1.1. 百度OCR登录 1.2 创建新应用 1.3 免费领取次数 1.3 查看是否创建成功 2. 按键精灵运用百度OCR接口 2.1 通用文字识别(高精度版)文档 2 ...

  8. 【PC工具】更新!windows图片文字识别软件,OCR免费文字识别工具

    今天再分享一个图片文字识别软件,上次的还能用,看哪个好用用哪个,这次分享的居然还有翻译功能,还会朗读... PandaOCR,软件可以从GitHub下载(注意这是个神奇的网站大家一定要记住!):htt ...

  9. python图像识别系统_Python图像处理之图片文字识别功能(OCR)

    OCR与Tesseract介绍 将图片翻译成文字一般被称为光学文字识别(Optical Character Recognition,OCR).可以实现OCR 的底层库并不多,目前很多库都是使用共同的几 ...

最新文章

  1. OpenCV中泛洪填充算法解析与应用
  2. 没事抽空学——常用界面组件属性
  3. 我国农村经济发展战略
  4. Java在线问题诊断工具Greys
  5. 一次性供应商不能用特别总账标准程序修改
  6. 四川航空签约神策数据,航司沉淀数据价值
  7. iOS开发网络资源整理-持续更新
  8. PHP 通过设置P3P头来实现跨域访问COOKIE
  9. python基础小白题5
  10. 进程、线程相关知识点整理
  11. 刘强东:京东必定会击败阿里巴巴
  12. TurboMail独家提供邮件服务器与Outlook间的地址簿同步插件
  13. 机器学习:样本去中心化目的
  14. 用 man 命令查看 ls 命令的使用手册_Python学习第167课--用man和info打开Linux命令说明书的区别...
  15. 解决办法:configure: error: C compiler cannot create executables错误
  16. dota2国服服务器延迟高,为何dota2国服经常崩溃 竟然是良心不分区的原因
  17. 木马专杀软件测试自学,5款免费杀软“紫狐”木马查杀测试
  18. Google, with new Pixel and camera, is serious about devices
  19. Docker部署Django+Mysql+uWSGI+Nginx Web应用 - 笔记更新2022-01-04
  20. js屏蔽键盘esc键

热门文章

  1. Caused by: javax.management.InstanceAlreadyExistsException: MXBean already registered with name org.
  2. Nexus3 部署备份与恢复
  3. C# WebForm 屏蔽输入框的验证
  4. 【广工考试笔记】计算机网络考试速成笔记
  5. 台式计算机包装清单,台式电脑主机箱的运输包装设计.docx
  6. react native unable to load script from sets “index.android.bundle“.make sure your bundle is packag
  7. 奶茶MM不值得羡慕:明明是正妻,却像个宠妃
  8. if/countif/sumif/averageif
  9. react-notifications-component,一个强大的React Notifications库
  10. uniapp ali-oss 体积过大无法使用