扫描包含变量名的文件

时间:2019-07-05 17:39:39

标签: java tesseract scanning

我有一段简单的代码,当前使用tesseract OCR读取任何给定图像中的文本,然后计算它产生多少行。但是,我想在目录中搜索包含字符串的任何文档(例如M000123456),并返回几个包含其名称的文档,并将其与tesseract输出的数字进行比较。这些文件的名称如下:M000123456_V987654_05-07-2000.pdf。最好的方法是什么?

import java.io.File;

import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;

public class Main {
    public static void main(String[] args) throws TesseractException {
        Tesseract tesseract = new Tesseract();

        tesseract.setDatapath("C:\\Users\\mmx0409\\Downloads\\Tess4J-3.4.8-src\\Tess4J\\tessdata");

        // the path of your tess data folder
        // inside the extracted file
        String text
                = tesseract.doOCR(new File("C:\\Users\\mmx0409\\Downloads\\testimage.png"));

        // path of your image file
        System.out.print(text);
        System.out.println(text.lines().count()); // count the number of lines tesseract saw

    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用以下函数来计算名称中带有searchString的文档的数量。

public int countDocuments(String directoryPath, String searchString) {
    File folder = new File(directoryPath);

    File[] listOfFiles = folder.listFiles();

    int count = 0;

    for (int i = 0; i < listOfFiles.length; i++) {
        if (listOfFiles[i].isFile()) {
            String fileName = listOfFiles[i].getName();
            if (fileName.contains(searchString)) {
                count++;
            }
        }
    }

    return count;
}
相关问题