Example usage for java.awt Image SCALE_AREA_AVERAGING

List of usage examples for java.awt Image SCALE_AREA_AVERAGING

Introduction

In this page you can find the example usage for java.awt Image SCALE_AREA_AVERAGING.

Prototype

int SCALE_AREA_AVERAGING

To view the source code for java.awt Image SCALE_AREA_AVERAGING.

Click Source Link

Document

Use the Area Averaging image scaling algorithm.

Usage

From source file:Main.java

public static void main(String[] args) {
    String imageFile = "A.jpg";
    RepaintManager.currentManager(null).setDoubleBufferingEnabled(false);
    Image image = Toolkit.getDefaultToolkit().getImage(Main.class.getResource(imageFile));
    image = image.getScaledInstance(imageWidth, imageHeight, Image.SCALE_AREA_AVERAGING);
    JFrame frame = new JFrame("DragImage");
    frame.getContentPane().add(new Main(image));
    frame.setSize(300, 300);//from w  w w.java  2 s.c o m
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

From source file:ImageUtil.java

/**
 * get image thumbnail/*from  w  w w  .jav a2  s  .c  o  m*/
 * @param imageFile
 */
public static ImageIcon getImageThumbnail(File imageFile, int width, int height) {
    ImageIcon thumbnail = null;
    if (imageFile != null && imageFile.isFile()) {
        ImageIcon tmpIcon = new ImageIcon(imageFile.getPath());
        if (tmpIcon != null) {
            if (tmpIcon.getIconWidth() > width) {
                int targetHeight = width / tmpIcon.getIconWidth() * tmpIcon.getIconHeight();
                if (targetHeight > height) {
                    targetHeight = height;
                } else {
                    targetHeight = -1;
                }
                thumbnail = new ImageIcon(
                        tmpIcon.getImage().getScaledInstance(width, targetHeight, Image.SCALE_AREA_AVERAGING));
            } else {
                thumbnail = tmpIcon;
            }
        }
    }
    return thumbnail;
}

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 {//  ww w.j ava2 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  ww  .j a v  a2s.  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);
    }

}

From source file:Interfaz.adminZone.java

private void ponerImagen() {

    Toolkit tk = Toolkit.getDefaultToolkit();
    String ruta = "./_data/Bicicol.png";
    Image imagen = tk.createImage(ruta);
    logo.setIcon(new ImageIcon(
            imagen.getScaledInstance(logo.getWidth(), logo.getHeight(), Image.SCALE_AREA_AVERAGING)));

}

From source file:org.jamwiki.utils.ImageUtil.java

/**
 * Resize an image, using a maximum dimension value.  Image dimensions will
 * be constrained so that the proportions are the same, but neither the width
 * or height exceeds the value specified.
 *///from ww  w.  jav a2  s. c  o  m
private static BufferedImage resizeImage(WikiImage wikiImage, int maxDimension) throws Exception {
    File imageFile = new File(Environment.getValue(Environment.PROP_FILE_DIR_FULL_PATH), wikiImage.getUrl());
    BufferedImage original = ImageUtil.loadImage(imageFile);
    maxDimension = calculateImageIncrement(maxDimension);
    int increment = Environment.getIntValue(Environment.PROP_IMAGE_RESIZE_INCREMENT);
    if (increment <= 0 || (maxDimension > original.getWidth() && maxDimension > original.getHeight())) {
        // let the browser scale the image
        return original;
    }
    String newUrl = wikiImage.getUrl();
    int pos = newUrl.lastIndexOf('.');
    if (pos > -1) {
        newUrl = newUrl.substring(0, pos) + "-" + maxDimension + "px" + newUrl.substring(pos);
    } else {
        newUrl += "-" + maxDimension + "px";
    }
    wikiImage.setUrl(newUrl);
    File newImageFile = new File(Environment.getValue(Environment.PROP_FILE_DIR_FULL_PATH), newUrl);
    if (newImageFile.exists()) {
        return ImageUtil.loadImage(newImageFile);
    }
    int width = -1;
    int height = -1;
    if (original.getWidth() >= original.getHeight()) {
        width = maxDimension;
    } else {
        height = maxDimension;
    }
    Image resized = null;
    try {
        resized = original.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING);
    } catch (Throwable t) {
        logger.severe(
                "Unable to resize image.  This problem sometimes occurs due to dependencies between Java and X on UNIX systems.  Consider enabling an X server or setting the java.awt.headless parameter to true for your JVM.",
                t);
        resized = original;
    }
    BufferedImage bufferedImage = null;
    if (resized instanceof BufferedImage) {
        bufferedImage = (BufferedImage) resized;
    } else {
        bufferedImage = ImageUtil.imageToBufferedImage(resized);
    }
    ImageUtil.saveImage(bufferedImage, newImageFile);
    return bufferedImage;
}

