標籤:sys ofo tostring 檔案 -- keyword tle output 儲存
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)2017年10月17日 10:11:10閱讀數:6372
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
JAVA使用Tess4J進行ocr識別