1
+ package com .ivory .ivory .ocr ;
2
+
3
+ import com .fasterxml .jackson .databind .ObjectMapper ;
4
+ import com .fasterxml .jackson .databind .node .ArrayNode ;
5
+ import com .fasterxml .jackson .databind .node .ObjectNode ;
6
+ import java .io .IOException ;
7
+ import java .net .HttpURLConnection ;
8
+ import java .net .URL ;
9
+ import java .util .Base64 ;
10
+ import java .util .UUID ;
11
+ import lombok .RequiredArgsConstructor ;
12
+ import lombok .extern .slf4j .Slf4j ;
13
+ import org .springframework .stereotype .Service ;
14
+ import org .springframework .web .multipart .MultipartFile ;
15
+
16
+ @ Service
17
+ @ RequiredArgsConstructor
18
+ @ Slf4j
19
+ public class MedicalCertificateOcrService {
20
+ public String processOcr (MultipartFile file , String apiUrl , String secretKey ) throws IOException {
21
+ // 1. JSON 메시지 생성
22
+ String jsonMessage = createJsonMessage (file );
23
+
24
+ // 2. HTTP 연결 설정
25
+ HttpURLConnection connection = (HttpURLConnection ) new URL (apiUrl ).openConnection ();
26
+ connection .setRequestMethod ("POST" );
27
+ connection .setDoOutput (true );
28
+ connection .setDoInput (true );
29
+ connection .setUseCaches (false );
30
+ connection .setRequestProperty ("Content-Type" , "application/json; charset=UTF-8" );
31
+ connection .setRequestProperty ("x-incizorlens-api-key" , secretKey );
32
+ connection .setReadTimeout (30000 );
33
+
34
+ // 3. JSON 데이터 전송
35
+ try (var outputStream = connection .getOutputStream ()) {
36
+ outputStream .write (jsonMessage .getBytes ("UTF-8" ));
37
+ outputStream .flush ();
38
+ }
39
+
40
+ // 4. 응답 처리
41
+ int responseCode = connection .getResponseCode ();
42
+ System .out .println ("responseCode = " + responseCode );
43
+ System .out .println ("responseMessage = " + connection .getResponseMessage ());
44
+ if (responseCode != HttpURLConnection .HTTP_OK ) {
45
+ throw new IOException ("OCR 요청 실패: 상태 코드 = " + responseCode + connection .getResponseMessage ());
46
+ }
47
+
48
+ // 5. 응답 데이터 읽기
49
+ return new String (connection .getInputStream ().readAllBytes (), "UTF-8" );
50
+ }
51
+
52
+ private String createJsonMessage (MultipartFile file ) throws IOException {
53
+ ObjectMapper objectMapper = new ObjectMapper ();
54
+
55
+ // JSON 메시지 생성
56
+ ObjectNode root = objectMapper .createObjectNode ();
57
+ root .put ("version" , "V2" );
58
+ root .put ("requestId" , UUID .randomUUID ().toString ());
59
+ root .put ("timestamp" , System .currentTimeMillis ());
60
+
61
+ // 이미지 정보 추가
62
+ ArrayNode images = objectMapper .createArrayNode ();
63
+ ObjectNode image = objectMapper .createObjectNode ();
64
+ image .put ("format" , "jpg" );
65
+ image .put ("name" , file .getName ());
66
+
67
+ // 파일 데이터 Base64 인코딩
68
+ String base64File = Base64 .getEncoder ().encodeToString (file .getBytes ());
69
+ image .put ("data" , base64File ); // Base64 데이터를 JSON에 추가
70
+ images .add (image );
71
+
72
+ root .set ("images" , images );
73
+
74
+ return objectMapper .writeValueAsString (root );
75
+ }
76
+ }
0 commit comments