FastAPI에서 이미지 가져오기
2024. 10. 19. 23:09ㆍJava/Spring Boot
Binary를 직접 가져오기
- pom.xml
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.1</version>
</dependency>
- Spring Boot Code
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.io.InputStream;
@RestController
public class QrCodeController {
@GetMapping("/qrcode")
public ResponseEntity<InputStreamResource> getQrCode(@RequestParam String url) {
try {
// Assume 'generateQRCodeImage' is a method that returns an InputStream for the QR code image
InputStream qrCodeStream = qrcode.generate(url); // Replace this with your QR code generation logic
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.IMAGE_PNG);
return ResponseEntity.ok()
.headers(headers)
.body(new InputStreamResource(qrCodeStream));
} catch (Exception e) {
e.printStackTrace();
return ResponseEntity.status(500).build();
}
}
}
Base64로 변환하기
- pom.xml
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version> <!-- or any other compatible version -->
</dependency>
- Sprng Boot Code
import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
@RestController
public class QrCodeController {
@GetMapping("/qrcode")
public Map<String, String> getQrCode(@RequestParam String url) {
try {
// Get the InputStream of the generated QR code image
InputStream qrCodeStream = qrcode.generate(url);
// Convert InputStream to a byte array
ByteArrayOutputStream baos = new ByteArrayOutputStream();
IOUtils.copy(qrCodeStream, baos);
byte[] qrCodeBytes = baos.toByteArray();
// Encode the image as a Base64 string
String base64Image = Base64.getEncoder().encodeToString(qrCodeBytes);
// Return the Base64 encoded image in a JSON response
Map<String, String> response = new HashMap<>();
response.put("qrCode", base64Image);
return response;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
'Java > Spring Boot' 카테고리의 다른 글
STS4 javascript 메뉴가 보이지 않는 경우 (0) | 2024.11.30 |
---|---|
Spring boot에서 java 변수값을 템플릿에 출력하기 (0) | 2024.11.17 |
Spring boot mapping 설정 (0) | 2024.10.01 |
Spring boot Gmail 연동하여 stmp로 메일보내기 (0) | 2024.08.14 |
Spring boot application.yml 변수 처리 (0) | 2024.08.14 |