-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add the extract business number from the image
- Loading branch information
Showing
12 changed files
with
289 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/main/java/com/bit/lot/flower/auth/store/dto/BusinessImageUrlDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.bit.lot.flower.auth.store.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Getter | ||
public class BusinessImageUrlDto { | ||
|
||
private String imageUrl; | ||
} |
53 changes: 53 additions & 0 deletions
53
src/main/java/com/bit/lot/flower/auth/store/dto/ImageRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.bit.lot.flower.auth.store.dto; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Getter | ||
@Builder | ||
public class ImageRequestDto { | ||
|
||
@JsonProperty("images") | ||
private Image[] images; | ||
|
||
@JsonProperty("lang") | ||
private String lang; | ||
|
||
@JsonProperty("requestId") | ||
private String requestId; | ||
|
||
@JsonProperty("resultType") | ||
private String resultType; | ||
|
||
@JsonProperty("timestamp") | ||
private long timestamp; | ||
|
||
@JsonProperty("version") | ||
private String version; | ||
|
||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Getter | ||
@Builder | ||
public static class Image { | ||
|
||
@JsonProperty("format") | ||
private String format; | ||
|
||
@JsonProperty("name") | ||
private String name; | ||
|
||
@JsonProperty("data") | ||
private Object data; | ||
|
||
@JsonProperty("url") | ||
private String url; | ||
|
||
|
||
} | ||
} |
111 changes: 111 additions & 0 deletions
111
src/main/java/com/bit/lot/flower/auth/store/dto/ImageResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package com.bit.lot.flower.auth.store.dto; | ||
|
||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import java.util.List; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Getter | ||
@Builder | ||
public class ImageResponseDto { | ||
|
||
@JsonProperty("version") | ||
private String version; | ||
|
||
@JsonProperty("requestId") | ||
private String requestId; | ||
|
||
@JsonProperty("timestamp") | ||
private long timestamp; | ||
|
||
@JsonProperty("images") | ||
private List<ImageInfo> images; | ||
|
||
|
||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Getter | ||
@Builder | ||
public static class ImageInfo { | ||
|
||
@JsonProperty("uid") | ||
private String uid; | ||
|
||
@JsonProperty("name") | ||
private String name; | ||
|
||
@JsonProperty("inferResult") | ||
private String inferResult; | ||
|
||
@JsonProperty("message") | ||
private String message; | ||
|
||
@JsonProperty("validationResult") | ||
private ValidationResult validationResult; | ||
|
||
@JsonProperty("fields") | ||
private List<FieldInfo> fields; | ||
|
||
|
||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Getter | ||
@Builder | ||
public static class ValidationResult { | ||
|
||
@JsonProperty("result") | ||
private String result; | ||
|
||
|
||
} | ||
|
||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Getter | ||
@Builder | ||
public static class FieldInfo { | ||
|
||
@JsonProperty("valueType") | ||
private String valueType; | ||
|
||
@JsonProperty("boundingPoly") | ||
private BoundingPoly boundingPoly; | ||
|
||
@JsonProperty("inferText") | ||
private String inferText; | ||
|
||
@JsonProperty("inferConfidence") | ||
private double inferConfidence; | ||
|
||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Getter | ||
@Builder | ||
public static class BoundingPoly { | ||
|
||
@JsonProperty("vertices") | ||
private List<Vertex> vertices; | ||
|
||
|
||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Getter | ||
@Builder | ||
public static class Vertex { | ||
|
||
@JsonProperty("x") | ||
private double x; | ||
|
||
@JsonProperty("y") | ||
private double y; | ||
|
||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
src/main/java/com/bit/lot/flower/auth/store/service/NaverClovaOCRService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package com.bit.lot.flower.auth.store.service; | ||
|
||
import com.bit.lot.flower.auth.store.dto.ImageRequestDto; | ||
import com.bit.lot.flower.auth.store.dto.ImageResponseDto; | ||
import com.bit.lot.flower.auth.store.dto.ImageResponseDto.ImageInfo.FieldInfo; | ||
import java.net.URLEncoder; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Optional; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class NaverClovaOCRService implements | ||
RequestBusinessNumberFromImageService { | ||
|
||
private final RestTemplate restTemplate; | ||
@Value("${ocr.naver.secret}") | ||
private String ocrSecret; | ||
|
||
@Override | ||
public String getBusinessNumber(String imageUrl) { | ||
|
||
HttpHeaders headers = new HttpHeaders(); | ||
headers.setContentType(MediaType.APPLICATION_JSON); | ||
|
||
headers.set("X-OCR-SECRET", ocrSecret); | ||
ImageRequestDto requestDto = createImageRequestDtoByImageUrl(imageUrl); | ||
|
||
HttpEntity<ImageRequestDto> requestEntity = new HttpEntity<>(requestDto, headers); | ||
|
||
String apiUrl = "https://5uwmtf47oq.apigw.ntruss.com/custom/v1/27880/634611cfef823f53c9ee0a045d3cbfddfa75b4154fbb56e2b30019466f39fedc/general"; | ||
|
||
|
||
ImageResponseDto response = restTemplate.postForObject(apiUrl, requestEntity, | ||
ImageResponseDto.class); | ||
|
||
return getBusinessNumberFromResponse(response); | ||
|
||
|
||
} | ||
|
||
private String getBusinessNumberFromResponse(ImageResponseDto responseDto) { | ||
|
||
Optional<FieldInfo> businessNumberfield = responseDto.getImages().stream() | ||
.flatMap(imageInfo -> imageInfo.getFields().stream()) | ||
.filter(fieldInfo -> fieldInfo.getInferText().contains("-")) | ||
.findFirst(); | ||
|
||
if (businessNumberfield.isPresent()) { | ||
return businessNumberfield.get().getInferText(); | ||
} | ||
|
||
throw new IllegalArgumentException("사업자 등록 번호를 찾을 수 없습니다."); | ||
} | ||
|
||
private ImageRequestDto createImageRequestDtoByImageUrl(String imageUrl) { | ||
ImageRequestDto.Image[] image = {createImage(imageUrl)}; | ||
return ImageRequestDto.builder().images(image).lang("ko").requestId("string") | ||
.resultType("string") | ||
.version("V1").timestamp(System.currentTimeMillis()).build(); | ||
} | ||
|
||
private ImageRequestDto.Image createImage(String imageUrl) { | ||
return ImageRequestDto.Image.builder().data(null).format("jpg").name("medium").url(imageUrl) | ||
.build(); | ||
|
||
|
||
} | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
...ain/java/com/bit/lot/flower/auth/store/service/RequestBusinessNumberFromImageService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.bit.lot.flower.auth.store.service; | ||
|
||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public interface RequestBusinessNumberFromImageService { | ||
|
||
public String getBusinessNumber(String imageUrl); | ||
} |