Project Icon

Tesseract4Android

基于 Tesseract 的 Android OCR 库 支持多线程识别

Tesseract4Android 是一个重写的 Android OCR 库,基于 tess-two 项目。该库采用 CMake 构建,兼容最新 Android Studio,集成 Tesseract OCR 5.3.4。它提供标准单线程和 OpenMP 多线程两个版本,满足不同性能需求。Tesseract4Android 简化了 OCR 技术在 Android 应用中的使用,支持多语言识别,并附带示例应用展示基本用法。

Tesseract4Android

Fork of tess-two rewritten from scratch to build with CMake and support latest Android Studio and Tesseract OCR.

The Java/JNI wrapper files and tests for Leptonica / Tesseract are based on the tess-two project, which is based on Tesseract Tools for Android.

Dependencies

This project uses additional libraries (with their own specific licenses):

Prerequisites

  • Android 4.1 (API 16) or higher
  • A v4.0.0 trained data file(s) for language(s) you want to use.
    • These files must be placed in the (sub)directory named tessdata and the path must be readable by the app. When targeting API >=29, only suitable places for this are app's private directories (like context.getFilesDir() or context.getExternalFilesDir()).

Variants

This library is available in two variants.

  • Standard - Single-threaded. Best for single-core processors or when using multiple Tesseract instances in parallel.
  • OpenMP - Multi-threaded. Provides better performance on multi-core processors when using only single instance of Tesseract.

Usage

You can get compiled version of Tesseract4Android from JitPack.io.

  1. Add the JitPack repository to your project root build.gradle file at the end of repositories:
allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}
  1. Add the dependency to your app module build.gradle file:
dependencies {
    // To use Standard variant:
    implementation 'cz.adaptech.tesseract4android:tesseract4android:4.7.0'

    // To use OpenMP variant:
    implementation 'cz.adaptech.tesseract4android:tesseract4android-openmp:4.7.0'
}
  1. Use the TessBaseAPI class in your code:

This is the simplest example you can have. In this case TessBaseAPI is always created, used to recognize the image and then destroyed. Better would be to create and initialize the instance only once and use it to recognize multiple images instead. Look at the sample project for such usage, additionally with progress notifications and a way to stop the ongoing processing.

// Create TessBaseAPI instance (this internally creates the native Tesseract instance)
TessBaseAPI tess = new TessBaseAPI();

// Given path must contain subdirectory `tessdata` where are `*.traineddata` language files
// The path must be directly readable by the app
String dataPath = new File(context.getFilesDir(), "tesseract").getAbsolutePath();

// Initialize API for specified language
// (can be called multiple times during Tesseract lifetime)
if (!tess.init(dataPath, "eng")) { // could be multiple languages, like "eng+deu+fra"
    // Error initializing Tesseract (wrong/inaccessible data path or not existing language file(s))
    // Release the native Tesseract instance
    tess.recycle();
    return;
}

// Load the image (file path, Bitmap, Pix...)
// (can be called multiple times during Tesseract lifetime)
tess.setImage(image);

// Start the recognition (if not done for this image yet) and retrieve the result
// (can be called multiple times during Tesseract lifetime)
String text = tess.getUTF8Text();

// Release the native Tesseract instance when you don't want to use it anymore
// After this call, no method can be called on this TessBaseAPI instance
tess.recycle();

Sample app

There is example application in the sample directory. It shows basic usage of the TessBaseAPI inside ViewModel, showing progress indication, allowing stopping the processing and more.

It uses sample image and english traineddata, which are extracted from the assets in the APK to app's private directory on device. This is simple, but you are keeping 2 instances of the data file (first is kept in the APK file itself, second is kept on the storage) - wasting some space. If you plan to use multiple traineddata files, it would be better to download them directly from the internet rather than distributing them within the APK.

Building

You can use Android Studio to open the project and build the AAR. Or you can use gradlew from command line.

