3 import java.awt.Dimension;
4 import java.awt.Graphics;
6 import java.awt.image.BufferedImage;
7 import java.awt.image.ConvolveOp;
8 import java.awt.image.Kernel;
10 import javax.swing.ImageIcon;
13 * Class for providing generic image processing functions
15 public abstract class ImageUtils
17 private static final float SMOOTH_FACTOR = 0.008f;
18 private static ConvolveOp CONVOLVER = null;
20 /** Static block for initialization */
23 float[] smoothMatrix = {
25 SMOOTH_FACTOR, 1-(SMOOTH_FACTOR*4), SMOOTH_FACTOR,
28 CONVOLVER = new ConvolveOp(new Kernel(3, 3, smoothMatrix));
33 * Create a scaled and smoothed image according to the specified size
34 * @param inImage image to scale
35 * @param inWidth width to scale to
36 * @param inHeight height to scale to
37 * @return BufferedImage containing scaled result
39 public static BufferedImage createScaledImage(Image inImage, int inWidth, int inHeight)
41 // create smaller image and force its loading
42 Image smallerImage = inImage.getScaledInstance(inWidth, inHeight, Image.SCALE_SMOOTH);
43 Image tempImage = new ImageIcon(smallerImage).getImage();
44 tempImage.getWidth(null);
46 // create buffered image to do transform
47 BufferedImage buffer = new BufferedImage(inWidth, inHeight, BufferedImage.TYPE_INT_RGB);
48 // copy scaled picture into buffer
49 Graphics buffG = buffer.getGraphics();
50 buffG.drawImage(smallerImage, 0, 0, inWidth, inHeight, null);
54 smallerImage = null; tempImage = null;
55 // smooth scaled image using a normalized 3x3 matrix - taking next neighbour
56 buffer = CONVOLVER.filter(buffer, null);
63 * Work out the max size of a thumbnail
64 * @param inOrigWidth width of original picture
65 * @param inOrigHeight height of original picture
66 * @param inMaxWidth max width of thumbnail
67 * @param inMaxHeight max height of thumbnail
68 * @return size of thumbnail as Dimension
70 public static Dimension getThumbnailSize(int inOrigWidth, int inOrigHeight, int inMaxWidth, int inMaxHeight)
72 if (inMaxWidth <= 0 || inMaxHeight <= 0)
74 //System.out.println("Can't do it - maxwidth=" + inMaxWidth + ", maxheight=" + inMaxHeight);
75 return new Dimension(0,0);
77 // work out maximum zoom ratio available so that thumbnail isn't too big
78 double xZoom = inMaxWidth * 1.0 / inOrigWidth;
79 double yZoom = inMaxHeight * 1.0 / inOrigHeight;
80 double zoom = (xZoom > yZoom?yZoom:xZoom);
81 // Don't make thumbnail bigger than picture
82 if (zoom > 1.0) {return new Dimension(inOrigWidth, inOrigHeight);}
83 // calculate new width and height
84 return new Dimension ((int) (zoom * inOrigWidth), (int) (zoom * inOrigHeight));