From source file:ScaleTest_2008.java

/**
 * This approach uses either the getScaledInstance() approach to get
 * each new size or it scales on the fly using drawImage().
 *//*from ww  w  .  j ava2s.c o m*/
private void drawImage(Graphics g, int yLoc, boolean getScaled) {
    int xLoc = 100;
    int delta = (int) (SCALE_FACTOR * FULL_SIZE);
    if (getScaled) {
        for (int scaledSize = FULL_SIZE; scaledSize > 0; scaledSize -= delta) {
            Image scaledImage = originalImage.getScaledInstance(scaledSize, scaledSize,
                    Image.SCALE_AREA_AVERAGING);
            g.drawImage(scaledImage, xLoc, yLoc + (FULL_SIZE - scaledSize) / 2, null);
            xLoc += scaledSize + 20;
        }
    } else {
        for (int scaledSize = FULL_SIZE; scaledSize > 0; scaledSize -= delta) {
            g.drawImage(originalImage, xLoc, yLoc + (FULL_SIZE - scaledSize) / 2, scaledSize, scaledSize, null);
            xLoc += scaledSize + 20;
        }
    }
}

From source file:jp.canetrash.maven.plugin.bijint.BujintMojo.java

/**
 * ??????????//from w  w  w  .  j  a  v  a 2s.  c  o  m
 * 
 * @param url
 * @return
 * @throws Exception
 */
BufferedImage getFittingImage(String url) throws Exception {
    // ??
    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = buildDefaultHttpMessage(new HttpGet(url));
    httpget.setHeader("Referer", "http://www.bijint.com/jp/");
    HttpResponse response = httpclient.execute(httpget);

    BufferedImage image = ImageIO.read(response.getEntity().getContent());
    httpclient.getConnectionManager().shutdown();

    int width = image.getWidth() / 10 * 4;
    int height = image.getHeight() / 10 * 4;
    BufferedImage resizedImage = null;
    resizedImage = new BufferedImage(width, height, image.getType());
    resizedImage.getGraphics().drawImage(image.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING), 0,
            0, width, height, null);
    return resizedImage;
}

From source file:PictureScaler.java

/**
 * Render all scaled versions 10 times, timing each version and 
 * reporting the results below the appropriate scaled image.
 *///from w  w w  . j  ava 2 s. com
