Pages

2023年5月4日 星期四

Java使用Tesseract進行OCR辨識的一些心得

最近有個專案想抓取PDF的檔案內容,進行檔案rename的動作,因此才開始瞭解Tesseract相關的用法,以下是使用的一些心得。

1. Windows上安裝Tesseract 5.0後,如果想轉移到其他台Windows PC,該PC要先安裝 Visual Studio 2015 的 Visual C++ 可轉散發套件,再將Tesseract安裝的目錄整個zip起來後,在其他台Windows PC上解壓縮後,設定Tesseract相關windows環境變數路徑後,Java程式就可以直接使用。


2. Tesseract程式中辨識資料的路徑與繁體中文語言設定如下:

  1. tesseract.setDatapath("D:\\Tesseract-OCR\\tessdata\\");
  2. tesseract.setLanguage("chi_tra");


3. 不要透過抓取PDF文字後再辨識,因為會抓不到PDF內容文字,要先轉成圖檔(TIFF)後,在進行圖片辨識文字,會比較不會出錯,下面是範例的Code。

  1. private boolean pdfToTiffConverter(String inputPdf, String outputTiff) {
  2. int dpi = 300; // the resolution of the output image in DPI
  3. boolean result = false;
  4. PDDocument document = null;
  5. PDFRenderer pdfRenderer = null;
  6. BufferedImage image = null;
  7. try {
  8. File pdfFile = new File(inputPdf);
  9. Thread.sleep(100); // wait 100ms for loading pdf to memory to avoid NullPointException
  10. document = PDDocument.load(pdfFile);
  11. pdfRenderer = new PDFRenderer(document);
  12. image = pdfRenderer.renderImageWithDPI(0, dpi); // render the first page of the PDF document
  13. ImageIO.write(image, "TIFF", new File(outputTiff)); // write the image to a TIFF file
  14. result = true;
  15. document.close();
  16. } catch (IOException e) {
  17. logger.error(e.getStackTrace());
  18. result = false;
  19. } catch (InterruptedException e) {
  20. logger.error(e.getStackTrace());
  21. } finally {
  22. }
  23. return result;
  24. }
  25.  
  26. private String tiffOcr(String outputTiff) {
  27. String tiffPath = outputTiff;
  28. Tesseract tesseract = new Tesseract();
  29. String result = "";
  30. try {
  31. tesseract.setDatapath("D:\\Tesseract-OCR\\tessdata\\"); // set the path to the Tesseract data directory
  32. tesseract.setLanguage("chi_tra");
  33. result = tesseract.doOCR(new File(tiffPath));
  34. } catch (TesseractException e) {
  35. logger.error(e.getStackTrace());
  36. } finally {
  37. File imageFile = new File(outputTiff);
  38. if(imageFile.exists()) imageFile.delete();
  39. }
  40. return result;
  41. }

4. import的套件很多,可以參考下面圖片的套件版本。


5. 在Eclipse上運行正常,但匯出成可運行的JAR後,辨識時會出現以下錯誤。

javax.imageio.spi.ImageOutputStreamSpi: Provider com.sun.media.imageioimpl.stream.ChannelImageOutputStreamSpi could not be instantiated

解決方式:Eclipse匯出方式需要變更為【copy required libraries into sub-folder....】。


可運行的JAR在執行前,要先把sub-folder的目錄跟可運行的JAR放在同一層執行,這樣才能順利運行。

這個問題花了我5個小時才解決掉,希望對大家有助。




Related Posts:

  • 自動擷取憑證與Https頁面資料在寫JSP使用Redirect的時候,如果是導向HTTPS頁面,也可能發生某些異常。 1. java.net.MalformedURLException: unknown protocol https ANS: 使用的方法不是 HTTPS 的方式,將jce.jar與jsse.jar導入classpath,在使用https連線函式 2. 頁面停留過久 ANS: 就像打開IE一樣,會叫你點選接受憑證,這時用程式跑就自動停住了,居然不會timeout … Read More
  • Linux下打包壓縮war、解壓war包和jar命令把project_a文件夾下的文件打包成project.war 1. 打包  jar -xvf project.war /project_a -c   創建war包 -v   顯示過程信息 -f   指定 JAR 檔案名,通常這個參數是必須的 -M  不產生所有項的清單(MANIFEST〕檔,此參數會忽略 -m 參數 -0   這個是阿拉伯數字,只打包不壓縮的意思 2. 特定檔案… Read More
  • Big5部分中文顯示?處理最近在抓股票資料,但是因為有些上市櫃公司中文字,在預設Big5編碼中不支援,文字抓出來後會顯示?號,因此需要指定特定的編碼方式,才能正確的寫入資料庫中,目前Java的的解決方式如下: File sotckFile = new File(fileName); InputStreamReader isr = null; BufferedReader br = null; try { isr = new InputStreamReader(… Read More
  • Microsoft JDBC 連接 URL 範例 在下列範例中,範例程式碼會在連接 URL 中設定各種連接屬性,然後呼叫 DriverManager 類別的 getConnection 方法,以傳回SQLServerConnection 物件。 接著,範例程式碼會使用 SQLServerConnection 物件的 createStatement 方法建立 SQLServerStatemen… Read More
  • 使用Triple DES 加密字串最近因為有需求,需要使用加密的方法來讓連線的帳號密碼用成亂碼來連線。 因此找到了使用 Tripple DES的方式加密字串。 1. 首先,先寄建立Decrypter與Encrypter package security.crypto; import java.io.UnsupportedEncodingException; import javax.crypto.Cipher; import javax.crypto.IllegalBloc… Read More

0 意見: