我有一个 pdf(或任何其他类型的文件,例如 .doc、.ppt 等),其中包含文本和图像。如何使用 Tika 从这些文件中提取图像?
还可以使用 Tess4j 或任何其他库对提取的图像运行 OCR 吗?
这就是我称呼蒂卡的方式:
AutoDetectParser parser = new AutoDetectParser();
BodyContentHandler handler = new BodyContentHandler(writeLimit);
Metadata metadata = new Metadata();
InputStream stream = new FileInputStream("file.pdf");
parser.parse(stream, handler, metadata);
附:我有 tika-app.jar。
最佳答案
实现方法:
InputStream stream = new FileInputStream(inputFile);
Parser parser = new AutoDetectParser();
BodyContentHandler handler = new BodyContentHandler(
Integer.MAX_VALUE);
TesseractOCRConfig config = new TesseractOCRConfig();
PDFParserConfig pdfConfig = new PDFParserConfig();
ParseContext parseContext = new ParseContext();
parseContext.set(TesseractOCRConfig.class, config);
parseContext.set(PDFParserConfig.class, pdfConfig);
parseContext.set(Parser.class, parser); // need to add this to make
// sure recursive parsing
// happens!
Metadata metadata = new Metadata();
parser.parse(stream, handler, metadata, parseContext);
String text = handler.toString().trim();
1) 确保您已使用以下位置的“tesseract-ocr-setup-3.05.00dev.exe”安装了 tesseract: https://sourceforge.net/projects/tesseract-ocr-alt/files/ 并将其路径(如果是 Windows,它将安装在程序文件中)放置在 PATH 环境变量中。如果需要,重新启动 Windows。 传递任何(是的任何!)文件,它就会解压。 2)从以下位置下载tess4j-3.0.0.jar: https://sourceforge.net/projects/tess4j/?source=typ_redirect 并使用以下方法引用此 jar :
<dependency>
<groupId>net.sourceforge.tess4j</groupId>
<artifactId>tess4j</artifactId>
<version>3.0.0</version>
</dependency>
然后,这些:
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-core</artifactId>
<version>1.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.tika/tika-parsers -->
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-parsers</artifactId>
<version>1.13</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>com.github.jai-imageio</groupId>
<artifactId>jai-imageio-core</artifactId>
<version>1.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/net.java.dev.jna/jna -->
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>4.2.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.11</version>
</dependency>
但是,如果使用 Ubuntu,则应使用 apt-get 安装 tesseract。 它会起作用的。
关于apache-tika - 如何使用 Apache TIka 从文件中提取图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31303735/