Example usage for java.awt.image BufferedImage getScaledInstance

List of usage examples for java.awt.image BufferedImage getScaledInstance

Introduction

In this page you can find the example usage for java.awt.image BufferedImage getScaledInstance.

Prototype

public Image getScaledInstance(int width, int height, int hints) 

Source Link

Document

Creates a scaled version of this image.

Usage

From source file:Main.java

/**
 * Smoothly scales the given {@link BufferedImage} to the given width and height using the
 * {@link Image#SCALE_SMOOTH} algorithm (generally bicubic resampling or bilinear filtering).
 *
 * @param source The source image./*  w w  w.j a  v a 2s.  c  o m*/
 * @param width  The destination width to scale to.
 * @param height The destination height to scale to.
 * @return A new, scaled image.
 */
public static BufferedImage scaledImage(BufferedImage source, int width, int height) {
    Image scaledImage = source.getScaledInstance(width, height, Image.SCALE_SMOOTH);
    BufferedImage scaledBufImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics g = scaledBufImage.createGraphics();
    g.drawImage(scaledImage, 0, 0, null);
    g.dispose();
    return scaledBufImage;
}

From source file:ImageUtil.java

/**
 * Creates a scaled copy of the source image.
 * //www . j a  v  a  2s.  c o  m
 * @param src
 *            source image to be scaled
 * @param width
 *            the width for the new scaled image in pixels
 * @param height
 *            the height for the new scaled image in pixels
 * @return a copy of the source image scaled to <tt>width</tt> x
 *         <tt>height</tt> pixels.
 */
public static BufferedImage scaleImage(BufferedImage src, int width, int height) {
    Image scaled = src.getScaledInstance(width, height, 0);
    BufferedImage ret = null;
    /*
     * ColorModel cm = src.getColorModel(); if (cm instanceof
     * IndexColorModel) { ret = new BufferedImage( width, height,
     * src.getType(), (IndexColorModel) cm ); } else { ret = new
     * BufferedImage( src.getWidth(), src.getHeight(), src.getType() ); }
     * Graphics2D g = ret.createGraphics(); //clear alpha channel Composite
     * comp = g.getComposite();
     * g.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR,
     * 0.0f)); Rectangle2D.Double d = new
     * Rectangle2D.Double(0,0,ret.getWidth(),ret.getHeight()); g.fill(d);
     * g.setComposite(comp);
     */
    ret = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = ret.createGraphics();
    // copy image
    g.drawImage(scaled, 0, 0, null);
    return ret;
}

From source file:com.taunova.app.libview.components.ImageHelpers.java

