问题描述
所以我试图弄清楚如何使用TessBase,并且在baseApi.init(dataPath, "eng")
处出现错误.我得到的错误是:directory must contain tessdata
.我不知道如何获取包含tessdata的目录.这是包含eng.traineddata的目录的图像.这是我的代码:
So I am trying to figure out how to use TessBase, and I get an error at baseApi.init(dataPath, "eng")
. The error I get is : directory must contain tessdata
. I can't figure out how to get the directory that contains tessdata. This is an image of the directory that contains eng.traineddata. This is my code:
Bundle extras = data.getExtras();
Bitmap photoBitmap = (Bitmap) extras.get("data");
TessBaseAPI baseApi = new TessBaseAPI();
//textcaptured.setText(DATA_PATH.toString());/*
String dataPath = Environment.getExternalStorageDirectory().toString() + "/Android/data/" + getApplicationContext().getPackageName() + "/";
textcaptured.setText(dataPath);
File tessdata = new File(dataPath);
if (!tessdata.exists() || !tessdata.isDirectory()) {
throw new IllegalArgumentException("Data path must contain subfolder tessdata!");
}
baseApi.init(dataPath, "eng");
baseApi.setImage(photoBitmap);
String recognizedText = baseApi.getUTF8Text(); // Log or otherwise display this string...
baseApi.end();
textcaptured.setText(recognizedText);
推荐答案
我推荐的最简单方式是自己在sdcard中创建一个文件夹,并在其中包含tessdata子目录的目录中带有eng.tessdata的目录,如下所示:
The easiest way that I recommend is to make a folder in your sdcard by yourself and put a directory with subdirectory tessdata with eng.tessdata in it the structure shown here:
+SdCardOfPhone
--+YourAppName
----+tessdata
------eng.tessdata
现在您可以通过以下方式指向该目录
now you can point to that directory by
String datapath = Environment.getExternalStorageDirectory() + "/YourAppName/";
TessBaseAPI tessBaseAPI = new TessBaseAPI();
tessBaseAPI.init(datapath, "eng");
您还必须在AndroidManifest.xml文件中包含以下内容
you also have to have the following in your AndroidManifest.xml file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
出于测试目的,我不建议将eng.tessdata捆绑到项目中,因为它会增加构建时间和.apk文件的大小(> 17mb).
For testing purposes I don't recommend bundling your eng.tessdata inside the project it increases the build time and the size of your .apk file(>17mb).
将eng.tessdata捆绑到项目内部的过程非常复杂,涉及到您自己创建一个新目录,然后在运行时将捆绑的数据复制到该目录中.
The process of bundling eng.tessdata inside the project is quite involved and involves making a new directory by yourself and copying the bundled data to that directory in runtime.
这篇关于如何获取需要在tessbase.init("directory","eng")中使用的目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!