본문 바로가기

Java

java Ocr

반응형

1. 개발 환경

       - 환경은 SpringBoot, Gradle에서 진행했다.

2. 준비

       - gradle :추가

//ocr
implementation 'net.sourceforge.tess4j:tess4j:4.5.4'

3. java 소스

	/**
	 * ocr
	 * @param reqMap
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/ocr")
	public Map<String, Object> ocr(RequestMap reqMap) throws Exception {
		Map<String, Object> param = reqMap.getMap();
        try {
        	Tesseract tesseract = new Tesseract();
        	
        	tesseract.setDatapath("D://tessdata");
        	
            File imageFile = new File("D://img//sns.jpg");
            
            System.out.println("exists : " + imageFile.exists());
            // 이미지에서 텍스트 추출
            String result = tesseract.doOCR(imageFile);
            System.out.println(result);
            
            
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        
		return null;
	}

여기에서 중요한 부분은 setDatapath부분이다. 학습된 언어별 데이터가 필요하며

해당 파일들이 들어있는 경로를 임포트해야 한다.

 

하기 링크에서 다운로드 가능하다.

https://github.com/tesseract-ocr/tessdata_best

반응형

'Java' 카테고리의 다른 글

CORS오류  (1) 2024.04.30