如何让Tess4J从PDF文件中获取图像?
我正在使用 OCR (Tess4J) 将图像文件转换为文本。它工作得很好,我已经在图像上进行了测试,效果非常棒。
File imageFile = new File("D:\\HEAD2.png");
Tesseract instance = Tesseract.getInstance(); // JNA Interface Mapping
// Tesseract1 instance = new Tesseract1(); // JNA Direct Mapping
try {
String result = instance.doOCR(imageFile);
System.out.println(result);
} catch (TesseractException e) {
System.err.println(e.getMessage());
}
但是我面临这个问题。我会解析一个包含图像的pdf文件。我不知道该怎么做而且我还没有找到任何带有 pdf 的 Tess4J 示例
我用 Asprise 测试了这个示例,但在 Tess4J 上没有找到类似的示例
import com.asprise.util.pdf.PDFReader;
import com.asprise.util.ocr.OCR;
PDFReader reader = new PDFReader(new File("my.pdf"));
reader.open(); // open the file.
int pages = reader.getNumberOfPages();
for(int i=0; i < pages; i++) {
BufferedImage img = reader.getPageAsImage(i);
// recognizes both characters and barcodes
String text = new OCR().recognizeAll(image);
System.out.println("Page " + i + ": " + text);
}
reader.close(); // finally, close the file.
最佳答案
利用pdfutilities.convertpdf2png并像以前处理图像一样使用它。
关于java - 让Tess4J从PDF文件中获取图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16584995/