zoukankan      html  css  js  c++  java
  • OCR识别的Android端实现

    1.OCR简介
    OCR (Optical Character Recognition,光学字符识别)是指电子设备(例如扫描仪或数码相机)检查纸上打印的字符,通过检测暗、亮的模式确定其形状,然后用字符识别方法将形状翻译成计算机文字的过程;

    2.Tesseract简介
    Tesseract是Ray Smith于1985到1995年间在惠普布里斯托实验室开发的一个OCR引擎,曾经在1995 UNLV精确度测试中名列前茅。但1996年后基本停止了开发。2006年,Google邀请Smith加盟,重启该项目。目前项目的许可证是Apache 2.0。该项目目前支持Windows、Linux和Mac OS等主流平台。但作为一个引擎,它只提供命令行工具。
    现阶段的Tesseract由Google负责维护,是最好的开源OCR Engine之一,并且支持中文。

    主页地址:https://github.com/tesseract-ocr

    在Tesseract的主页中,我们可以下载到Tesseract的源码及语言包,常用的语言包为

    中文:chi-sim.traineddata

    英文:eng.traineddata

    3.Tess-two
    因为Tesseract使用C++实现的,在Android中不能直接使用,需要封装JavaAPI才能在Android平台中进行调用,这里我们直接使用TessTwo项目,tess-two是TesseraToolsForAndroid的一个git分支,使用简单,切集成了leptonica,在使用之前需要先从git上下载源码进行编译。

    3.1.1 项目地址
    Tess-two在git上地址为:https://github.com/rmtheis/tess-two

    3.1.2 使用

    在你的Android项目中,修改build.gradle 文件,添加如下依赖,即可使用了

    dependencies {
        implementation 'com.rmtheis:tess-two:9.0.0'
    }

    Android 代码如下:

    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.os.Environment;
    import android.os.SystemClock;
    import android.util.Log;
    
    import com.googlecode.tesseract.android.TessBaseAPI;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    /**
     * ocr 识别截图文本
     *
     */
    public class Imagett {
        private static String TAG = "IMAGETT";
        private static final String DEFAULT_LANGUAGE = "chi_sim";
        private static String text;
    
        /**
         *
         * @param imageFile 识别的图片文件
         * @param language 识别的语言 chi_sim : 中文, eng:英文
         * @param refresh 是否重新获取图片
         * @return
         */
        public static String imageToText(final String imageFile, final String language, boolean refresh){ //language :简体中文 chi_sim, 英文 eng
            if (!refresh){
                try {
                    return MyFile.readFile(CONST.TESSDATA + File.separator + "text.txt");  //文件读取操作
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            Thread t = new Thread(new Runnable() {
                @Override
                public void run() {
                    Bitmap bitmap = BitmapFactory.decodeFile(imageFile);
                    TessBaseAPI tessBaseAPI = new TessBaseAPI();
                    tessBaseAPI.init(CONST.LOGPATH, language);
                    tessBaseAPI.setImage(bitmap);
                    text = tessBaseAPI.getUTF8Text();
    //                logUtil.i(TAG, "run: text " + System.currentTimeMillis() + text);
                    //识别的文本内容写入的文件中
                    try {
                        MyFile.writeFile(CONST.TESSDATA + File.separator + "text.txt", text, false); //文件写操作
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
    
                    tessBaseAPI.end();
                }
            });
            t.start();
            //等待识别完成
            while (t.isAlive()){
                SystemClock.sleep(100);
            }
            return text;
        }
        }

    实现的功能,将指定图片内的文字识别后输出的txt文件内

  • 相关阅读:
    fastlane 自动化打包不同的target,以及手动传版本号参数
    xcode 添加Account报错 This action could not be completed,Try again
    iOS 如何在模拟器中安装APP
    git 基础总结
    cnpm run build 报错 Node Sass could not find a binding for your current environment: OS X 64-bit with Node.js 9.x
    iOS 静态库sdk项目依赖到工程项目测试
    Windows下程序的自删除
    80x86指令码查询表(转载)
    C语言实现Win32第一个窗口
    Visual Studio的Unicode和ASCII
  • 原文地址:https://www.cnblogs.com/gaigaige/p/11157188.html
Copyright © 2011-2022 走看看