我正在使用tesseract OCR在Android Studio上开发一个应用程序。我编写了一段代码,该代码应该能够识别手机相机拍摄的图像上的文本。问题是:tesseract函数 getUTF8Text() 没有返回任何结果(即使图片上有文本)。程序没有报错。
我考虑可能存在以下问题: 1. 可能我没有正确地将tesseract集成到我的项目中吗?(当在代码中使用tesseract类时,编译器没有显示任何问题) 2. 也许问题在代码中?(训练数据路径错误吗?)
主要类: 代码:
private TessOCR Tess;
//after taking picture I call:
PictureCallback pictureCallback = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
String result = Tess.getOCRResult(bitmap);
if (result != null) Log.i(TAG, result);
else Log.i(TAG, "NO RESULT");
}
};
TessOCR 类用于查找或添加训练数据文件并进行文本识别(构造函数仅用于查找训练数据文件):
public class TessOCR {
public static final String PACKAGE_NAME = "com.example.dainius.ocr";
public static final String DATA_PATH = Environment
.getExternalStorageDirectory().toString() + "/AndroidOCR/";
public static final String lang = "eng";
private static final String TAG = "OCR";
private TessBaseAPI mTess;
public TessOCR(AssetManager assetManager) {
mTess = new TessBaseAPI();
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(DATA_PATH
+ "tessdata/" + lang + ".traineddata");
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
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.setDebug(true);
mTess.init(DATA_PATH, lang);
}
public String getOCRResult(Bitmap bitmap) {
mTess.setImage(bitmap);
String result = mTess.getUTF8Text();
return result;
}
public void onDestroy() {
if (mTess != null)
mTess.end();
}
- 如果这个问题是由于tesseract集成不良引起的,请发布一份适当的教程来介绍如何进行集成,因为互联网上的每篇教程都不同,很难理解如何正确地操作。