To build the release version of the library, use task tesseract4android:assembleRelease. After successful build, you will have resulting AAR files in the <project dir>/tesseract4Android/build/outputs/aar/ directory.

Or you can publish the AAR directly to your local maven repository, by using task tesseract4android:publishToMavenLocal. After successful build, you can consume your library as any other maven dependency. Just make sure to add mavenLocal() repository in repositories {} block in your project's build.gradle file.

Android Studio

  • Open this project in Android Studio.
  • Open Gradle panel, expand Tesseract4Android / :tesseract4Android / Tasks / other and run assembleRelease (to get AAR).
  • Or in the same panel expand Tesseract4Android / :tesseract4Android / Tasks / publishing and run publishToMavenLocal (to publish AAR).

GradleW

  • In project directory create local.properties file containing:
sdk.dir=c\:\\your\\path\\to\\android\\sdk
ndk.dir=c\:\\your\\path\\to\\android\\ndk

Note for paths on Windows you must use \ to escape some special characters, as in example above.

  • Call gradlew tesseract4android:assembleRelease from command line (to get AAR).
  • Or call gradlew tesseract4android:publishToMavenLocal from command line (to publish AAR).

License

Copyright 2019 Adaptech s.r.o., Robert Pösel

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
项目侧边栏1项目侧边栏2
推荐项目
Project Cover

豆包MarsCode

豆包 MarsCode 是一款革命性的编程助手,通过AI技术提供代码补全、单测生成、代码解释和智能问答等功能,支持100+编程语言,与主流编辑器无缝集成,显著提升开发效率和代码质量。

Project Cover

AI写歌

Suno AI是一个革命性的AI音乐创作平台,能在短短30秒内帮助用户创作出一首完整的歌曲。无论是寻找创作灵感还是需要快速制作音乐,Suno AI都是音乐爱好者和专业人士的理想选择。

Project Cover

白日梦AI

白日梦AI提供专注于AI视频生成的多样化功能,包括文生视频、动态画面和形象生成等,帮助用户快速上手,创造专业级内容。

Project Cover

有言AI

有言平台提供一站式AIGC视频创作解决方案,通过智能技术简化视频制作流程。无论是企业宣传还是个人分享,有言都能帮助用户快速、轻松地制作出专业级别的视频内容。

Project Cover

Kimi

Kimi AI助手提供多语言对话支持,能够阅读和理解用户上传的文件内容,解析网页信息,并结合搜索结果为用户提供详尽的答案。无论是日常咨询还是专业问题,Kimi都能以友好、专业的方式提供帮助。

Project Cover

讯飞绘镜

讯飞绘镜是一个支持从创意到完整视频创作的智能平台,用户可以快速生成视频素材并创作独特的音乐视频和故事。平台提供多样化的主题和精选作品,帮助用户探索创意灵感。

Project Cover

讯飞文书

讯飞文书依托讯飞星火大模型,为文书写作者提供从素材筹备到稿件撰写及审稿的全程支持。通过录音智记和以稿写稿等功能,满足事务性工作的高频需求,帮助撰稿人节省精力,提高效率,优化工作与生活。

Project Cover

阿里绘蛙

绘蛙是阿里巴巴集团推出的革命性AI电商营销平台。利用尖端人工智能技术,为商家提供一键生成商品图和营销文案的服务,显著提升内容创作效率和营销效果。适用于淘宝、天猫等电商平台,让商品第一时间被种草。

Project Cover

AIWritePaper论文写作

AIWritePaper论文写作是一站式AI论文写作辅助工具,简化了选题、文献检索至论文撰写的整个过程。通过简单设定,平台可快速生成高质量论文大纲和全文,配合图表、参考文献等一应俱全,同时提供开题报告和答辩PPT等增值服务,保障数据安全,有效提升写作效率和论文质量。

投诉举报邮箱: service@vectorlightyear.com
@2024 懂AI·鲁ICP备2024100362号-6·鲁公网安备37021002001498号