赞
踩
什么是OCR?OCR(Optical Character Recognition,光学字符识别)是指电子设备(例如扫描仪或数码相机)检查纸上打印的字符,通过检测暗、亮的模式确定其形状,然后用字符识别方法将形状翻译成计算机文字的过程。简单地说,OCR是一种技术,该项技术采用光学的方式将纸质文档中的文字转换为黑白点阵图像,然后通过识别软件将图像中的文字转换成文本格式,供文字处理软件进一步编辑加工。
什么是Tesseract?Tesseract was originally developed at Hewlett-Packard Laboratories Bristol UK and at Hewlett-Packard Co, Greeley Colorado USA between 1985 and 1994, with some more changes made in 1996 to port to Windows, and some C++izing in 1998. In 2005 Tesseract was open sourced by HP. From 2006 until November 2018 it was developed by Google.Tesseract最初是在英国布里斯托尔的惠普实验室和美国科罗拉多州格里利的惠普公司于1985年至1994年间开发的,1996年做了一些更改以移植到Windows,并在1998年进行了一些c++化。2005年,Tesseract被惠普开源。从2006年到2018年11月,它由谷歌开发。简单地说,Tesseract 就是上面OCR所说的“识别软件”的具体实现。
OCR的识别对象(输入)是一张图片,而识别结果(输出)是计算机文字。在Android手机端主要存在两种图片的获取方式,一种是从相册中选择一个,另一个是直接拍照获得。因此,本文将实现最简单的OCR思路:首先从手机中获得一张图片,然后将其输入到Tesseract库,最后通过该库输出识别结果。由于只是学习该库的使用方式,所以博主忽略了其它辅助性的功能,比如拍照识别。
implementation 'com.rmtheis:tess-two:9.1.0'
- <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
建议在onCreate方法中执行下面的checkPermission方法
- // 检查应用所需的权限,如不满足则发出权限请求
- private void checkPermission() {
- if (ContextCompat.checkSelfPermission(getApplicationContext(),
- Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
- ActivityCompat.requestPermissions(MainActivity.this,
- new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 120);
- }
- if (ContextCompat.checkSelfPermission(getApplicationContext(),
- Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
- ActivityCompat.requestPermissions(MainActivity.this,
- new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 121);
- }
- }
既用于显示,又用于识别
- // 从assets中读取一张Bitmap类型的图片
- private Bitmap getBitmapFromAssets(Context context, String filename) {
- Bitmap bitmap = null;
- AssetManager assetManager = context.getAssets();
- try {
- InputStream is = assetManager.open(filename);
- bitmap = BitmapFactory.decodeStream(is);
- is.close();
- Log.i(TAG, "图片读取成功。");
- Toast.makeText(getApplicationContext(), "图片读取成功。", Toast.LENGTH_SHORT).show();
- } catch (IOException e) {
- Log.i(TAG, "图片读取失败。");
- Toast.makeText(getApplicationContext(), "图片读取失败。", Toast.LENGTH_SHORT).show();
- e.printStackTrace();
- }
- return bitmap;
- }
去https://github.com/tesseract-ocr/下载.traineddata语言包,然后放在项目assets目录下,并通过以下代码复制到Android文件系统中
- // 为Tesserect复制(从assets中复制过去)所需的数据
- private void prepareTess() {
- try{
- // 先创建必须的目录
- File dir = getExternalFilesDir(TESS_DATA);
- if(!dir.exists()){
- if (!dir.mkdir()) {
- Toast.makeText(getApplicationContext(), "目录" + dir.getPath() + "没有创建成功", Toast.LENGTH_SHORT).show();
- }
- }
- // 从assets中复制必须的数据
- String pathToDataFile = dir + "/" + DATA_FILENAME;
- if (!(new File(pathToDataFile)).exists()) {
- InputStream in = getAssets().open(DATA_FILENAME);
- OutputStream out = new FileOutputStream(pathToDataFile);
- byte[] buff = new byte[1024];
- int len;
- while ((len = in.read(buff)) > 0) {
- out.write(buff, 0, len);
- }
- in.close();
- out.close();
- }
- } catch (Exception e) {
- Log.e(TAG, e.getMessage());
- }
- }
- // OCR识别的主程序
- private void mainProgram() {
- // 从assets中获取一张Bitmap图片
- Bitmap bitmap = getBitmapFromAssets(MainActivity.this, TARGET_FILENAME);
- // 同时显示在界面
- main_iv_image.setImageBitmap(bitmap);
- if (bitmap != null) {
- // 准备工作:创建路径和Tesserect的数据
- prepareTess();
- // 初始化Tesserect
- TessBaseAPI tessBaseAPI = new TessBaseAPI();
- String dataPath = getExternalFilesDir("/").getPath() + "/";
- tessBaseAPI.init(dataPath, "eng");
- // 识别并显示结果
- String result = getOCRResult(tessBaseAPI, bitmap);
- main_tv_result.setText(result);
- }
- }
-
- // 进行OCR并返回识别结果
- private String getOCRResult(TessBaseAPI tessBaseAPI, Bitmap bitmap) {
- tessBaseAPI.setImage(bitmap);
- String result = "-";
- try{
- result = tessBaseAPI.getUTF8Text();
- }catch (Exception e){
- Log.e(TAG, e.getMessage());
- }
- tessBaseAPI.end();
- return result;
- }
个人感觉识别率不是很准,一般般。
MainActivity.java
- import androidx.appcompat.app.AppCompatActivity;
- import androidx.core.app.ActivityCompat;
- import androidx.core.content.ContextCompat;
-
- import android.Manifest;
- import android.content.Context;
- import android.content.pm.PackageManager;
- import android.content.res.AssetManager;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- 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 android.widget.Toast;
-
- import com.googlecode.tesseract.android.TessBaseAPI;
-
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
-
- public class MainActivity extends AppCompatActivity {
-
- public static final String TESS_DATA = "/tessdata";
- private static final String TARGET_FILENAME = "vin_demo.png";
- private static final String DATA_FILENAME = "eng.traineddata";
- private static final String TAG = MainActivity.class.getSimpleName();
-
- private Button main_bt_recognize;
- private TextView main_tv_result;
- private ImageView main_iv_image;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // 设置布局文件
- setContentView(R.layout.activity_main);
- // 检查并请求应用所需权限
- checkPermission();
- // 获取控件对象
- initView();
- // 设置控件的监听器
- setListener();
- }
-
- private void setListener() {
- // 设置识别按钮的监听器
- main_bt_recognize.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- // 识别之前需要再次检查一遍权限
- checkPermission();
- // 点击后的主程序
- mainProgram();
- }
- });
- }
-
- // 获得界面需要交互的控件
- private void initView() {
- main_bt_recognize = findViewById(R.id.main_bt_recognize);
- main_tv_result = findViewById(R.id.main_tv_result);
- main_iv_image = findViewById(R.id.main_iv_image);
- }
-
- // 检查应用所需的权限,如不满足则发出权限请求
- private void checkPermission() {
- if (ContextCompat.checkSelfPermission(getApplicationContext(),
- Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
- ActivityCompat.requestPermissions(MainActivity.this,
- new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 120);
- }
- if (ContextCompat.checkSelfPermission(getApplicationContext(),
- Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
- ActivityCompat.requestPermissions(MainActivity.this,
- new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 121);
- }
- }
-
- // OCR识别的主程序
- private void mainProgram() {
- // 从assets中获取一张Bitmap图片
- Bitmap bitmap = getBitmapFromAssets(MainActivity.this, TARGET_FILENAME);
- // 同时显示在界面
- main_iv_image.setImageBitmap(bitmap);
- if (bitmap != null) {
- // 准备工作:创建路径和Tesserect的数据
- prepareTess();
- // 初始化Tesserect
- TessBaseAPI tessBaseAPI = new TessBaseAPI();
- String dataPath = getExternalFilesDir("/").getPath() + "/";
- tessBaseAPI.init(dataPath, "eng");
- // 识别并显示结果
- String result = getOCRResult(tessBaseAPI, bitmap);
- main_tv_result.setText(result);
- }
- }
-
- // 从assets中读取一张Bitmap类型的图片
- private Bitmap getBitmapFromAssets(Context context, String filename) {
- Bitmap bitmap = null;
- AssetManager assetManager = context.getAssets();
- try {
- InputStream is = assetManager.open(filename);
- bitmap = BitmapFactory.decodeStream(is);
- is.close();
- Log.i(TAG, "图片读取成功。");
- Toast.makeText(getApplicationContext(), "图片读取成功。", Toast.LENGTH_SHORT).show();
- } catch (IOException e) {
- Log.i(TAG, "图片读取失败。");
- Toast.makeText(getApplicationContext(), "图片读取失败。", Toast.LENGTH_SHORT).show();
- e.printStackTrace();
- }
- return bitmap;
- }
-
- // 为Tesserect复制(从assets中复制过去)所需的数据
- private void prepareTess() {
- try{
- // 先创建必须的目录
- File dir = getExternalFilesDir(TESS_DATA);
- if(!dir.exists()){
- if (!dir.mkdir()) {
- Toast.makeText(getApplicationContext(), "目录" + dir.getPath() + "没有创建成功", Toast.LENGTH_SHORT).show();
- }
- }
- // 从assets中复制必须的数据
- String pathToDataFile = dir + "/" + DATA_FILENAME;
- if (!(new File(pathToDataFile)).exists()) {
- InputStream in = getAssets().open(DATA_FILENAME);
- OutputStream out = new FileOutputStream(pathToDataFile);
- byte[] buff = new byte[1024];
- int len;
- while ((len = in.read(buff)) > 0) {
- out.write(buff, 0, len);
- }
- in.close();
- out.close();
- }
- } catch (Exception e) {
- Log.e(TAG, e.getMessage());
- }
- }
-
- // 进行OCR并返回识别结果
- private String getOCRResult(TessBaseAPI tessBaseAPI, Bitmap bitmap) {
- tessBaseAPI.setImage(bitmap);
- String result = "-";
- try{
- result = tessBaseAPI.getUTF8Text();
- }catch (Exception e){
- Log.e(TAG, e.getMessage());
- }
- tessBaseAPI.end();
- return result;
- }
- }
layout_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"
- tools:context=".MainActivity">
-
- <ScrollView
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical" >
-
- <ImageView
- android:id="@+id/main_iv_image"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- 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="wrap_content"
- android:layout_marginLeft="5dp"
- android:layout_marginRight="5dp"
- android:layout_gravity="center_horizontal" />
- </LinearLayout>
- </ScrollView>
- </LinearLayout>
AndroidManifest.xml
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.cs.ocrdemo4csdn">
-
- <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
-
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:roundIcon="@mipmap/ic_launcher_round"
- android:supportsRtl="true"
- android:theme="@style/Theme.OCRDemo4CSDN">
-
- <activity
- android:name=".MainActivity"
- android:exported="true">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
build.gradle(Module)
- plugins {
- id 'com.android.application'
- }
-
- android {
- compileSdk 34
-
- defaultConfig {
- applicationId "com.cs.ocrdemo4csdn"
- minSdk 21
- targetSdk 34
- versionCode 1
- versionName "1.0"
-
- testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
- }
-
- buildTypes {
- release {
- minifyEnabled false
- proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
- }
- }
- compileOptions {
- sourceCompatibility JavaVersion.VERSION_1_8
- targetCompatibility JavaVersion.VERSION_1_8
- }
- }
-
- dependencies {
-
- implementation 'androidx.appcompat:appcompat:1.2.0'
- implementation 'com.google.android.material:material:1.3.0'
- implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
- testImplementation 'junit:junit:4.+'
- androidTestImplementation 'androidx.test.ext:junit:1.1.2'
- androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
-
- implementation 'com.rmtheis:tess-two:9.1.0'
- }
vin_demo.png
随便从网上下的图片(保证存在光学字符即可)。
eng.traineddata
从https://github.com/tesseract-ocr/下载的,如果不行从https://github.com/raykibul/Android-OCR-Testing/tree/main下载。
1、光学字符识别
2、GitHub - tesseract-ocr/tesseract
3、GitHub - raykibul/Android-OCR-Testing
1、跟着别人的CSDN博客捣鼓了一天多,但是没能调通,一直在报错,比如遇到“Could not initialize Tesseract API with language=eng”、“getUTF8Text导致android tesseract崩溃”等等问题。后边看了GitHub上边比较新的代码(诸位如果代码参考了我的博客还是没能调通,建议看看这份代码),然后就跑通了。目前还不知道原因是啥。
2、我看Tesseract这个库的识别结果并不是十分准确,尤其是对于拍照出来的结果识别率很低,再从这个库的发展历史来看,好像现在都没有什么人再维护它了(最后的维护者是谷歌,而且停留在2018年),所以,对于现在(今年是2023年)而言,我感觉它已经有点属于是过时技术。大家可以寻求一些比较新的技术方案,毕竟现在大模型都搞得这么牛了,OCR这种应该搞得更好才是。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。