sample: Extract all assets to easily try different languages/images

Now the Tesseract engine, language and sample image is specified in Config.java
This commit is contained in:
Robert Pösel 2023-05-27 13:29:22 +02:00
parent c637167c88
commit 6ee37bceb4
3 changed files with 48 additions and 19 deletions

View File

@ -16,21 +16,26 @@ import java.io.OutputStream;
public class Assets {
/**
* Returns locally accessible directory where our assets are extracted.
*/
@NonNull
public static String getTessDataPath(@NonNull Context context) {
// We need to return folder that contains the "tessdata" folder,
// which is in this sample directly the app's files dir
return context.getFilesDir().getAbsolutePath();
public static File getLocalDir(@NonNull Context context) {
return context.getFilesDir();
}
/**
* Returns locally accessible directory path which contains the "tessdata" subdirectory
* with *.traineddata files.
*/
@NonNull
public static String getLanguage() {
return "eng";
public static String getTessDataPath(@NonNull Context context) {
return getLocalDir(context).getAbsolutePath();
}
@NonNull
public static File getImageFile(@NonNull Context context) {
return new File(context.getFilesDir(), "sample.jpg");
return new File(getLocalDir(context), Config.IMAGE_NAME);
}
@Nullable
@ -41,18 +46,32 @@ public class Assets {
public static void extractAssets(@NonNull Context context) {
AssetManager am = context.getAssets();
File imageFile = getImageFile(context);
if (!imageFile.exists()) {
copyFile(am, "sample.jpg", imageFile);
File localDir = getLocalDir(context);
if (!localDir.exists() && !localDir.mkdir()) {
throw new RuntimeException("Can't create directory " + localDir);
}
File tessDir = new File(getTessDataPath(context), "tessdata");
if (!tessDir.exists()) {
tessDir.mkdir();
if (!tessDir.exists() && !tessDir.mkdir()) {
throw new RuntimeException("Can't create directory " + tessDir);
}
File engFile = new File(tessDir, "eng.traineddata");
if (!engFile.exists()) {
copyFile(am, "eng.traineddata", engFile);
// Extract all assets to our local directory.
// All *.traineddata into "tessdata" subdirectory, other files into root.
try {
for (String assetName : am.list("")) {
final File targetFile;
if (assetName.endsWith(".traineddata")) {
targetFile = new File(tessDir, assetName);
} else {
targetFile = new File(localDir, assetName);
}
if (!targetFile.exists()) {
copyFile(am, assetName, targetFile);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}

View File

@ -0,0 +1,12 @@
package cz.adaptech.tesseract4android.sample;
import com.googlecode.tesseract.android.TessBaseAPI;
public class Config {
public static final int TESS_ENGINE = TessBaseAPI.OEM_LSTM_ONLY;
public static final String TESS_LANG = "eng";
public static final String IMAGE_NAME = "sample.jpg";
}

View File

@ -11,11 +11,10 @@ import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import com.googlecode.tesseract.android.TessBaseAPI;
import java.io.File;
import cz.adaptech.tesseract4android.sample.Assets;
import cz.adaptech.tesseract4android.sample.Config;
import cz.adaptech.tesseract4android.sample.databinding.FragmentMainBinding;
public class MainFragment extends Fragment {
@ -38,8 +37,7 @@ public class MainFragment extends Fragment {
if (!viewModel.isInitialized()) {
String dataPath = Assets.getTessDataPath(requireContext());
String language = Assets.getLanguage();
viewModel.initTesseract(dataPath, language, TessBaseAPI.OEM_LSTM_ONLY);
viewModel.initTesseract(dataPath, Config.TESS_LANG, Config.TESS_ENGINE);
}
}