我有一个pdf(或任何其他类型的文件,如.doc,.ppt等),其中包含文本和图像。如何使用Tika从这些文件中提取图像?
还可以使用Tess4j或任何其他lib对提取的图像运行OCR吗?
这就是我称之为Tika的方式:
AutoDetectParser parser = new AutoDetectParser();
BodyContentHandler handler = new BodyContentHandler(writeLimit);
Metadata metadata = new Metadata();
InputStream stream = new FileInputStream("file.pdf");
parser.parse(stream, handler, metadata);
P.S。我有tika-app.jar。
答案 0 :(得分:2)
执行此操作的方法:
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。 它会起作用。