Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从PDF格式的图像中读取数据_Java_Itext_Pdf Parsing - Fatal编程技术网

Java 从PDF格式的图像中读取数据

Java 从PDF格式的图像中读取数据,java,itext,pdf-parsing,Java,Itext,Pdf Parsing,我正在使用iText java TextExtraction从PDF文件中读取文本。我使用下面的代码,它对英文PDF非常有效,现在我有了包含数据的PDF图像。我想从图像中读取数据 public class pdfreader { public static void main(String[] args) throws IOException, DocumentException, TransformerException { String SRC = "";

我正在使用iText java TextExtraction从PDF文件中读取文本。我使用下面的代码,它对英文PDF非常有效,现在我有了包含数据的PDF图像。我想从图像中读取数据

public class pdfreader {
    public static void main(String[] args) throws IOException, DocumentException, TransformerException {
        String SRC = "";
        String DEST = "";

        for (String s : args) {
            SRC = args[0];
            DEST = args[1];
        }
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new pdfreader().readText(SRC, DEST);
    }

    public void readText(String src, String dest) throws IOException, DocumentException, TransformerException {
         try {
                PdfReader pdfReader = new PdfReader(src);
                PdfReaderContentParser PdfParser = new PdfReaderContentParser(
                        pdfReader);
                PrintWriter out = new PrintWriter(new FileOutputStream(
                        dest));
                TextExtractionStrategy textStrategy;
                for (int i = 1; i <= pdfReader.getNumberOfPages(); i++) {
                    textStrategy = PdfParser.processContent(i,
                            new SimpleTextExtractionStrategy());
                    out.println(textStrategy.getResultantText());
                }
                out.flush();
                out.close();
                pdfReader.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
公共类PDF阅读器{
公共静态void main(字符串[]args)引发IOException、DocumentException、TransformerException{
字符串SRC=”“;
字符串DEST=“”;
for(字符串s:args){
SRC=args[0];
DEST=args[1];
}
文件文件=新文件(DEST);
文件.getParentFile().mkdirs();
新建pdfreader().readText(SRC,DEST);
}
public void readText(String src,String dest)抛出IOException、DocumentException、TransformerException{
试一试{
PdfReader PdfReader=新PdfReader(src);
PdfReaderContentParser PdfParser=新的PdfReaderContentParser(
pdfReader);
PrintWriter out=新的PrintWriter(新文件输出流(
目的地);
文本提取策略文本策略;

对于(int i=1;iiText不支持OCR从图像中提取文本。请尝试使用或其他方法。

您可以使用iText实现OCR工作流。正如Amedee已经暗示的,这是我们在iText中尝试过的方法,具有非常好的效果

算法(高级):

  • 实现IEventListener来解析文档的页面
  • 注意ImageRenderInfo事件,当PDF解析器点击图像时会触发这些事件
  • 您可以对事件调用
    getImage()
    ,最终获得一个BuffereImage
  • 将BuffereImage馈送到Tesseract
  • 应用坐标变换(tesseract不使用与iText相同的坐标空间)
  • 现在图像中有了texf和位置,您可以使用iText在PDF上覆盖文本,或者简单地提取文本

  • 我可以用iText从PDF中提取图像吗?是的,你可以用iText从PDF中提取图像,然后将图像提供给Tessaract,但这不是你的问题。我想先提取图像,然后从图像中读取数据