[Java] Image Resize 방법
Java에서 기본적으로 제공하는 패키지들을 이용하면 손쉽게 이미지 크기를 변경할 수 있습니다.
예시로 작성한 코드는 아래와 같습니다.
package com.example.multipart.lib;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
public class ImageUtils {
public static BufferedImage resize(InputStream inputStream, int width, int height)
throws IOException {
BufferedImage inputImage = ImageIO.read(inputStream);
BufferedImage outputImage =
new BufferedImage(width, height, inputImage.getType());
Graphics2D graphics2D = outputImage.createGraphics();
graphics2D.drawImage(inputImage, 0, 0, width, height, null);
graphics2D.dispose();
return outputImage;
}
}
ImageUtils 의 static 메서드인 resize에서는 입력받은 이미지 파일에 대한 InputStream, 변경하고자 하는 width, height를 인자 값으로 받습니다.
예시를 위한 테스트 코드는 아래와 같이 작성되었습니다.
package com.example.multipart.lib;
import org.junit.jupiter.api.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
class ImageUtilsTest {
@Test
public void imageResizeTest() throws IOException {
Resource resource = new ClassPathResource("dog.jpg");
BufferedImage resizedImage =
ImageUtils.resize(resource.getInputStream(), 800, 600);
ImageIO.write(resizedImage, "jpg", new File("src/main/resources/dog_2.jpg"));
}
}
현재 저의 프로젝트에서 resources 디렉토리 아래에는 dog.jpg 라는 이미지가 들어있습니다. 해당 리소스를 손쉽게 읽어 들이도록 ClassPathResource 클래스를 이용하여 이미지 파일을 읽어 들였습니다.
읽어들인 Resource를 InputStream 과 변경하고자 하는 크기의 너비와 높이 값을 ImageUtils 의 resize 메서드로 넘깁니다. 테스트 코드에서는 너비 800, 높이 600 의 크기로 이미지를 변경하도록 하였습니다. 그리고 변경된 이미지는 같은 위치의 dog_2.jpg 로 저장됩니다.
코드를 실행한 후에 resources 디렉토리 아래에 dog_2.jpg 파일이 생성된 것을 확인해 볼 수 있습니다.
변경 전 이미지의 크기입니다. (2048 x 1365)
변경 후 이미지의 크기입니다. (800 x 600)
참고 자료
https://www.codejava.net/java-se/graphics/how-to-resize-images-in-java
How to resize images in Java
DetailsWritten by Nam Ha Minh Last Updated on 14 August 2019 | Print Email In Java, to resize (or scale) an image read from an image file and save the scaled image into another image file, we can follow these steps: Create a BufferedImage o
www.codejava.net
https://www.mkyong.com/spring/spring-read-file-from-resources-folder/
Spring – Read file from resources folder – Mkyong.com
In Spring, we can use ClassPathResource or ResourceLoader to get files from classpath easily. P.S Tested with Spring 5.1.4.RELEASE 1. src/main/resources/ For example, an image file in the src/main/resources/ folder 2. ClassPathResource import org.springfra
www.mkyong.com
https://www.mkyong.com/spring-boot/spring-boot-file-upload-example/
Spring Boot file upload example – Mkyong.com
This article shows you how to upload a file in Spring Boot web application. Tools used : Spring Boot 1.4.3.RELEASE Spring 4.3.5.RELEASE Thymeleaf Maven Embedded Tomcat 8.5.6 1. Project Structure A standard project structure. 2. Project Dependency Spring bo
www.mkyong.com
예제에서 사용한 이미지 출처