Java Utililty Methods Image Scale

List of utility methods to do Image Scale

Description

The list of methods to do Image Scale are organized into topic(s).

Method

ImageIcongetScaledIcon(final Image image, final double scale)
get Scaled Icon
ImageIcon scaledIcon = new ImageIcon(image) {
    private static final long serialVersionUID = 1L;
    public int getIconWidth() {
        return (int) (image.getWidth(null) * scale);
    public int getIconHeight() {
        return (int) (image.getHeight(null) * scale);
    public void paintIcon(Component c, Graphics g, int x, int y) {
        g.drawImage(image, x, y, getIconWidth(), getIconHeight(), c);
};
return scaledIcon;
ImagegetScaledImage(final ImageIcon icon, final int newMaxWidth, final int newMaxHeight, final boolean maintainRatio)
Gets a scaled icon and if it doesn't exist it creates one and scales it
if (icon != null) {
    int dstWidth = newMaxWidth;
    int dstHeight = newMaxHeight;
    int srcWidth = icon.getIconWidth();
    int srcHeight = icon.getIconHeight();
    if ((dstWidth < 0) || (dstHeight < 0)) { 
        dstWidth = icon.getIconWidth();
        dstHeight = icon.getIconHeight();
...
java.awt.ImagegetScaledImage(Image image, int maxWidth, int maxHeight)
get Scaled Image
image = new ImageIcon(image).getImage();
final int imgWidth = image.getWidth(null);
final int imgHeight = image.getHeight(null);
final int preferredWidth = Math.min(imgWidth, maxWidth);
final int preferredHeight = Math.min(imgHeight, maxHeight);
final double scaleX = (double) preferredWidth / (double) imgWidth;
final double scaleY = (double) preferredHeight / (double) imgHeight;
final double bestScale = Math.min(scaleX, scaleY);
...
BufferedImagegetScaledImage(Image imagen, double scaleW, double scaleH)
Obtiene una imagen escalada
BufferedImage bi = toBufferedImage(imagen);
int w = (int) (bi.getWidth() * scaleW);
int h = (int) (bi.getHeight() * scaleH);
bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = bi.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,
        RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
AffineTransform at = AffineTransform.getScaleInstance(scaleW, scaleH);
...
ImageIcongetScaledImage(Image srcImg, int w, int h)
Resizes an image using a Graphics2D object backed by a BufferedImage.
BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = resizedImg.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(srcImg, 0, 0, w, h, null);
g2.dispose();
return new ImageIcon(resizedImg);
ImagegetScaledImage(ImageIcon icon, int w, int h)
get Scaled Image
BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = resizedImg.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(icon.getImage(), 0, 0, w, h, null);
g2.dispose();
return resizedImg;
ImageIcongetScaledImage(ImageIcon srcImg, int h, int maxWidth)
Method used to resize images to fit the cover into the label
int w = srcImg.getIconWidth() * h / srcImg.getIconHeight();
if (w > maxWidth) {
    w = maxWidth;
    h = srcImg.getIconHeight() * w / srcImg.getIconWidth();
if (w > srcImg.getIconWidth() || h > srcImg.getIconHeight()) {
    w = srcImg.getIconWidth();
    h = srcImg.getIconHeight();
...
doublegetScaleToFit(JPanel componentToFit, File imageFile)
get Scale To Fit
BufferedImage bimg = ImageIO.read(imageFile);
int width = bimg.getWidth();
int height = bimg.getHeight();
double scalex = componentToFit.getVisibleRect().getWidth() / (double) width;
double scaley = componentToFit.getVisibleRect().getHeight() / (double) height;
return (scalex < scaley) ? scalex : scaley;
ImagescaleImage(Image image, float scale)
scale Image
if (image == null)
    return null;
BufferedImage sourceImage = toBufferedImage(image);
int scaledWidth = (int) (sourceImage.getWidth() * scale);
if (scaledWidth <= 0)
    return null;
int scaledHeight = (int) (sourceImage.getHeight() * scale);
if (scaledHeight <= 0)
...
ImagescaleImage(Image image, float widthPercentage, float heightPercentage)
scale Image
Dimension imageSize = new Dimension(image.getWidth(IMAGE_OBSERVER), image.getHeight(IMAGE_OBSERVER));
imageSize.width *= widthPercentage;
imageSize.height *= heightPercentage;
Image scaledImage = image.getScaledInstance(imageSize.width, imageSize.height, SCALE_METHOD);
return scaledImage;