Search code examples
javaandroidocrtesseractandroid-assets

How can I read multiple data from assets?


Hi I am trying to use tess-two API to make OCR app

I need to use two languages of trained data

I load my data from assets, but I don't how to load multiple data from it

this is my code:

   private void checkFile(File dir) {
    if (!dir.exists()&& dir.mkdirs()){
        copyFiles();
    }
    if(dir.exists()) {
        String datafilepath = datapath+ "/tessdata/eng.traineddata";
        File datafile = new File(datafilepath);

        if (!datafile.exists()) {
            copyFiles();
        }
    }
}

private void copyFiles() {
    try {
        String filepath = datapath + "/tessdata/eng.traineddata";
        AssetManager assetManager = getAssets();

        InputStream instream = assetManager.open("tessdata/eng.traineddata");
        OutputStream outstream = new FileOutputStream(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();

        File file = new File(filepath);
        if (!file.exists()) {
            throw new FileNotFoundException();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

How can I change it to copy multiple data?


Solution

  • You should copy large files in background thread (AsyncTask or thread), and you can use following logic to copy multiple trainedData files:

    private void checkFile(File dir) {
            if (!dir.exists()) {
                dir.mkdirs();
            }
            if (dir.exists()) {
                copyFiles("/tessdata/eng.traineddata");
                copyFiles("/tessdata/hin.traineddata");
                copyFiles("/tessdata/fra.traineddata");
            }
        }
    
        private void copyFiles(String path) {
            try {
                String filepath = datapath + path;
                if (!new File(filepath).exists()) {
                    AssetManager assetManager = getAssets();
    
                    InputStream instream = assetManager.open(path);
                    OutputStream outstream = new FileOutputStream(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();
    
                    File file = new File(filepath);
                    if (!file.exists()) {
                        throw new FileNotFoundException();
                    }
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }