美文网首页
Android基于tesseract_ocr和tessdata的

Android基于tesseract_ocr和tessdata的

作者: white_or_black | 来源:发表于2017-10-31 10:15 被阅读0次

    一.开发前准备工作

    1.下载tess-two-master项目。https://github.com/rmtheis/tess-two
    2.下载tessdata-master匹配库。https://github.com/tesseract-ocr/tessdata/tree/3.04.00
    3.Android Studio配置ndk的路径。

    JWU8N4YBC$7XBBH$C{)~)%L.png

    二.

    1.新建项目 ,将tess-two作为依赖文件,添加到项目中,同时在build gradle(Mudule :app中添加 compile project(':tess-two'))。

    image.png image.png image.png

    2.在项目的build.gradle(project:xxxx)的dependencis添加以下代码


        classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
        classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.0'
    

    image.png image.png

    3.在MainActivity中的代码(图片文件自己添加就可以了,记得添加文件读写权限)


    public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tvResult;
    private ImageView ivPic;
    private Button btnEng;
    private Button btnClear;
    private Button btnChisim;
    
    private TessBaseAPI mTess;
    private Handler handler;
    private String path;
    private boolean tag;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        initView();
        initData();
        setListener();
    }
    
    private void initView() {
        tvResult = (TextView) findViewById(R.id.tv_result);
        ivPic = (ImageView) findViewById(R.id.iv);
        btnEng = (Button) findViewById(R.id.btn_eng);
        btnClear = (Button) findViewById(R.id.btn_clear);
        btnChisim = (Button) findViewById(R.id.btn_chi_sim);
    }
    
    private void initData() {
        handler = new InnHandler();
        path = getDiskCacheDir(this);
        mTess = new TessBaseAPI();
    
        //存放tessdata的路径,注意放到一个目录的下面,即dir1/dir2/xxxx
        File dir = new File(path, "tessdata");
        if (!dir.exists()) {
            dir.mkdirs();
        }
    }
    
    private void setListener() {
        btnEng.setOnClickListener(this);
        btnChisim.setOnClickListener(this);
        btnClear.setOnClickListener(this);
    }
    
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_eng:
                initTessBaseData("eng", R.mipmap.eng);
                break;
            case R.id.btn_chi_sim:
                initTessBaseData("chi_sim", R.mipmap.chi_sim);
                break;
            case R.id.btn_clear:
                tvResult.setText("");
                break;
            default:
                break;
        }
    }
    
    /**
     * @param language 样本匹配库
     */
    private void initTessBaseData(final String language, final int imgId) {
        tag = false;
        tvResult.setText("识别中。。。");
        ivPic.setImageBitmap(BitmapFactory.decodeResource(getResources(), imgId));
        new Thread(new Runnable() {
            @Override
            public void run() {
                tag = mTess.init(path, language);
                if (tag) {
                    Message msg = new Message();
                    msg.what = 1;
                    msg.obj = imgId;
                    handler.sendMessage(msg);
                }
            }
        }).start();
    }
    
    /**
     * 对图片的识别操作(耗时操作,放到线程里)
     *
     * @param imgId 图片资源ID
     */
    private void method(int imgId) {
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), imgId);
        ivPic.setImageBitmap(bitmap);
        if (tag) {
            mTess.setImage(bitmap);
            //开启一个线程
            new InnThread().start();
        }
    }
    
    /**
     * 获取缓存的目录
     *
     * @param context 上下文对象
     * @return cache目录的路径
     */
    public static String getDiskCacheDir(Context context) {
        String cachePath;
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
                || !Environment.isExternalStorageRemovable()) {
            //SD卡存在,或者sd卡不可移动
            cachePath = context.getExternalCacheDir().getPath();
        } else {
            cachePath = context.getCacheDir().getPath();
        }
        return cachePath;
    }
    
    
    private class InnThread extends Thread {
        @Override
        public void run() {
            super.run();
            synchronized (this) {
                String result = mTess.getUTF8Text();
                Message msg = new Message();
                msg.what = 2;
                msg.obj = result;
                handler.sendMessage(msg);
            }
        }
    }
    
    
    /**
     * 在UI线程中刷新结果
     */
    private class InnHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    if (tag) {
                        int imgId = (int) msg.obj;
                        method(imgId);
                    }
                    break;
                case 2:
                    String result = (String) msg.obj;
                    tvResult.setText(String.valueOf("结果:" + result));
                    break;
                default:
                    break;
            }
    
        }
    }
    

    }


    注意:tessdata文件中我们并不是需要取所有库,测试取2个库放到之前项目中定义的路径下,我这儿是直接放在了/Android/data/com.example.administrator.tesseract_ocr/cache/tessdata/xxx中。

    image.png image.png

    github地址:https://github.com/lucklyperson/Tesseract_ocr

    相关文章

      网友评论

          本文标题:Android基于tesseract_ocr和tessdata的

          本文链接:https://www.haomeiwen.com/subject/tokmpxtx.html