Skip to content

Commit

Permalink
Merge pull request #22 from potenday-23/feature/login_exception
Browse files Browse the repository at this point in the history
Feat: 이미지 ocr 추가
  • Loading branch information
rrosiee authored Jul 24, 2024
2 parents 9e9ca6d + 5c6affe commit 47425f9
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import project.backend.domain.culturalevent.entity.CulturalEvent;
import project.backend.domain.member.entity.Member;
import project.backend.domain.ticket.dto.TicketCreateDto;
Expand Down Expand Up @@ -42,4 +44,14 @@ public ResponseEntity retrieve(@Positive @PathVariable Long id) {
Ticket ticket = ticketService.getTicket(id);
return ResponseEntity.status(HttpStatus.OK).body(ticketMapper.ticketToTicketRetrieveDto(ticket));
}

@ApiOperation(value = "티켓 정보 추출(ocr)")
@PreAuthorize("isAuthenticated()")
@PostMapping(value = "/extract-info", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity extractInfo(
@RequestPart(value = "file") MultipartFile file
) {
ResponseEntity<String> response = ticketService.extractInfoFromImage(file);
return ResponseEntity.status(response.getStatusCode()).body(response.getBody());
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package project.backend.domain.ticket.service;

import lombok.RequiredArgsConstructor;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.util.UriComponentsBuilder;
import project.backend.domain.media.dto.MediaDto;
import project.backend.domain.media.entity.Media;
import project.backend.domain.media.service.MediaService;
Expand All @@ -14,6 +21,8 @@
import project.backend.global.error.exception.BusinessException;
import project.backend.global.error.exception.ErrorCode;

import java.io.IOException;

@Service
@RequiredArgsConstructor
@Transactional
Expand Down Expand Up @@ -57,4 +66,49 @@ public Ticket getTicket(Long id) {
public Ticket verifiedTicket(Long id) {
return ticketRepository.findById(id).orElseThrow(() -> new BusinessException(ErrorCode.TICKET_NOT_FOUND));
}

public ResponseEntity<String> extractInfoFromImage(MultipartFile file) {
try {
// Prepare headers
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);

// Create MultiValueMap to hold the file
LinkedMultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add("image", getByteArrayResource(file));

// Create an HttpEntity with the file and headers
HttpEntity<LinkedMultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);

// Build the URI for the OCR API
UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromHttpUrl("http://13.125.32.85:8000/api/ocr");

// Create RestTemplate instance
RestTemplate restTemplate = new RestTemplate();

// Send request to OCR API and get the response
ResponseEntity<String> response = restTemplate.exchange(
uriBuilder.toUriString(),
HttpMethod.POST,
requestEntity,
String.class
);

// Return the response from OCR API to the client
return response;
} catch (IOException e) {
throw new BusinessException(ErrorCode.NOTICE_NOT_FOUND);
} catch (Exception e) {
throw new BusinessException(ErrorCode.NOTICE_NOT_FOUND);
}
}

private Resource getByteArrayResource(MultipartFile file) throws IOException {
return new ByteArrayResource(file.getBytes()) {
@Override
public String getFilename() {
return file.getOriginalFilename();
}
};
}
}

0 comments on commit 47425f9

Please sign in to comment.