FastAPI에서 이미지 가져오기

2024. 10. 19. 23:09Java/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;
        }
    }
}