Java 实现OCR 识别图像文字(手写中文)----tess4j
?
最近有個需要,小程序端手寫中文之后生成圖像,后端需識別圖片上中文..;剛開始想到第三方收費api試試,先用了百度AI開放平臺的通用字體識別的API,后來又找到了Tessearct-OCR,參考了幾篇文摘整合了一下
準備:
1.下載Tessearct-COR 3.0以上版本:https://download.csdn.net/download/qq_26161693/10646074
2. 在安裝時選擇chi_sim.traineddata 語言庫;之后在程序中需加載安裝目錄tessdata下的中文包(chi_sim.traineddata );
maven依賴:
? ? ? ? <dependency>
?? ??? ? ? ?<groupId>net.sourceforge.tess4j</groupId>
?? ??? ? ? ?<artifactId>tess4j</artifactId>
?? ??? ? ? ?<version>3.2.1</version>
?? ??? ?</dependency>
?
Demo:
?? ?/**
?? ? *?
?? ? * @param srImage 圖片路徑
?? ? * @param ZH_CN 是否使用中文訓練庫,true-是
?? ? * @return 識別結果
?? ? */
?? ?public static String discernWord(String imagePath) {
?? ??? ?try {
?? ??? ??? ?File image = new File(imagePath);
?? ??? ??? ?BufferedImage textImage = ImageIO.read(image);
?? ??? ??? ?Tesseract instance = Tesseract.getInstance();
?? ??? ??? ?instance.setDatapath("C:\\Program Files (x86)\\Tesseract-OCR\\tessdata");// 設置語言庫
? ? ? ? ? ? instance.setLanguage("chi_sim");// 中文識別
?? ??? ??? ?String words = null;
?? ??? ??? ?words = instance.doOCR(textImage);
?? ??? ??? ?return words;
?? ??? ?}
?? ??? ?catch (Exception e) {
?? ??? ??? ?e.printStackTrace();
?? ??? ?}
?? ?}
Test:
?public static void main(String[] args) throws Exception {
?? ??? ?String words = discernWord("F:/test_used_url/ocr/originalPic/hotkidclub.jpg", true);//需識別的圖文件路徑
?? ??? ?System.out.println(words);
?? ?}
ps:
在window開發環境下先安裝了tesseract親測可行,不過沒試過不安裝exe只加載語言包的情;況
隨后在部署到Linux環境下運行會有種種的坑
解決:1)linux安裝Tesseract-OCR之后,將.so相關文件復制到/usr/lib目錄
? ? ? ? ?2)在項目的根目錄(maven的話就是resources)下添加:linux-86-64文件夾
? ? ? ? ?3)配置Linux的語言環境變量
? ? ? ? ?4)若訪問量大的情況tomcat也容易崩掉,需設置好線程數或并發量;
?
詳情參考:http://www.cnblogs.com/zlAurora/p/9266039.html;
總結
以上是生活随笔為你收集整理的Java 实现OCR 识别图像文字(手写中文)----tess4j的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数字地球各类贴图资源下载地址
- 下一篇: 给android studio安装新字体