-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOcrService.java
More file actions
36 lines (29 loc) · 1.12 KB
/
OcrService.java
File metadata and controls
36 lines (29 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.example.ocr.service;
import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
@Service
public class OcrService {
private final ITesseract tesseract;
public OcrService() {
this.tesseract = new Tesseract();
this.tesseract.setDatapath("C:/Tesseract-OCR/tessdata-main/tessdata-main"); // Tesseract 데이터 폴더 경로 설정
this.tesseract.setLanguage("kor"); // 한국어 OCR 설정
}
public String extractText(MultipartFile file) throws IOException {
File tempFile = File.createTempFile("ocr_", ".png");
file.transferTo(tempFile);
try {
return tesseract.doOCR(tempFile);
} catch (TesseractException e) {
e.printStackTrace();
return "OCR 처리 실패";
} finally {
tempFile.deleteOnExit(); // 임시 파일 삭제
}
}
}