-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThumbnailGenerator.java
56 lines (49 loc) · 1.69 KB
/
ThumbnailGenerator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
* Éú³ÉËõÂÔͼÀà
*/
package com.gootrip.util;
/**
* @author advance
*
*/
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
public class ThumbnailGenerator {
public void transform(String originalFile, String thumbnailFile, int thumbWidth, int thumbHeight, int quality) throws Exception
{
Image image = javax.imageio.ImageIO.read(new File(originalFile));
double thumbRatio = (double)thumbWidth / (double)thumbHeight;
int imageWidth = image.getWidth(null);
int imageHeight = image.getHeight(null);
double imageRatio = (double)imageWidth / (double)imageHeight;
if (thumbRatio < imageRatio)
{
thumbHeight = (int)(thumbWidth / imageRatio);
}
else
{
thumbWidth = (int)(thumbHeight * imageRatio);
}
if(imageWidth < thumbWidth && imageHeight < thumbHeight)
{
thumbWidth = imageWidth;
thumbHeight = imageHeight;
}
else if(imageWidth < thumbWidth)
thumbWidth = imageWidth;
else if(imageHeight < thumbHeight)
thumbHeight = imageHeight;
BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = thumbImage.createGraphics();
graphics2D.setBackground(Color.WHITE);
graphics2D.setPaint(Color.WHITE);
graphics2D.fillRect(0, 0, thumbWidth, thumbHeight);
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);
javax.imageio.ImageIO.write(thumbImage, "JPG", new File(thumbnailFile));
}
}