I am making an Android Studio application with Tesseract OCR. I made a code that should recognize text on images taken by the phone’s camera. Problem: tesseract getUTF8Text () does not produce AT ALL (null, even though the image has text). The program does not give any errors.
I wondered about possible problems: 1. Maybe I didn’t include tesseract in my project correctly? (The compiler does not detect problems when using tesseract classes in code). 2. Could there be a problem with the code? (bad path traveled?).
Main class: Code:
private TessOCR Tess;
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 class for searching and adding text and tesseract text recognition for training (Constructor is intended only for searching the file of trained data):
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();
}