使用Asprise with Java在PDF上进行OCR

时间:2014-02-24 11:38:18

标签: java ocr

Hye我不熟悉这项工作我在编译代码时遇到了这个错误:

     OCR ocr = new OCR(); 
     PDFReader reader = new PDFReader(new File("C:\\Users\\pc\\Downloads\\chk1.pdf")); 
     reader.open(); // open the file.  
     int pages = reader.getNumberOfPages(); 



     for(int i=0; i<pages; i++) { 
      BufferedImage image = reader.getPageAsImage(i); /////null pointer exception here
      System.out.println("OCR result:\n" + ocr.recognizeCharacters(image));  
     } 

      reader.close(); // finally, close the file. 

错误是:

    java.lang.NullPointerException
at org.pdfbox.util.operator.pagedrawer.Invoke.a(Unknown Source)
at com.asprise.util.pdf.as.a(Unknown Source)
at com.asprise.util.pdf.as.b(Unknown Source)
at com.asprise.util.pdf.as.a(Unknown Source)
at com.asprise.util.pdf.gV.a(Unknown Source)
at com.asprise.util.pdf.G.l(Unknown Source)
at com.asprise.util.pdf.PDFReader.getPageAsImage(Unknown Source)
at file.tracker.threads.PDFFilerConverter.gotoRead(PDFFilerConverter.java:94)
at file.tracker.threads.PDFFilerConverter.run(PDFFilerConverter.java:60)
at java.lang.Thread.run(Thread.java:744)

请有人帮助我摆脱它!

2 个答案:

答案 0 :(得分:1)

试试这个

for(int i=0; i<pages; i++) { 
     String txt =reader.extractTextFromPage(i);
     System.out.println(“Text result:\n” + txt);
}

答案 1 :(得分:0)

Vitalik,您使用的是哪个版本的OCR SDK?

我建议您尝试新的Asprise OCR SDK版本5,它可以解决您的问题。示例代码:

String s = ocr.recognize("C:/test.pdf", -1, 100, 100, 400, 200, 
  Ocr.RECOGNIZE_TYPE_TEXT, Ocr.OUTPUT_FORMAT_PLAINTEXT); 

您可以在此处访问download page of Asprise OCR SDK for Java