-
[Spring] 모바일 이미지 회전 & 썸네일프레임워크/Spring 2020. 4. 28. 13:55
<dependency><groupId>com.drewnoakes</groupId>
<artifactId>metadata-extractor</artifactId>
<version>2.9.1</version></dependency>
public static void thumbnailMaker(String imageFile, int thumbWidth, int thumbHeight, String absolutePath, String originalFilenameExtension, String randomFilename) throws Exception {
File oFile = new File(imageFile);
int orientation = getOrientation(oFile);
BufferedImage oImage = checkImage(oFile,orientation);
int oriWidth = oImage.getWidth(null);
int oriHeight = oImage.getHeight(null);
System.out.println("1원본이미지 너비 = "+oriWidth);
System.out.println("1원본이미지 높이 = "+oriHeight);
int resWidth = thumbWidth;
int resHeight = thumbWidth * oriHeight / oriWidth;
if(resHeight < thumbHeight) {
resWidth = thumbHeight * oriWidth / oriHeight;
resHeight = thumbHeight;
}
int cropWP = (resWidth / 2) - (thumbWidth / 2);
int cropHP = (resHeight / 2) - (thumbHeight / 2);
Image resImage = oImage.getScaledInstance(resWidth, resHeight, Image.SCALE_SMOOTH);
int pixels[] = new int[resWidth * resHeight];
PixelGrabber pixelGrabber = new PixelGrabber(resImage, cropWP, cropHP, resWidth, resHeight, pixels, 0, resWidth );
try {
pixelGrabber.grabPixels();
} catch (InterruptedException e) {
throw new IOException( e.getMessage() );
}
BufferedImage thumbnail = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB );
thumbnail.setRGB(0, 0, thumbWidth, thumbHeight, pixels, 0, resWidth);
ImageIO.write(thumbnail, originalFilenameExtension, new File(absolutePath + "thumb1_" + randomFilename));
}
public static void thumbnailMaker2(File oFile,String absolutePath, String ext, String randomFilename) throws Exception {
//BufferedImage oImage = ImageIO.read(oFile); // 원본이미지
int orientation = getOrientation(oFile);
BufferedImage oImage = checkImage(oFile,orientation);
System.out.println("회전 값 = "+orientation);
System.out.println("원본이미지 너비 = "+oImage.getWidth());
System.out.println("원본이미지 높이 = "+oImage.getHeight());
int tWidth = (int) (oImage.getWidth() / 4); // 생성할 썸네일이미지의 너비
int tHeight = (int) (oImage.getHeight() / 4); // 생성할 썸네일이미지의 높이
BufferedImage tImage = new BufferedImage(tWidth, tHeight, BufferedImage.TYPE_3BYTE_BGR); // 썸네일이미지
Graphics2D graphic = tImage.createGraphics();
Image image = oImage.getScaledInstance(tWidth, tHeight, Image.SCALE_SMOOTH);
graphic.drawImage(image, 0, 0, tWidth, tHeight, null);
graphic.dispose(); // 리소스를 모두 해제
ImageIO.write(tImage, ext, new File(absolutePath + "thumb_" + randomFilename));
}
private static int getOrientation(File file) throws Exception {
int orientation = 1;
Metadata metadata = ImageMetadataReader.readMetadata(file);
Directory directory = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class);
if (directory != null) {
orientation = directory.getInt(ExifIFD0Directory.TAG_ORIENTATION);
}
return orientation;
}
private static BufferedImage checkImage(File file, int orientation) throws Exception {
BufferedImage bi = ImageIO.read(file);
if (orientation == 1) { // 정위치
return bi;
} else if (orientation == 6) {
return rotateImage(bi, 90);
} else if (orientation == 3) {
return rotateImage(bi, 180);
} else if (orientation == 8) {
return rotateImage(bi, 270);
} else{
return bi;
}
}
private static BufferedImage rotateImage (BufferedImage bimage, int radians) {
BufferedImage newImage;
if(radians == 90 || radians == 270) {
newImage = new BufferedImage(bimage.getHeight(),bimage.getWidth(),bimage.getType());
} else if (radians==180){
newImage = new BufferedImage(bimage.getWidth(),bimage.getHeight(),bimage.getType());
} else{
return bimage;
}
Graphics2D graphics = (Graphics2D) newImage.getGraphics();
graphics.rotate(Math.toRadians(radians), newImage.getWidth() / 2, newImage.getHeight() / 2);
graphics.translate((newImage.getWidth() - bimage.getWidth()) / 2, (newImage.getHeight() - bimage.getHeight()) / 2);
graphics.drawImage(bimage, 0, 0, bimage.getWidth(), bimage.getHeight(), null);
return newImage;
}'프레임워크 > Spring' 카테고리의 다른 글
[Spring] H2 database 설정하기 (0) 2022.04.22 [Spring] validation 체크 - 2 (0) 2022.04.20 [Spring] validation 체크 (0) 2022.04.14 [ibatis]다중 Insert 문 (0) 2020.09.21 [Spring]Async (0) 2020.07.01