Tess4J是對Tesseract OCR API.的Java JNA 封裝。使java能夠通過調用Tess4J的API來使用Tesseract OCR。支持的格式:TIFF,JPEG,GIF,PNG,BMP,JPEG,and PDF
Tesseract 的github地址:https://github.com/tesseract-ocr/tesseract
Tess4J的github地址:https://github.com/nguyenq/tess4j
Tess4J API 提供的功能:
1、直接識別支持的文件
2、識別圖片流
3、識別圖片的某塊區域
4、將識別結果保存為 TEXT/ HOCR/ PDF/ UNLV/ BOX
5、通過設置取詞的等級,提取識別出來的文字
6、獲得每一個識別區域的具體坐標范圍
7、調整傾斜的圖片
8、裁剪圖片
9、調整圖片分辨率
10、從粘貼板獲得圖像
11、克隆一個圖像(目的:創建一份一模一樣的圖片,與原圖在操作修改上,不相 互影響)
12、圖片轉換為二進制、黑白圖像、灰度圖像
13、反轉圖片顏色
demo.java:
/** * Test of doOCR method, of class Tesseract. * 根據圖片文件進行識別 * @throws Exception while processing image. */ @Test public void testDoOCR_File() throws Exception { logger.info("doOCR on a jpg image"); File imageFile = new File(this.testResourcesDataPath, "ocr.png"); //set language instance.setDatapath(testResourcesLanguagePath); instance.setLanguage("chi_sim"); String result = instance.doOCR(imageFile); logger.info(result); } /** * Test of doOCR method, of class Tesseract. * 根據圖片流進行識別 * @throws Exception while processing image. */ @Test public void testDoOCR_BufferedImage() throws Exception { logger.info("doOCR on a buffered image of a PNG"); File imageFile = new File(this.testResourcesDataPath, "ocr.png"); BufferedImage bi = ImageIO.read(imageFile); //set language instance.setDatapath(testResourcesLanguagePath); instance.setLanguage("chi_sim"); String result = instance.doOCR(bi); logger.info(result); } /** * Test of getSegmentedRegions method, of class Tesseract. * 得到每一個划分區域的具體坐標 * @throws java.lang.Exception */ @Test public void testGetSegmentedRegions() throws Exception { logger.info("getSegmentedRegions at given TessPageIteratorLevel"); File imageFile = new File(testResourcesDataPath, "ocr.png"); BufferedImage bi = ImageIO.read(imageFile); int level = TessPageIteratorLevel.RIL_SYMBOL; logger.info("PageIteratorLevel: " + Utils.getConstantName(level, TessPageIteratorLevel.class)); List<Rectangle> result = instance.getSegmentedRegions(bi, level); for (int i = 0; i < result.size(); i++) { Rectangle rect = result.get(i); logger.info(String.format("Box[%d]: x=%d, y=%d, w=%d, h=%d", i, rect.x, rect.y, rect.width, rect.height)); } assertTrue(result.size() > 0); } /** * Test of doOCR method, of class Tesseract. * 根據定義坐標范圍進行識別 * @throws Exception while processing image. */ @Test public void testDoOCR_File_Rectangle() throws Exception { logger.info("doOCR on a BMP image with bounding rectangle"); File imageFile = new File(this.testResourcesDataPath, "ocr.png"); //設置語言庫 instance.setDatapath(testResourcesLanguagePath); instance.setLanguage("chi_sim"); //划定區域 // x,y是以左上角為原點,width和height是以xy為基礎 Rectangle rect = new Rectangle(84, 21, 15, 13); String result = instance.doOCR(imageFile, rect); logger.info(result); } /** * Test of createDocuments method, of class Tesseract. * 存儲結果 * @throws java.lang.Exception */ @Test public void testCreateDocuments() throws Exception { logger.info("createDocuments for png"); File imageFile = new File(this.testResourcesDataPath, "ocr.png"); String outputbase = "target/test-classes/docrenderer-2"; List<RenderedFormat> formats = new ArrayList<RenderedFormat>(Arrays.asList(RenderedFormat.HOCR, RenderedFormat.TEXT)); //設置語言庫 instance.setDatapath(testResourcesLanguagePath); instance.setLanguage("chi_sim"); instance.createDocuments(new String[]{imageFile.getPath()}, new String[]{outputbase}, formats); } /** * Test of getWords method, of class Tesseract. * 取詞方法 * @throws java.lang.Exception */ @Test public void testGetWords() throws Exception { logger.info("getWords"); File imageFile = new File(this.testResourcesDataPath, "ocr.png"); //設置語言庫 instance.setDatapath(testResourcesLanguagePath); instance.setLanguage("chi_sim"); //按照每個字取詞 int pageIteratorLevel = TessPageIteratorLevel.RIL_SYMBOL; logger.info("PageIteratorLevel: " + Utils.getConstantName(pageIteratorLevel, TessPageIteratorLevel.class)); BufferedImage bi = ImageIO.read(imageFile); List<Word> result = instance.getWords(bi, pageIteratorLevel); //print the complete result for (Word word : result) { logger.info(word.toString()); } } /** * Test of Invalid memory access. * 處理傾斜 * @throws Exception while processing image. */ @Test public void testDoOCR_SkewedImage() throws Exception { //設置語言庫 instance.setDatapath(testResourcesLanguagePath); instance.setLanguage("chi_sim"); logger.info("doOCR on a skewed PNG image"); File imageFile = new File(this.testResourcesDataPath, "ocr_skewed.jpg"); BufferedImage bi = ImageIO.read(imageFile); ImageDeskew id = new ImageDeskew(bi); double imageSkewAngle = id.getSkewAngle(); // determine skew angle if ((imageSkewAngle > MINIMUM_DESKEW_THRESHOLD || imageSkewAngle < -(MINIMUM_DESKEW_THRESHOLD))) { bi = ImageHelper.rotateImage(bi, -imageSkewAngle); // deskew image } String result = instance.doOCR(bi); logger.info(result); }
Tess4JDemo碼雲地址:https://gitee.com/zhaohuihbwj/Tess4JDemo
Java OCR文字識別(Tess4J)
OCR (Optical Character Recognition,光學字符識別)是指電子設備(例如掃描儀或數碼相機)檢查紙上打印的字符,通過檢測暗、亮的模式確定其形狀,然后用字符識別方法將形狀翻譯成計算機文字的過程;即,針對印刷體字符,采用光學的方式將紙質文檔中的文字轉換成為黑白點陣的圖像文件,並通過識別軟件將圖像中的文字轉換成文本格式,供文字處理軟件進一步編輯加工的技術。如何除錯或利用輔助信息提高識別正確率,是OCR最重要的課題,ICR(Intelligent Character Recognition)的名詞也因此而產生。衡量一個OCR系統性能好壞的主要指標有:拒識率、誤識率、識別速度、用戶界面的友好性,產品的穩定性,易用性及可行性等。
Tess4J是對google tesseract ocr的java庫的一種實現
1.maven添加依賴
<!-- https://mvnrepository.com/artifact/net.sourceforge.tess4j/tess4j --> <dependency> <groupId>net.sourceforge.tess4j</groupId> <artifactId>tess4j</artifactId> <version>3.2.1</version> </dependency>2.工具類編輯
/** * tesseract for java, ocr(Optical Character Recognition,光學字符識別) * 工具類 * @author wind */ public class Tess4jUtils { /** * 從圖片中提取文字,默認設置英文字庫,使用classpath目錄下的訓練庫 * @param path * @return */ public static String readChar(String path){ // JNA Interface Mapping ITesseract instance = new Tesseract(); // JNA Direct Mapping // ITesseract instance = new Tesseract1(); File imageFile = new File(path); //In case you don't have your own tessdata, let it also be extracted for you //這樣就能使用classpath目錄下的訓練庫了 File tessDataFolder = LoadLibs.extractTessResources("tessdata"); //Set the tessdata path instance.setDatapath(tessDataFolder.getAbsolutePath()); //英文庫識別數字比較准確 instance.setLanguage(Const.ENG); return getOCRText(instance, imageFile); } /** * 從圖片中提取文字 * @param path 圖片路徑 * @param dataPath 訓練庫路徑 * @param language 語言字庫 * @return */ public static String readChar(String path, String dataPath, String language){ File imageFile = new File(path); ITesseract instance = new Tesseract(); instance.setDatapath(dataPath); //英文庫識別數字比較准確 instance.setLanguage(language); return getOCRText(instance, imageFile); } /** * 識別圖片文件中的文字 * @param instance * @param imageFile * @return */ private static String getOCRText(ITesseract instance, File imageFile){ String result = null; try { result = instance.doOCR(imageFile); } catch (TesseractException e) { e.printStackTrace(); } return result; } public static void main(String[] args) { /*String path = "src/main/resources/image/text.png"; System.out.println(readChar(path));*/ String ch = "src/main/resources/image/ch.png"; System.out.println(readChar(ch, "src/main/resources", Const.CHI_SIM)); } }
注釋:中文結果不夠准確,需要自己訓練字庫
具體訓練字庫,以及完整代碼請移步https://github.com/followwwind/javautils