1 package org.varienaja.util.coverart;
2
3 import java.awt.Dimension;
4 import java.awt.Graphics2D;
5 import java.awt.GraphicsEnvironment;
6 import java.awt.Image;
7 import java.awt.RenderingHints;
8 import java.awt.image.BufferedImage;
9 import java.io.BufferedOutputStream;
10 import java.io.FileInputStream;
11 import java.io.FileNotFoundException;
12 import java.io.FileOutputStream;
13 import java.io.IOException;
14 import java.io.InputStream;
15
16 import javax.imageio.ImageIO;
17
18 import org.apache.log4j.Logger;
19
20 import com.sun.image.codec.jpeg.JPEGCodec;
21 import com.sun.image.codec.jpeg.JPEGEncodeParam;
22 import com.sun.image.codec.jpeg.JPEGImageEncoder;
23
24
25
26
27
28 public class ImageScaler {
29
30
31
32
33
34
35
36
37
38 public static boolean scale(InputStream input, String dest, int newSize) {
39 BufferedImage image = null;
40 try {
41 image = scale(input,newSize);
42
43 BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(dest));
44 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
45 JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(image);
46 int quality = 90;
47 param.setQuality((float)quality / 100.0f, false);
48 encoder.setJPEGEncodeParam(param);
49 encoder.encode(image);
50 out.close();
51 return true;
52 } catch (Exception e) {
53 return false;
54 }
55 }
56
57 public static boolean scale(String source, String dest, int newSize) throws FileNotFoundException {
58 FileInputStream src = new FileInputStream(source);
59 boolean result = scale(src,dest,newSize);
60 try {
61 src.close();
62 } catch (IOException e) {
63
64 }
65 return result;
66 }
67
68
69
70
71
72
73
74 public static BufferedImage scale(InputStream input, int newSize) throws Exception {
75 try {
76 BufferedImage image = ImageIO.read(input);
77 return scaleInternal(image,newSize);
78 } catch (Exception e) {
79 return null;
80 }
81 }
82
83
84
85
86
87
88
89 public static BufferedImage scale(Image image, int newSize) throws Exception {
90 try {
91 return scaleInternal(image,newSize);
92 } catch (Exception e) {
93 return null;
94 }
95 }
96
97
98
99
100
101
102
103 private static BufferedImage scaleInternal(Image image, int newSize) throws Exception {
104 Dimension resized = calculateNewDimension(image.getWidth(null), image.getHeight(null), newSize);
105
106
107 BufferedImage thumbImage = new BufferedImage(resized.width,resized.height,BufferedImage.TYPE_INT_RGB);
108 Graphics2D graphics2D = thumbImage.createGraphics();
109 graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BICUBIC);
110 graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON );
111 graphics2D.drawImage(image, 0, 0, resized.width, resized.height, null);
112 graphics2D.dispose();
113 return thumbImage;
114 }
115
116
117
118
119
120
121
122
123
124 public static Dimension calculateNewDimension(int width, int height, int newSize) {
125
126 int thumbWidth = newSize;
127 int thumbHeight = newSize;
128 double thumbRatio = (double)thumbWidth / (double)thumbHeight;
129 int imageWidth = width;
130 int imageHeight = height;
131 double imageRatio = (double)imageWidth / (double)imageHeight;
132 if (thumbRatio < imageRatio) {
133 thumbHeight = (int)(thumbWidth / imageRatio);
134 } else {
135 thumbWidth = (int)(thumbHeight * imageRatio);
136 }
137 return new Dimension(thumbWidth, thumbHeight);
138 }
139 }