华域联盟 Andriod Android实现图片文字识别

Android实现图片文字识别

文章目录[隐藏]

导言

OCR,tess-two ,openCV等晕人的东西先分清,OCR,tess-two是图片文字识别,而openCV是图像识别比对,对于更复杂的图片文字识别需求可以采用百度云人工智能通用文字识别开发的SDK,准确性更高

可运行的步骤

1、添加依赖

implementation 'com.rmtheis:tess-two:8.0.0'

2、下载字体识别库(chi_sim.traineddata 中文简体,chi_tra.traineddata 中文繁体,eng.traineddata 英文库)

3、为了apk的大小,我们需要将字体识别库文件拷贝到SD卡目录中,比如eng.traineddata的copy

private String mDataPath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator;
private String mFilePath = mDataPath + File.separator + "tessdata" + File.separator + "eng.traineddata";
private void copyFile() {
        try {
            File mFile = new File(mFilePath);
            if (mFile.exists()) {
                mFile.delete();
            }
            if (!mFile.exists()) {
                File p = new File(mFile.getParent());
                if (!p.exists()) {
                    p.mkdirs();
                }
                try {
                    mFile.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            OutputStream os = new FileOutputStream(mFilePath);
            InputStream is = this.getAssets().open("eng.traineddata");
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = is.read(buffer)) != -1) {
                os.write(buffer, 0, len);
            }
            os.flush();
            os.close();
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

4、tess two初始化

TessBaseAPI baseApi;
baseApi = new TessBaseAPI();
baseApi.init(mDataPath, "eng");

5、处理bitmap图片并识别里面的内容

//OCR图片文字识别
baseApi.setImage(bitmap);
String result = baseApi.getUTF8Text().replace(" ", "").toLowerCase();

6、他要求看需求,本文结束

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持华域联盟。

本文由 华域联盟 原创撰写:华域联盟 » Android实现图片文字识别

转载请保留出处和原文链接:https://www.cnhackhy.com/109517.htm

本文来自网络,不代表华域联盟立场,转载请注明出处。

作者: sterben

Android适配器(Adapter)的概念与自定义

Android自定义ViewGroup实现朋友圈九宫格控件

发表回复

联系我们

联系我们

2551209778

在线咨询: QQ交谈

邮箱: [email protected]

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们