Java Image Scale getScaledImage(Image imagen, double scaleW, double scaleH)

Here you can find the source of getScaledImage(Image imagen, double scaleW, double scaleH)

Description

Obtiene una imagen escalada

License

Open Source License

Declaration

public static BufferedImage getScaledImage(Image imagen, double scaleW, double scaleH) 

Method Source Code


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

import java.awt.*;

import java.awt.geom.AffineTransform;
import java.awt.image.*;

import javax.swing.*;

public class Main {
    /**//from  ww w.  ja  va  2 s. com
     * Obtiene una imagen escalada
     */
    public static BufferedImage getScaledImage(Image imagen, double scaleW, double scaleH) {

        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);
        g2.drawRenderedImage(toBufferedImage(imagen), at);
        g2.dispose();
        return bi;
    }

    /**
     * Convierte un objeto Image a un BufferedImage
     *
     * @param img El objeto Image a convertir
     * @return El BufferedImage obtenido
     */
    public static BufferedImage toBufferedImage(Image img) {
        if (img instanceof BufferedImage) {
            return (BufferedImage) img;
        }
        // Crea un buffered image con trasparencia
        BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null),
                BufferedImage.TYPE_INT_ARGB);
        // Dibuja la imagen en el buffered image
        Graphics2D bGr = bimage.createGraphics();
        bGr.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        bGr.drawImage(img, 0, 0, null);
        bGr.dispose();
        // Retorna el buffered image
        return bimage;
    }

    /**
     * Obtiene el ancho de una Image
     */
    public static int getWidth(Image imagen) {
        int width = new ImageIcon(imagen).getIconWidth();
        return width;
    }

    /**
     * Obtiene el ancho de un IconImage
     */
    public static int getWidth(ImageIcon imagen) {
        return imagen.getIconWidth();
    }

    /**
     * Obtiene el alto de una Image
     */
    public static int getHeight(Image imagen) {
        int height = new ImageIcon(imagen).getIconHeight();
        return height;
    }

    /**
     * Obtiene el alto de un IconImage
     */
    public static int getHeight(ImageIcon imagen) {
        return imagen.getIconHeight();
    }
}

Related

  1. getScaledIcon(final Image image, final double scale)
  2. getScaledImage(final ImageIcon icon, final int newMaxWidth, final int newMaxHeight, final boolean maintainRatio)
  3. getScaledImage(Image image, int maxWidth, int maxHeight)
  4. getScaledImage(Image srcImg, int w, int h)
  5. getScaledImage(ImageIcon icon, int w, int h)
  6. getScaledImage(ImageIcon srcImg, int h, int maxWidth)
  7. getScaleToFit(JPanel componentToFit, File imageFile)