Java Image GetImagenConTamanioDado(File file, int ancho, int alto)

Here you can find the source of GetImagenConTamanioDado(File file, int ancho, int alto)

Description

Get Imagen Con Tamanio Dado

License

Open Source License

Declaration

public static BufferedImage GetImagenConTamanioDado(File file, int ancho, int alto) 

Method Source Code

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

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;

import java.io.File;

import javax.swing.ImageIcon;

public class Main {

    public static BufferedImage GetImagenConTamanioDado(File file, int ancho, int alto) {
        BufferedImage bi = null;/*w  w  w .  ja v  a2  s.co  m*/
        if (file.exists()) {
            String nombreFile = file.getName();
            String extension = nombreFile.substring(nombreFile.indexOf("."));

            if ((extension.endsWith("jpg") || extension.endsWith("JPG"))
                    || (extension.endsWith("png") || extension.endsWith("PNG"))
                    || (extension.endsWith("bmp") || extension.endsWith("BMP"))) {
                Image img = new ImageIcon(file.getAbsolutePath()).getImage();
                int anchoOriginal = img.getWidth(null);
                int altoOriginal = img.getHeight(null);

                double anchoResultado = 1f * ancho / anchoOriginal;
                double altoResultado = 1f * alto / altoOriginal;

                int tipo = extension.compareToIgnoreCase(".jpg") == 0 || extension.compareToIgnoreCase(".bmp") == 0
                        ? BufferedImage.TYPE_INT_RGB
                        : BufferedImage.TYPE_INT_ARGB;

                bi = new BufferedImage((int) (anchoOriginal * anchoResultado), (int) (altoOriginal * altoResultado),
                        tipo);

                Graphics2D grph = (Graphics2D) bi.getGraphics();
                grph.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

                grph.scale(anchoResultado, altoResultado);
                grph.drawImage(img, 0, 0, null);
                grph.dispose();
            }
        }

        return bi;

    }
}

Related

  1. getDisabledImage(Image image)
  2. getEingabeImage(int x, int y)
  3. getHeight(Image imagen)
  4. getImageByFilename(String filename)
  5. getImageImmediate(final Image image)
  6. getImageWithBorder(Image imagen)
  7. getInputStreamFromImage(Image imagen, String format)
  8. getLogoImage()
  9. getManagedImage(Component source, String file, float tint, Color solid)