Sign in using Google or Yahoo! id.  

Image scaling: computing the new dimension based on max value

 Comments Share:   Twitter   Reddit   HackerNews   Facebook 

Requirement:

  • The scaled image should maintain aspect-ratio.
  • If the original image is smaller than the max size, do not scale.
  • The width/height should not exceed the max parameter.

Code:

Code to compute scaled dimension:

import java.awt.Dimension;

...

static Dimension getDimension(final int max,
                              final int width,
                              final int height) {
    if(width < max && height < max) { // do not resize!
        return new Dimension(width, height);
    }
    else if(width == height) {
        return new Dimension(max, max);
    }
    else if(width > height) {
        final int w = max;
        final int h = (max * height) / width;
        return new Dimension(w, h);
    }
    else{
        final int h = max;
        final int w = (max * width) / height;
        return new Dimension(w, h);
    }
}

Code to get the BufferedImage from Image object:

http://www.exampledepot.com/egs/java.awt.image/Image2Buf.html

// This method returns a buffered image with the contents of an image
public static BufferedImage toBufferedImage(Image image) {
    if (image instanceof BufferedImage) {
        return (BufferedImage)image;
    }

    // This code ensures that all the pixels in the image are loaded
    image = new ImageIcon(image).getImage();

    // Determine if the image has transparent pixels; for this method's
    // implementation, see Determining If an Image Has Transparent Pixels
    boolean hasAlpha = hasAlpha(image);

    // Create a buffered image with a format that's compatible with the screen
    BufferedImage bimage = null;
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    try {
        // Determine the type of transparency of the new buffered image
        int transparency = Transparency.OPAQUE;
        if (hasAlpha) {
            transparency = Transparency.BITMASK;
        }

        // Create the buffered image
        GraphicsDevice gs = ge.getDefaultScreenDevice();
        GraphicsConfiguration gc = gs.getDefaultConfiguration();
        bimage = gc.createCompatibleImage(
            image.getWidth(null), image.getHeight(null), transparency);
    } catch (HeadlessException e) {
        // The system does not have a screen
    }

    if (bimage == null) {
        // Create a buffered image using the default color model
        int type = BufferedImage.TYPE_INT_RGB;
        if (hasAlpha) {
            type = BufferedImage.TYPE_INT_ARGB;
        }
        bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
    }

    // Copy image to buffered image
    Graphics g = bimage.createGraphics();

    // Paint the image onto the buffered image
    g.drawImage(image, 0, 0, null);
    g.dispose();

    return bimage;
}

Finally, code to resize:

BufferedImage img = ImageIO.read(
                    new File("/home/subwiz/17042010033.jpg"));

int height = img.getHeight();
int width = img.getWidth();

Dimension d = getDimension(200, width, height);
Image outImg = img.getScaledInstance(d.width,
                                     d.height,
                                     Image.SCALE_DEFAULT);
ImageIO.write(toBufferedImage(outImg),
              "png",
              new File("/home/subwiz/out.png"));
Posted on May 19, 2010 07:46 PM by Subhash Chandran
java
blog comments powered by Disqus