Java Image createScaledImage(Image inImage, int inWidth, int inHeight)

Here you can find the source of createScaledImage(Image inImage, int inWidth, int inHeight)

Description

Create a scaled and smoothed image according to the specified size

License

Open Source License

Parameter

Parameter Description
inImage image to scale
inWidth width to scale to
inHeight height to scale to

Return

BufferedImage containing scaled result

Declaration

public static BufferedImage createScaledImage(Image inImage, int inWidth, int inHeight) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.awt.Graphics;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.ConvolveOp;

import javax.swing.ImageIcon;

public class Main {
    private static ConvolveOp CONVOLVER = null;

    /**//from  www  . ja va 2  s . com
     * Create a scaled and smoothed image according to the specified size
     * @param inImage image to scale
     * @param inWidth width to scale to
     * @param inHeight height to scale to
     * @return BufferedImage containing scaled result
     */
    public static BufferedImage createScaledImage(Image inImage, int inWidth, int inHeight) {
        // create smaller image and force its loading
        Image smallerImage = inImage.getScaledInstance(inWidth, inHeight, Image.SCALE_SMOOTH);
        Image tempImage = new ImageIcon(smallerImage).getImage();
        tempImage.getWidth(null);

        // create buffered image to do transform
        BufferedImage buffer = new BufferedImage(inWidth, inHeight, BufferedImage.TYPE_INT_RGB);
        // copy scaled picture into buffer
        Graphics buffG = buffer.getGraphics();
        buffG.drawImage(smallerImage, 0, 0, inWidth, inHeight, null);
        buffG.dispose();

        // clear variables
        smallerImage = null;
        tempImage = null;
        // smooth scaled image using a normalized 3x3 matrix - taking next neighbour
        buffer = CONVOLVER.filter(buffer, null);

        return buffer;
    }
}

Related

  1. CreateCopy(Image srcImg)
  2. createCursor(Image cursorImage, Point hotSpot, String name)
  3. createCursor(Image image, Point hotspot, String name)
  4. createMemoryImage(int width, int height, Color colour, JComponent someComponent)
  5. createQualityResizedImage( Image orginalImage, int width, int height, boolean keepRatio)
  6. createShadowPicture(Image buf)
  7. createTiledImage(Image img, int width, int height)
  8. crop(Image source, int x1, int y1, int x2, int y2)
  9. displayImage(Image image)