티스토리 뷰
고화질 썸네일을 찾기 위해 이것저것 테스트해 본 것이 아까워서 적어본다.
나중에 재사용할 수 있도록 ...
1. 라이브러리 추가 없이 기본으로 생성하는 방법(1)
public Bufferedimage createThumbnail(MultipartFile mfile, int thumbWidth, int thumbHeight){
InputStream in = mfile.getInputStream();
BufferedImage originalImage = ImageIO.read(in);
BufferedImage thumbImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
Graphics2D g = thumbImage.createGraphics();
g.setBackground(Color.WHITE);
g.drawImage(originalImage, 0, 0, width, height, null);
g.dispose();
//퀄리티를 높히려면 RendringHints를 적용하라고 하는데... 효과가 눈에 잘 보이지 않는다 ㅠㅠ
//g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
//g.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
//g.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
//g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
in.close();
return thumbImage;
}
2. 라이브러리 추가 없이 기본으로 생성하는 방법(2)
public Bufferedimage createThumbnail(MultipartFile mfile, int thumbWidth, int thumbHeight){
InputStream in = mfile.getInputStream();
Image originalImage = null;
String imgOriginalName = mfile.getOriginalFilename();
String imgSuffix = imgOriginalName.indexOf(".") > 0 ?
imgOriginalName.substring(imgOriginalName.lastIndexOf(".")) : "";
if(imgSuffix.equals("png") || imgSuffix.equals("gif")){
originalImage = ImageIO.read(in);
} else {
originalImage = new imageIcon(IOUtils.toByteArray(in)).getImage();
}
int originalWidth = originalImage.getWidth(null);
int originalHeight = originalImage.getHeight(null);
if((originalWidth > thumbWidth) || (originalHeight > thumbHeight)){
double deltaWidth = (originalWidth - thumbWidth) / thumbWidth;
double deltaHeight = (originalHeight - thumbHeight) / thumbHeight;
double scaleFactor = 1.0;
if(deltaHeight > deltaWidth){
scaleFactor = (double) thumbHeight / (double) originalHeight;
} else {
scaleFactor = (double) thumbWidth / (double) originalWidth;
}
thumbWidth = originalWidth * scaleFactor;
thumbHeight = originalHeight * scaleFactor;
}
if(thumbWidth < 1) thumbWidth = 1;
if(thumbHeight < 1) thumbHeight = 1;
Image imgTarget = originalImage.getScaledInstance(thumbWidth, thumbHeight, Image.SCALE_SMOOTH);
int pixels[] = new int[thumbWidth * thumbHeight];
PixelGrabber pg = new PixelGrabber(imgTarget, 0, 0, thumbWidth, thumbHeight, pixels, 0, thumbWidth);
try{
pg.grabPixels();
} catch (InterruptedException e){
throw new IOException(e.getMessage());
}
BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_BGR);
thumbImage.setRGB(0, 0, thumbWidth, thumbHeight, pixel, 0, thumbWidth);
in.close();
return thumbImage;
}
3. java-image-scaling 라이브러리 이용
*출처 : https://handcoding.tistory.com/147
*pom.xml에 dependency를 추가한다.
<dependency>
<group>com.mortennobel</group>
<artifactId>java-image-scaling</artifactId>
<version>0.8.6</version>
</dependency>
public Bufferedimage createThumbnail(MultipartFile mfile, int thumbWidth, int thumbHeight){
InputStream in = mfile.getInputStream();
BufferedImage originalImage = ImageIO.read(in);
MultiStepRescaleOp rescale = new MultiStepRescaleOp(thumbWidth, thumbHeight);
rescale.setUnsharpenMask(AdvancedResizeOp.UnsharpenMask.Soft);
BufferedImage thumbImage = rescale.filter(originalImage, null);
in.close();
return thumbImage;
}
4. imagscalr 라이브러리 이용
*출처 : https://offbyone.tistory.com/114
*pom.xml에 dependency를 추가한다.
<dependency>
<group>org.imgscalr</group>
<artifactId>imgscalr-lib</artifactId>
<version>4.2</version>
</dependency>
public Bufferedimage createThumbnail(MultipartFile mfile, int thumbWidth, int thumbHeight){
InputStream in = mfile.getInputStream();
BufferedImage originalImage = ImageIO.read(in);
BufferedImage thumbImage = Scalr.resize(originalImage, thumbWidth, thumbHeight);
//만약 자동 조절 기능을 사용하려면
//가로 기준일 때,
// BufferedImage thumbImage = Scalr.resize(originalImage, Scalr.Method.AUTOMATIC, Scalr.Mode.FIT_TO_WIDTH, thumbWidth);
//세로 기준일 때,
// BufferedImage thumbImage = Scalr.resize(originalImage, Scalr.Method.AUTOMATIC, Scalr.Mode.FIT_TO_HEIGHT, thumbHeight);
in.close();
return thumbImage;
}
5. thumbnailator 라이브러리 이용
*출처 : https://codeday.me/ko/qa/20190321/112754.html
*pom.xml에 dependency를 추가한다.
<dependency>
<group>net.coobird</group>
<artifactId>thumbnailator</artifactId>
<version>0.4.8</version>
</dependency>
public Bufferedimage createThumbnail(MultipartFile mfile, int thumbWidth, int thumbHeight){
InputStream in = mfile.getInputStream();
BufferedImage originalImage = ImageIO.read(in);
BufferedImage thumbImage = Thumbnails.of(originalImage).size(width, height).asBufferedimage();
//보다 고화질을 얻기 위해서는
//Thumbnails.of(originalImage).size(width, height).outputQuality(1.0f).outputFormat("png").asBufferedimage();
in.close();
return thumbImage;
}