如何从资产中读取多个数据?

How can I read multiple data from assets?

您好,我正在尝试使用 tess-two API 制作 OCR 应用程序

我需要使用两种语言的训练数据

我从资产加载我的数据,但我不知道如何从它加载多个数据

这是我的代码:

   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();
    }
}

如何更改为复制多个数据?

您应该在后台线程(AsyncTask 或线程)中复制大文件,您可以使用以下逻辑复制多个训练数据文件:

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();
        }
    }