1

这是我目前正在做的事情:

  • tess-two 在我的 Android 项目中设置

安装了 tess-2 的文件目录

  • 我在我的主应用程序的 AndroidManifest.xml 中指定了权限(不是 tess-two AndroidManifest.xml):

权限

  • 我还在我的代码中明确检查权限:

    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();
    }
    
  • 尝试初始化 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;
            }
        }
    }
    

当我运行该应用程序时,我的日志中出现以下错误:

E/Tesseract(native): Could not initialize Tesseract API with language=eng!

我不知道从这里去哪里,所以任何帮助或建议将不胜感激,谢谢:)

4

1 回答 1

0

确保您使用的是正确版本的训练数据文件

于 2017-04-16T14:02:58.087 回答