【问题标题】:How can I read multiple data from assets?如何从资产中读取多个数据?
【发布时间】:2018-02-16 23:44:44
【问题描述】:

您好,我正在尝试使用 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();
    }
}

如何更改它以复制多个数据?

【问题讨论】:

    标签: java android ocr tesseract android-assets


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-20
      • 2012-03-21
      • 1970-01-01
      • 1970-01-01
      • 2017-03-02
      相关资源
      最近更新 更多