protected void paintComponent(Graphics g) {
    // Scale with NEAREST_NEIGHBOR
    int xLoc = PADDING, yLoc = PADDING;
    long startTime, endTime;
    float totalTime;
    int iterations = 10;
    ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_INTERPOLATION,
            RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
    startTime = System.nanoTime();
    for (int i = 0; i < iterations; ++i) {
        g.drawImage(picture, xLoc, yLoc, scaleW, scaleH, null);
    }
    endTime = System.nanoTime();
    totalTime = (float) ((endTime - startTime) / 1000000) / iterations;
    g.drawString("NEAREST ", xLoc, yLoc + scaleH + PADDING);
    g.drawString(Float.toString(totalTime) + " ms", xLoc, yLoc + scaleH + PADDING + 10);
    System.out.println("NEAREST: " + ((endTime - startTime) / 1000000));

    // Scale with BILINEAR
    xLoc += scaleW + PADDING;
    ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_INTERPOLATION,
            RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    startTime = System.nanoTime();
    for (int i = 0; i < iterations; ++i) {
        g.drawImage(picture, xLoc, yLoc, scaleW, scaleH, null);
    }
    endTime = System.nanoTime();
    totalTime = (float) ((endTime - startTime) / 1000000) / iterations;
    g.drawString("BILINEAR", xLoc, yLoc + scaleH + PADDING);
    g.drawString(Float.toString(totalTime) + " ms", xLoc, yLoc + scaleH + PADDING + 10);
    System.out.println("BILINEAR: " + ((endTime - startTime) / 1000000));

    // Scale with BICUBIC
    xLoc += scaleW + PADDING;
    ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_INTERPOLATION,
            RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    startTime = System.nanoTime();
    for (int i = 0; i < iterations; ++i) {
        g.drawImage(picture, xLoc, yLoc, scaleW, scaleH, null);
    }
    endTime = System.nanoTime();
    totalTime = (float) ((endTime - startTime) / 1000000) / iterations;
    g.drawString("BICUBIC", xLoc, yLoc + scaleH + PADDING);
    g.drawString(Float.toString(totalTime) + " ms", xLoc, yLoc + scaleH + PADDING + 10);
    System.out.println("BICUBIC: " + ((endTime - startTime) / 1000000));

    // Scale with getScaledInstance
    xLoc += scaleW + PADDING;
    startTime = System.nanoTime();
    for (int i = 0; i < iterations; ++i) {
        Image scaledPicture = picture.getScaledInstance(scaleW, scaleH, Image.SCALE_AREA_AVERAGING);
        g.drawImage(scaledPicture, xLoc, yLoc, null);
    }
    endTime = System.nanoTime();
    totalTime = (float) ((endTime - startTime) / 1000000) / iterations;
    g.drawString("getScaled", xLoc, yLoc + scaleH + PADDING);
    g.drawString(Float.toString(totalTime) + " ms", xLoc, yLoc + scaleH + PADDING + 10);
    System.out.println("getScaled: " + ((endTime - startTime) / 1000000));

    // Scale with Progressive Bilinear
    xLoc += scaleW + PADDING;
    startTime = System.nanoTime();
    for (int i = 0; i < iterations; ++i) {
        Image scaledPicture = getFasterScaledInstance(picture, scaleW, scaleH,
                RenderingHints.VALUE_INTERPOLATION_BILINEAR, true);
        g.drawImage(scaledPicture, xLoc, yLoc, null);
    }
    endTime = System.nanoTime();
    totalTime = (float) ((endTime - startTime) / 1000000) / iterations;
    g.drawString("Progressive", xLoc, yLoc + scaleH + PADDING);
    g.drawString(Float.toString(totalTime) + " ms", xLoc, yLoc + scaleH + PADDING + 10);
    System.out.println("Progressive: " + ((endTime - startTime) / 1000000));
}

From source file:ucar.unidata.idv.control.chart.ChartManager.java

/**
 * actually update the thumbnail image/*from   w  ww .j  av a  2 s  .com*/
 */
public void updateThumbInner() {
    try {
        if (!showThumb) {
            return;
        }
        //            if (settingData) return;
        if ((getContents().getWidth() == 0) || (getContents().getHeight() == 0)) {
            //                Misc.runInABit(1000, this, "updateThumb",null);
            return;
        }
        List images = new ArrayList();
        Image thumb = ImageUtils.getImage(getContents());
        if (thumb == null) {
            return;
        }
        if ((thumb.getWidth(null) <= 0) || (thumb.getHeight(null) <= 0)) {
            return;
        }
        double ratio = thumb.getWidth(null) / (double) thumb.getHeight(null);
        //            int width = Math.max(label.getWidth(),200);
        int width = 200;
        int height = (int) (width / ratio);
        thumb = ImageUtils.toBufferedImage(thumb.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING),
                BufferedImage.TYPE_INT_RGB);
        boolean chartsShowingSomething = false;
        for (ChartHolder chartHolder : chartHolders) {
            if (chartHolder.getBeingShown() && chartHolder.hasParameters()) {
                chartsShowingSomething = true;
                break;
            }
        }
        if (chartsShowingSomething) {
            getThumb().setIcon(new ImageIcon(thumb));
        } else {
            getThumb().setIcon(GuiUtils.getImageIcon("/auxdata/ui/icons/OnePixel.gif"));
        }

    } catch (Exception exc) {
        //            LogUtil.logException("Showing thumbnail", exc);
    }
    thumbUpdatePending = false;
}