答案 0 :(得分:0)
我找到了答案,下面的java文件代码:
public class TessOCR {
public static final String DATA_PATH = Environment.getExternalStorageDirectory().toString() + "/Android/data/myOcr/";
public static final String lang = "eng";
private static final String TAG = "---------> TESS_OCR";
private AssetManager assetManager;
private TessBaseAPI mTess;
public TessOCR(AssetManager assetManager) {
Log.i(TAG, DATA_PATH);
this.assetManager = assetManager;
String[] paths = new String[]{DATA_PATH, DATA_PATH + "tessdata/"};
for (String path : paths) {
File dir = new File(path);
if (!dir.exists()) {
if (!dir.mkdirs()) {
Log.v(TAG, "ERROR: Creation of directory " + path + " on sdcard failed");
return;
} else {
Log.v(TAG, "Created directory " + path + " on sdcard");
}
}
}
if (!(new File(DATA_PATH + "tessdata/" + lang + ".traineddata")).exists()) {
try {
InputStream in = assetManager.open("tessdata/" + lang + ".traineddata");
OutputStream out = new FileOutputStream(new File(DATA_PATH + "tessdata/", lang + ".traineddata"));
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
}
in.close();
out.close();
Log.v(TAG, "Copied " + lang + " traineddata");
} catch (IOException e) {
Log.e(TAG, "Was unable to copy " + lang + " traineddata " + e.toString());
}
}
mTess = new TessBaseAPI();
mTess.setDebug(true);
mTess.init(DATA_PATH, lang);
}
public String getResults(Bitmap bitmap) {
if(bitmap != null) {
mTess.setImage(bitmap);
String result = mTess.getUTF8Text();
return result;
}else{
return "empty";
}
}
public void onDestroy() {
if (mTess != null)
mTess.end();
}
}
在主要活动中,您需要将图像获取为位图,然后使用String temp = tess.getResults(imgae.jpg)
将其发送到ocr