1 | package fr.mael.jiwigo.transverse.util; |
---|
2 | |
---|
3 | import java.awt.Graphics; |
---|
4 | import java.awt.Graphics2D; |
---|
5 | import java.awt.GraphicsConfiguration; |
---|
6 | import java.awt.GraphicsDevice; |
---|
7 | import java.awt.GraphicsEnvironment; |
---|
8 | import java.awt.HeadlessException; |
---|
9 | import java.awt.Image; |
---|
10 | import java.awt.RenderingHints; |
---|
11 | import java.awt.Transparency; |
---|
12 | import java.awt.image.BufferedImage; |
---|
13 | import java.awt.image.ColorModel; |
---|
14 | import java.awt.image.PixelGrabber; |
---|
15 | import java.io.BufferedInputStream; |
---|
16 | import java.io.File; |
---|
17 | import java.io.FileInputStream; |
---|
18 | import java.io.FileNotFoundException; |
---|
19 | import java.io.IOException; |
---|
20 | import java.io.InputStream; |
---|
21 | import java.util.Iterator; |
---|
22 | |
---|
23 | import javax.imageio.IIOImage; |
---|
24 | import javax.imageio.ImageIO; |
---|
25 | import javax.imageio.ImageWriteParam; |
---|
26 | import javax.imageio.ImageWriter; |
---|
27 | import javax.imageio.stream.FileImageOutputStream; |
---|
28 | import javax.swing.ImageIcon; |
---|
29 | |
---|
30 | /* |
---|
31 | Copyright (c) 2010, Mael |
---|
32 | All rights reserved. |
---|
33 | |
---|
34 | Redistribution and use in source and binary forms, with or without |
---|
35 | modification, are permitted provided that the following conditions are met: |
---|
36 | * Redistributions of source code must retain the above copyright |
---|
37 | notice, this list of conditions and the following disclaimer. |
---|
38 | * Redistributions in binary form must reproduce the above copyright |
---|
39 | notice, this list of conditions and the following disclaimer in the |
---|
40 | documentation and/or other materials provided with the distribution. |
---|
41 | * Neither the name of jiwigo nor the |
---|
42 | names of its contributors may be used to endorse or promote products |
---|
43 | derived from this software without specific prior written permission. |
---|
44 | |
---|
45 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
---|
46 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
---|
47 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
---|
48 | DISCLAIMED. IN NO EVENT SHALL Mael BE LIABLE FOR ANY |
---|
49 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
---|
50 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
---|
51 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
---|
52 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
53 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
54 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
55 | */ |
---|
56 | /** |
---|
57 | |
---|
58 | * @author mael |
---|
59 | * |
---|
60 | */ |
---|
61 | public class ImagesUtil { |
---|
62 | /** |
---|
63 | * Logger |
---|
64 | */ |
---|
65 | public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory |
---|
66 | .getLog(ImagesUtil.class); |
---|
67 | |
---|
68 | /** |
---|
69 | * rotates an image |
---|
70 | * @param image the image to rotate |
---|
71 | * @param angle the angle of rotation |
---|
72 | * @return the rotated image |
---|
73 | */ |
---|
74 | public static BufferedImage rotate(BufferedImage image, double angle) { |
---|
75 | GraphicsConfiguration gc = getDefaultConfiguration(); |
---|
76 | double sin = Math.abs(Math.sin(angle)), cos = Math.abs(Math.cos(angle)); |
---|
77 | int w = image.getWidth(), h = image.getHeight(); |
---|
78 | int neww = (int) Math.floor(w * cos + h * sin), newh = (int) Math.floor(h * cos + w * sin); |
---|
79 | int transparency = image.getColorModel().getTransparency(); |
---|
80 | BufferedImage result = gc.createCompatibleImage(neww, newh, transparency); |
---|
81 | Graphics2D g = result.createGraphics(); |
---|
82 | g.translate((neww - w) / 2, (newh - h) / 2); |
---|
83 | g.rotate(angle, w / 2, h / 2); |
---|
84 | g.drawRenderedImage(image, null); |
---|
85 | return result; |
---|
86 | } |
---|
87 | |
---|
88 | /** |
---|
89 | * @return |
---|
90 | */ |
---|
91 | public static GraphicsConfiguration getDefaultConfiguration() { |
---|
92 | GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); |
---|
93 | GraphicsDevice gd = ge.getDefaultScreenDevice(); |
---|
94 | return gd.getDefaultConfiguration(); |
---|
95 | } |
---|
96 | |
---|
97 | /** |
---|
98 | * Writes an image in a file |
---|
99 | * @param file the file |
---|
100 | * @param bufferedImage the image |
---|
101 | * @throws IOException |
---|
102 | */ |
---|
103 | public static void writeImageToPNG(File file, BufferedImage bufferedImage) throws IOException { |
---|
104 | ImageIO.write(bufferedImage, "png", file); |
---|
105 | } |
---|
106 | |
---|
107 | /** |
---|
108 | * scales an image |
---|
109 | * @param filePath the path to the file of the image |
---|
110 | * @param tempName the name of the resulting file |
---|
111 | * @param width the width |
---|
112 | * @param height the height |
---|
113 | * @return true if successful |
---|
114 | * @throws Exception |
---|
115 | */ |
---|
116 | public static boolean scale(String filePath, String tempName, int width, int height) throws Exception { |
---|
117 | File file = new File(filePath); |
---|
118 | InputStream imageStream = new BufferedInputStream(new FileInputStream(filePath)); |
---|
119 | Image image = (Image) ImageIO.read(imageStream); |
---|
120 | |
---|
121 | double thumbRatio = (double) width / (double) height; |
---|
122 | int imageWidth = image.getWidth(null); |
---|
123 | int imageHeight = image.getHeight(null); |
---|
124 | if (imageWidth < width || imageHeight < height) { |
---|
125 | return false; |
---|
126 | } |
---|
127 | double imageRatio = (double) imageWidth / (double) imageHeight; |
---|
128 | if (thumbRatio < imageRatio) { |
---|
129 | height = (int) (width / imageRatio); |
---|
130 | } else { |
---|
131 | width = (int) (height * imageRatio); |
---|
132 | } |
---|
133 | BufferedImage thumbImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); |
---|
134 | Graphics2D graphics2D = thumbImage.createGraphics(); |
---|
135 | graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); |
---|
136 | graphics2D.drawImage(image, 0, 0, width, height, null); |
---|
137 | |
---|
138 | // ImageIO.write(thumbImage, "jpg", new FileOutputStream(System.getProperty("java.io.tmpdir") + "/" + tempName)); |
---|
139 | saveImage(System.getProperty("java.io.tmpdir") + "/" + tempName, thumbImage); |
---|
140 | return true; |
---|
141 | |
---|
142 | } |
---|
143 | |
---|
144 | /** |
---|
145 | * Save an image |
---|
146 | * @param fileName the name of the file |
---|
147 | * @param img the img |
---|
148 | * @throws FileNotFoundException |
---|
149 | * @throws IOException |
---|
150 | */ |
---|
151 | private static void saveImage(String fileName, BufferedImage img) throws FileNotFoundException, IOException { |
---|
152 | Iterator iter = ImageIO.getImageWritersByFormatName("jpeg"); |
---|
153 | ImageWriter writer = (ImageWriter) iter.next(); |
---|
154 | ImageWriteParam iwp = writer.getDefaultWriteParam(); |
---|
155 | iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); |
---|
156 | iwp.setCompressionQuality(1); |
---|
157 | File outputfile = new File(fileName); |
---|
158 | FileImageOutputStream output = new FileImageOutputStream(outputfile); |
---|
159 | writer.setOutput(output); |
---|
160 | IIOImage outimage = new IIOImage(img, null, null); |
---|
161 | writer.write(null, outimage, iwp); |
---|
162 | writer.dispose(); |
---|
163 | } |
---|
164 | |
---|
165 | // This method returns a buffered image with the contents of an image |
---|
166 | public static BufferedImage toBufferedImage(Image image) { |
---|
167 | if (image instanceof BufferedImage) { |
---|
168 | return (BufferedImage) image; |
---|
169 | } |
---|
170 | |
---|
171 | // This code ensures that all the pixels in the image are loaded |
---|
172 | image = new ImageIcon(image).getImage(); |
---|
173 | |
---|
174 | // Determine if the image has transparent pixels; for this method's |
---|
175 | // implementation, see Determining If an Image Has Transparent Pixels |
---|
176 | boolean hasAlpha = hasAlpha(image); |
---|
177 | |
---|
178 | // Create a buffered image with a format that's compatible with the screen |
---|
179 | BufferedImage bimage = null; |
---|
180 | GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); |
---|
181 | try { |
---|
182 | // Determine the type of transparency of the new buffered image |
---|
183 | int transparency = Transparency.OPAQUE; |
---|
184 | if (hasAlpha) { |
---|
185 | transparency = Transparency.BITMASK; |
---|
186 | } |
---|
187 | |
---|
188 | // Create the buffered image |
---|
189 | GraphicsDevice gs = ge.getDefaultScreenDevice(); |
---|
190 | GraphicsConfiguration gc = gs.getDefaultConfiguration(); |
---|
191 | bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency); |
---|
192 | } catch (HeadlessException e) { |
---|
193 | // The system does not have a screen |
---|
194 | } |
---|
195 | |
---|
196 | if (bimage == null) { |
---|
197 | // Create a buffered image using the default color model |
---|
198 | int type = BufferedImage.TYPE_INT_RGB; |
---|
199 | if (hasAlpha) { |
---|
200 | type = BufferedImage.TYPE_INT_ARGB; |
---|
201 | } |
---|
202 | bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type); |
---|
203 | } |
---|
204 | |
---|
205 | // Copy image to buffered image |
---|
206 | Graphics g = bimage.createGraphics(); |
---|
207 | |
---|
208 | // Paint the image onto the buffered image |
---|
209 | g.drawImage(image, 0, 0, null); |
---|
210 | g.dispose(); |
---|
211 | |
---|
212 | return bimage; |
---|
213 | } |
---|
214 | |
---|
215 | /** |
---|
216 | * This method returns true if the specified image has transparent pixels |
---|
217 | * @param image the image to check |
---|
218 | * @return true or false |
---|
219 | */ |
---|
220 | public static boolean hasAlpha(Image image) { |
---|
221 | // If buffered image, the color model is readily available |
---|
222 | if (image instanceof BufferedImage) { |
---|
223 | BufferedImage bimage = (BufferedImage) image; |
---|
224 | return bimage.getColorModel().hasAlpha(); |
---|
225 | } |
---|
226 | |
---|
227 | // Use a pixel grabber to retrieve the image's color model; |
---|
228 | // grabbing a single pixel is usually sufficient |
---|
229 | PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false); |
---|
230 | try { |
---|
231 | pg.grabPixels(); |
---|
232 | } catch (InterruptedException e) { |
---|
233 | } |
---|
234 | |
---|
235 | // Get the image's color model |
---|
236 | ColorModel cm = pg.getColorModel(); |
---|
237 | return cm.hasAlpha(); |
---|
238 | } |
---|
239 | |
---|
240 | } |
---|