1 43 package net.jforum.util.image; 44 45 import java.awt.Dimension ; 46 import java.awt.Graphics ; 47 import java.awt.Image ; 48 import java.awt.image.BufferedImage ; 49 import java.awt.image.PixelGrabber ; 50 import java.io.File ; 51 import java.io.IOException ; 52 import java.util.Iterator ; 53 import java.util.Locale ; 54 55 import javax.imageio.IIOImage ; 56 import javax.imageio.ImageIO ; 57 import javax.imageio.ImageWriteParam ; 58 import javax.imageio.ImageWriter ; 59 import javax.imageio.plugins.jpeg.JPEGImageWriteParam ; 60 import javax.imageio.stream.ImageOutputStream ; 61 62 70 public class ImageUtils 71 { 72 public static final int IMAGE_UNKNOWN = -1; 73 public static final int IMAGE_JPEG = 0; 74 public static final int IMAGE_PNG = 1; 75 public static final int IMAGE_GIF = 2; 76 77 86 public static BufferedImage resizeImage(String imgName, int type, int maxWidth, int maxHeight) throws IOException 87 { 88 return resizeImage(ImageIO.read(new File (imgName)), type, maxWidth, maxHeight); 89 } 90 91 99 public static BufferedImage resizeImage(Image image, int type, int maxWidth, int maxHeight) 100 { 101 Dimension largestDimension = new Dimension (maxWidth, maxHeight); 102 103 int imageWidth = image.getWidth(null); 105 int imageHeight = image.getHeight(null); 106 107 float aspectRation = (float)imageWidth / imageHeight; 108 109 if (imageWidth > maxWidth || imageHeight > maxHeight) { 110 if ((float)largestDimension.width / largestDimension.height > aspectRation) { 111 largestDimension.width = (int)Math.ceil(largestDimension.height * aspectRation); 112 } 113 else { 114 largestDimension.height = (int)Math.ceil(largestDimension.width / aspectRation); 115 } 116 117 imageWidth = largestDimension.width; 118 imageHeight = largestDimension.height; 119 } 120 121 return createBufferedImage(image, type, imageWidth, imageHeight); 122 } 123 124 134 public static boolean saveImage(BufferedImage image, String toFileName, int type) throws IOException 135 { 136 return ImageIO.write(image, type == IMAGE_JPEG ? "jpg" : "png", new File (toFileName)); 137 } 138 139 151 public static void saveCompressedImage(BufferedImage image, String toFileName, int type) throws IOException 152 { 153 if (type == IMAGE_PNG) { 154 throw new UnsupportedOperationException ("PNG compression not implemented"); 155 } 156 157 ImageWriter writer = null; 158 159 Iterator iter = ImageIO.getImageWritersByFormatName("jpg"); 160 writer = (ImageWriter )iter.next(); 161 162 ImageOutputStream ios = ImageIO.createImageOutputStream(new File (toFileName)); 163 writer.setOutput(ios); 164 165 ImageWriteParam iwparam = new JPEGImageWriteParam (Locale.getDefault()); 166 167 iwparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); 168 iwparam.setCompressionQuality(0.7F); 169 170 writer.write(null, new IIOImage (image, null, null), iwparam); 171 172 ios.flush(); 173 writer.dispose(); 174 ios.close(); 175 } 176 177 185 public static BufferedImage createBufferedImage(Image image, int type, int w, int h) 186 { 187 if (type == ImageUtils.IMAGE_PNG && hasAlpha(image)) { 188 type = BufferedImage.TYPE_INT_ARGB; 189 } 190 else { 191 type = BufferedImage.TYPE_INT_RGB; 192 } 193 194 BufferedImage bi = new BufferedImage (w, h, type); 195 196 Graphics g = bi.createGraphics(); 197 g.drawImage(image, 0, 0, w, h, null); 198 g.dispose(); 199 200 return bi; 201 } 202 203 210 public static boolean hasAlpha(Image image) 211 { 212 try { 213 PixelGrabber pg = new PixelGrabber (image, 0, 0, 1, 1, false); 214 pg.grabPixels(); 215 216 return pg.getColorModel().hasAlpha(); 217 } 218 catch (InterruptedException e) { 219 return false; 220 } 221 } 222 } 223 | Popular Tags |