public static Image getScaledImage(BufferedImage image, int width) {
    Dimension d = getScaledDimension(image, width);
    Image scaledImage = image.getScaledInstance(d.width, d.height, Image.SCALE_SMOOTH);

    BufferedImage resizedImage = null;

    final boolean OPTIMIZE_SCALE = true;

    if (OPTIMIZE_SCALE) {
        ResampleOp resampleOp = new ResampleOp(100, 200);
        resampleOp.setUnsharpenMask(AdvancedResizeOp.UnsharpenMask.Normal);
        resizedImage = resampleOp.filter(image, null);
    } else {/*from ww  w. j  a  v a2s  .  com*/
        resizedImage = new BufferedImage(d.width, d.height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = resizedImage.createGraphics();
        g.setComposite(AlphaComposite.Src);
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.drawImage(scaledImage, 0, 0, d.width, d.height, null);
        g.dispose();
    }

    return resizedImage;
}

From source file:com.spstudio.common.image.ImageUtils.java

public static BufferedImage zoom(BufferedImage sourceImage, int width, int height) {
    BufferedImage zoomImage = new BufferedImage(width, height, sourceImage.getType());
    Image image = sourceImage.getScaledInstance(width, height, Image.SCALE_SMOOTH);
    Graphics gc = zoomImage.getGraphics();
    gc.setColor(Color.WHITE);/*from   w w  w . j  av a 2 s .co m*/
    gc.drawImage(image, 0, 0, null);
    return zoomImage;
}

From source file:com.shending.support.CompressPic.java

/**
 * ?//from   w  w w .j a va 2 s.  c  o m
 *
 * @param srcFile
 * @param dstFile
 * @param widthRange
 * @param heightRange
 */
public static void cutSquare(String srcFile, String dstFile, int widthRange, int heightRange, int width,
        int height) {
    int x = 0;
    int y = 0;
    try {
        ImageInputStream iis = ImageIO.createImageInputStream(new File(srcFile));
        Iterator<ImageReader> iterator = ImageIO.getImageReaders(iis);
        ImageReader reader = (ImageReader) iterator.next();
        reader.setInput(iis, true);
        ImageReadParam param = reader.getDefaultReadParam();
        int oldWidth = reader.getWidth(0);
        int oldHeight = reader.getHeight(0);
        int newWidth, newHeight;
        if (width <= oldWidth && height <= oldHeight) {
            newWidth = oldHeight * widthRange / heightRange;
            if (newWidth < oldWidth) {
                newHeight = oldHeight;
                x = (oldWidth - newWidth) / 2;
            } else {
                newWidth = oldWidth;
                newHeight = oldWidth * heightRange / widthRange;
                y = (oldHeight - newHeight) / 2;
            }
            Rectangle rectangle = new Rectangle(x, y, newWidth, newHeight);
            param.setSourceRegion(rectangle);
            BufferedImage bi = reader.read(0, param);
            BufferedImage tag = new BufferedImage((int) width, (int) height, BufferedImage.TYPE_INT_RGB);
            tag.getGraphics().drawImage(bi.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
            File file = new File(dstFile);
            ImageIO.write(tag, reader.getFormatName(), file);
        } else {
            BufferedImage bi = reader.read(0, param);
            BufferedImage tag = new BufferedImage((int) width, (int) height, BufferedImage.TYPE_INT_RGB);
            Graphics2D g2d = tag.createGraphics();
            g2d.setColor(Color.WHITE);
            g2d.fillRect(0, 0, tag.getWidth(), tag.getHeight());
            g2d.drawImage(bi.getScaledInstance(bi.getWidth(), bi.getHeight(), Image.SCALE_SMOOTH),
                    (width - bi.getWidth()) / 2, (height - bi.getHeight()) / 2, null);
            g2d.dispose();
            File file = new File(dstFile);
            ImageIO.write(tag, reader.getFormatName(), file);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.smash.revolance.ui.model.helper.ImageHelper.java

public static BufferedImage scaleImage(BufferedImage image, double widthPerCent, double heightPerCent) {
    int w = (int) (image.getWidth() * widthPerCent);
    int h = (int) (image.getHeight() * heightPerCent);
    int t = image.getType();

    Image scaledImage = image.getScaledInstance(w, h, Image.SCALE_SMOOTH);

    BufferedImage newImg = new BufferedImage(w, h, t);
    newImg.getGraphics().drawImage(scaledImage, 0, 0, null);

    return newImg;
}

From source file:preprocessing.Utils.java

public static BufferedImage resize(BufferedImage img, int newW, int newH) {
    Image tmp = img.getScaledInstance(newW, newH, Image.SCALE_SMOOTH);
    BufferedImage dimg = new BufferedImage(newW, newH, BufferedImage.TYPE_INT_RGB);

    Graphics2D g2d = dimg.createGraphics();
    g2d.drawImage(tmp, 0, 0, null);//from   w w w  . j av  a2  s  .co m
    g2d.dispose();

    return dimg;
}

From source file:net.duckling.ddl.util.ImageUtils.java

/**
 * ???/*  w w  w . j  a  v  a 2s  .  co m*/
 * @param tmpFilePath ?
 * @return
 */
public static boolean scare(String tmpFilePath) {
    try {
        BufferedImage src = ImageIO.read(new File(tmpFilePath)); // 
        int width = src.getWidth();
        int height = src.getHeight();
        if (width > DEFAULT_WIDTH) {
            height = (DEFAULT_WIDTH * height) / width;
            width = DEFAULT_WIDTH;
        }
        Image image = src.getScaledInstance(width, height, Image.SCALE_DEFAULT);
        BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics g = tag.getGraphics();
        g.drawImage(image, 0, 0, null); // ??
        g.dispose();
        File resultFile = new File(tmpFilePath);
        ImageIO.write(tag, "JPEG", resultFile);// ?
        return true;
    } catch (IOException e) {
        LOG.error(e);
    }
    return false;
}

From source file:com.l1j5.web.common.utils.ImageUtils.java

public static void getImageThumbnail(BufferedInputStream stream_file, String save, String type, int w, int h) {

    try {//from w  w  w .  j  a  va2  s .c  o  m
        File file = new File(save);
        BufferedImage bi = ImageIO.read(stream_file);

        int width = bi.getWidth();
        int height = bi.getHeight();
        if (w < width) {
            width = w;
        }
        if (h < height) {
            height = h;
        }

        BufferedImage bufferIm = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Image atemp = bi.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING);

        Graphics2D g2 = bufferIm.createGraphics();
        g2.drawImage(atemp, 0, 0, width, height, null);
        ImageIO.write(bufferIm, type, file);
    } catch (Exception e) {
        log.error(e);
    }

}

From source file:com.l1j5.web.common.utils.ImageUtils.java

public static void getImageThumbnail(BufferedInputStream stream_file, String save, String type, int w) {

    try {/*from   w w w .  ja v a  2 s  . c  o m*/
        File file = new File(save);
        BufferedImage bi = ImageIO.read(stream_file);

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

        double ratio = (double) height / width;
        height = (int) (w * ratio);
        if (w < width) {
            width = w;
        }

        BufferedImage bufferIm = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Image atemp = bi.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING);

        Graphics2D g2 = bufferIm.createGraphics();
        g2.drawImage(atemp, 0, 0, width, height, null);
        ImageIO.write(bufferIm, type, file);
    } catch (Exception e) {
        log.error(e);
    }

}