Here's what I'm currently doing:
- tess-two is set up in my Android project
- I have permissions specified in the AndroidManifest.xml of my main app (not the tess-two AndroidManifest.xml):
I also check for permissions explicitly in my code:
int readPermission = ActivityCompat.checkSelfPermission(this, READ_EXTERNAL_STORAGE); int writePermission = ActivityCompat.checkSelfPermission(this, WRITE_EXTERNAL_STORAGE); // Check we have both read and write permissions if (readPermission != PackageManager.PERMISSION_GRANTED || writePermission != PackageManager.PERMISSION_GRANTED) { // We don't have permission so prompt the user ActivityCompat.requestPermissions( this, new String[] {READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE}, REQUEST_EXTERNAL_STORAGE ); } else { Log.d(TAG, "Read and write external permissions granted"); initTess(); }
Try to initialise the TessBaseAPI:
private void initTess() { // Check we have the eng.traineddata file in the correct place mTessDataPath = getFilesDir() + "/tesseract/"; checkTessFile(new File(mTessDataPath + "tessdata/")); // Initialise TessBaseAPI mTess = new TessBaseAPI(); mTess.init(mTessDataPath, "eng"); } private void checkTessFile(File dir) { // Check if directory already exists if (dir.exists()) { // Check if file already exists String dataFilePath = mTessDataPath + "tessdata/eng.traineddata"; File datafile = new File(dataFilePath); if (!datafile.exists()) { // If file doesn't exist, copy it over from assets folder copyTessFiles(); } } else { if (dir.mkdirs()) { // If directory doesn't exist, but we can create it, copy file from assets folder copyTessFiles(); } } } private void copyTessFiles() { try { // Location we want the file to be at String filepath = mTessDataPath + "tessdata/eng.traineddata"; // Get access to AssetManager AssetManager assetManager = getAssets(); // Open byte streams for reading/writing InputStream instream = assetManager.open("tessdata/eng.traineddata"); OutputStream outstream = new FileOutputStream(filepath); // Copy the file to the location specified by filepath byte[] buffer = new byte[1024]; int read; while ((read = instream.read(buffer)) != -1) { outstream.write(buffer, 0, read); } outstream.flush(); outstream.close(); instream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case REQUEST_EXTERNAL_STORAGE: { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { // Initialise Tesseract API initTess(); } return; } } }
When I run the app, I get the following error in my logs:
E/Tesseract(native): Could not initialize Tesseract API with language=eng!
I have no idea where to go from here, so any help or advise would be hugely appreciated, thank you :)