Example usage for java.awt RenderingHints VALUE_INTERPOLATION_BILINEAR

List of usage examples for java.awt RenderingHints VALUE_INTERPOLATION_BILINEAR

Introduction

In this page you can find the example usage for java.awt RenderingHints VALUE_INTERPOLATION_BILINEAR.

Prototype

Object VALUE_INTERPOLATION_BILINEAR

To view the source code for java.awt RenderingHints VALUE_INTERPOLATION_BILINEAR.

Click Source Link

Document

Interpolation hint value -- the color samples of the 4 nearest neighboring integer coordinate samples in the image are interpolated linearly to produce a color sample.

Usage

From source file:org.apache.batchee.tools.maven.doc.DiagramGenerator.java

private void saveView(final Dimension currentSize, final Dimension desiredSize, final String name,
        final VisualizationViewer<Node, Edge> viewer) {
    BufferedImage bi = new BufferedImage(currentSize.width, currentSize.height, BufferedImage.TYPE_INT_ARGB);

    final Graphics2D g = bi.createGraphics();
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);

    final boolean db = viewer.isDoubleBuffered();
    viewer.setDoubleBuffered(false);/*from w ww. ja  v a2s  . c  o  m*/
    viewer.paint(g);
    viewer.setDoubleBuffered(db);
    if (!currentSize.equals(desiredSize)) {
        final double xFactor = desiredSize.width * 1. / currentSize.width;
        final double yFactor = desiredSize.height * 1. / currentSize.height;
        final double factor = Math.min(xFactor, yFactor);
        info("optimal size is (" + currentSize.width + ", " + currentSize.height + ")");
        info("scaling with a factor of " + factor);

        final AffineTransform tx = new AffineTransform();
        tx.scale(factor, factor);
        final AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
        BufferedImage biNew = new BufferedImage((int) (bi.getWidth() * factor), (int) (bi.getHeight() * factor),
                bi.getType());
        bi = op.filter(bi, biNew);
    }
    g.dispose();

    OutputStream os = null;
    try {
        final File file = new File(output, (outputFileName != null ? outputFileName : name) + "." + format);
        os = new FileOutputStream(file);
        if (!ImageIO.write(bi, format, os)) {
            throw new IllegalStateException("can't save picture " + name + "." + format);
        }
        info("Saved " + file.getAbsolutePath());
    } catch (final IOException e) {
        throw new IllegalStateException("can't save the diagram", e);
    } finally {
        if (os != null) {
            try {
                os.flush();
                os.close();
            } catch (final IOException e) {
                // no-op
            }
        }
    }
}

From source file:org.apache.stratos.theme.mgt.ui.processors.AddThemeResourceProcessor.java

private static DataHandler scaleImage(DataHandler dataHandler, int height, int width) throws IOException {

    Image image = ImageIO.read(new BufferedInputStream(dataHandler.getInputStream()));
    // Check if the image has transparent pixels
    boolean hasAlpha = ((BufferedImage) image).getColorModel().hasAlpha();

    // Maintain Aspect ratio
    int thumbHeight = height;
    int thumbWidth = width;
    double thumbRatio = (double) width / (double) height;
    double imageRatio = (double) image.getWidth(null) / (double) image.getHeight(null);
    if (thumbRatio < imageRatio) {
        thumbHeight = (int) (thumbWidth / imageRatio);
    } else {//from w w  w.  jav  a2  s. c  om
        thumbWidth = (int) (thumbHeight * imageRatio);
    }

    BufferedImage thumb;
    // Check if transparent pixels are available and set the color mode accordingly 
    if (hasAlpha) {
        thumb = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_ARGB);
    } else {
        thumb = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
    }
    Graphics2D graphics2D = thumb.createGraphics();
    graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);

    // Save the image as PNG so that transparent images are rendered as intended
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    ImageIO.write(thumb, "PNG", output);

    DataSource dataSource = new ByteArrayDataSource(output.toByteArray(), "application/octet-stream");
    return new DataHandler(dataSource);
}

From source file:org.elasticwarehouse.core.parsers.FileTools.java

private static BufferedImage resizeImageWithHint(BufferedImage originalImage, int type, int IMG_WIDTH,
        int IMG_HEIGHT) {

    BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type);
    Graphics2D g = resizedImage.createGraphics();
    g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
    g.dispose();//from w ww.j a v  a2  s  . c om
    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);

    return resizedImage;
}

From source file:org.forumj.web.servlet.post.SetAvatar.java

private BufferedImage renderImage(ImageSize destSize, int imgType, Image image) {
    BufferedImage thumbsImage = new BufferedImage(destSize.getWidth(), destSize.getHeight(), imgType);
    Graphics2D graphics2D = thumbsImage.createGraphics();
    graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    graphics2D.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    graphics2D.drawImage(image, 0, 0, destSize.getWidth(), destSize.getHeight(), null);
    return thumbsImage;
}

From source file:org.jas.util.ImageUtils.java

public Image resize(Image image, int width, int height) {
    BufferedImage bufferedImage = (BufferedImage) image;
    int type = bufferedImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : bufferedImage.getType();
    BufferedImage resizedImage = new BufferedImage(width, height, type);
    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(image, 0, 0, width, height, null);
    g.dispose();//  w w w  . j  a va2s  .  c  o m
    return resizedImage;
}

From source file:org.lnicholls.galleon.util.Tools.java

public static Image getImage(URL url, int width, int height) {
    if (url != null) {
        // System.out.println(url);
        try {//from  w  ww.ja v a 2 s  .c o  m
            Image internetImage = null;
            if (log.isDebugEnabled())
                log.debug("Downloading internet image=" + url.toExternalForm());

            class TimedThread implements Callable {
                private URL mUrl;

                public TimedThread(URL url) {
                    mUrl = url;
                }

                public synchronized java.lang.Object call() throws java.lang.Exception {
                    return new ImageTracker(mUrl).load();
                }
            }

            TimedCallable timedCallable = new TimedCallable(new TimedThread(url), 2000 * 60);
            internetImage = (Image) timedCallable.call();

            // System.out.println("internetImage="+internetImage);
            if (internetImage == null) {
                log.error("Invalid internet image: " + url.getPath());
            } else {
                // System.out.println("width="+width);
                // System.out.println("height="+height);
                if (width == -1)
                    width = internetImage.getWidth(null);
                if (height == -1)
                    height = internetImage.getHeight(null);

                // System.out.println("width="+width);
                // System.out.println("height="+height);

                Image image = null;
                if (internetImage instanceof BufferedImage) {
                    image = ImageManipulator.getScaledImage((BufferedImage) internetImage, width, height);
                    // System.out.println("image1="+image);
                } else {
                    try {
                        image = createBufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
                        Graphics2D graphics2D = ((BufferedImage) image).createGraphics();
                        graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                                RenderingHints.VALUE_INTERPOLATION_BILINEAR);
                        graphics2D.drawImage(internetImage, 0, 0, width, height, null);
                        graphics2D.dispose();
                        graphics2D = null;
                        // System.out.println("image2="+image);
                    } catch (Throwable ex) {
                        // ex.printStackTrace();
                        image = internetImage.getScaledInstance(width, height, Image.SCALE_SMOOTH);
                        // System.out.println("image3="+image);
                    }
                }
                internetImage.flush();
                internetImage = null;

                return image;
            }
        } catch (Throwable ex) {
            // ex.printStackTrace();
            Tools.logException(Tools.class, ex, url.toExternalForm());
        }
    }
    return null;
}

From source file:org.madsonic.controller.CoverArtController.java

public static BufferedImage videoScale(BufferedImage image, int width, int height) {

    BufferedImage thumb = image;/*w w  w  .  j  a va 2  s  .  co  m*/

    BufferedImage temp = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = temp.createGraphics();
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g2.drawImage(thumb, 0, 0, width, height, null);
    g2.dispose();
    thumb = temp;

    return thumb;
}

From source file:org.madsonic.controller.CoverArtController.java

public static BufferedImage scale(BufferedImage image, int width, int height) {

    int w = image.getWidth();
    int h = image.getHeight();
    BufferedImage thumb = image;//from  w  w w .  j a va 2  s .  co  m

    // For optimal results, use step by step bilinear resampling - halfing the size at each step.
    do {
        w /= 2;
        h /= 2;
        if (w < width) {
            w = width;
        }
        if (h < height) {
            h = height;
        }

        double thumbRatio = (double) width / (double) height;
        double aspectRatio = (double) w / (double) h;

        //            LOG.debug("## thumbsRatio: " + thumbRatio);
        //            LOG.debug("## aspectRatio: " + aspectRatio);

        if (thumbRatio < aspectRatio) {
            h = (int) (w / aspectRatio);
        } else {
            w = (int) (h * aspectRatio);
        }

        BufferedImage temp = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = temp.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2.drawImage(thumb, 0, 0, w, h, null);
        g2.dispose();

        thumb = temp;
    } while (w != width);

    //FIXME: check
    if (thumb.getHeight() > thumb.getWidth()) {
        thumb = thumb.getSubimage(0, 0, thumb.getWidth(), thumb.getWidth());
    }
    return thumb;
}

From source file:org.olat.core.commons.services.image.spi.ImageHelperImpl.java

/**
 * This code is very inspired on Chris Campbells article "The Perils of Image.getScaledInstance()"
 *
 * The article can be found here:/*from  w ww .  j av  a 2 s .  co  m*/
 * http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html
 *
 * Note that the filter method is threadsafe
 */
private static BufferedImage scaleFastTo(BufferedImage img, Size scaledSize) {
    if (!scaledSize.isChanged())
        return img;

    BufferedImage dest;
    if (img.getType() == BufferedImage.TYPE_CUSTOM) {
        dest = new BufferedImage(scaledSize.getWidth(), scaledSize.getHeight(), BufferedImage.TYPE_INT_ARGB);
    } else {
        dest = new BufferedImage(scaledSize.getWidth(), scaledSize.getHeight(), img.getType());
    }

    int dstWidth = scaledSize.getWidth();
    int dstHeight = scaledSize.getHeight();

    BufferedImage ret = img;
    int w, h;

    // Use multi-step technique: start with original size, then
    // scale down in multiple passes with drawImage()
    // until the target size is reached
    w = img.getWidth();
    h = img.getHeight();

    int x = scaledSize.getXOffset();
    int y = scaledSize.getYOffset();

    //crop the image to see only the center of the image
    if (x > 0 || y > 0) {
        ret = img.getSubimage(x, y, w - (2 * x), h - (2 * y));
    }

    do {
        if (w > dstWidth) {
            if (x > 0) {
                x /= 2;
            }
            w /= 2;
            if (w < dstWidth) {
                w = dstWidth;
            }
        } else {
            w = dstWidth;
        }

        if (h > dstHeight) {
            h /= 2;
            if (h < dstHeight) {
                h = dstHeight;
            }
        } else {
            h = dstHeight;
        }

        BufferedImage tmp;
        if (dest.getWidth() == w && dest.getHeight() == h && w == dstWidth && h == dstHeight) {
            tmp = dest;
        } else {
            tmp = new BufferedImage(w, h, dest.getType());
        }

        Graphics2D g2 = tmp.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2.drawImage(ret, 0, 0, w, h, null);
        g2.dispose();

        ret = tmp;
    } while (w != dstWidth || h != dstHeight);

    return ret;
}

From source file:org.openbravo.erpCommon.utility.Utility.java

/**
 * Resize an image giving the image input as byte[]
 * //from w  w  w  .j  a v a  2  s.c  om
 * @param bytea
 *          The contents of the image as a byte array
 * @param maxW
 *          Maximum width that the image will be resized.
 * @param maxH
 *          Maximum height that the image will be resized.
 * @param maintainAspectRatio
 *          If true, the image will be resized exactly to the maximum parameters. If false, the
 *          imagen will be resized closest to the maximum parameters keeping aspect ratio
 * @param canMakeLargerImage
 *          If true and the original image is smaller than maximum parameters, the resized image
 *          could be larger than the original one. If false, not.
 * @return The resized image
 */
public static byte[] resizeImageByte(byte[] bytea, int maxW, int maxH, boolean maintainAspectRatio,
        boolean canMakeLargerImage) throws IOException {
    ByteArrayInputStream bis = new ByteArrayInputStream(bytea);
    BufferedImage rImage = ImageIO.read(bis);
    int newW = maxW;
    int newH = maxH;
    int oldW = rImage.getWidth();
    int oldH = rImage.getHeight();
    if (newW == 0 && newH == 0) {
        return bytea;
    } else if (newW == 0) {
        if (maintainAspectRatio) {
            newW = 99999;
        } else {
            newW = oldW;
        }
    } else if (newH == 0) {
        if (maintainAspectRatio) {
            newH = 99999;
        } else {
            newH = oldH;
        }
    }
    if (oldW == newW && oldH == newH) {
        return bytea;
    }
    if (!canMakeLargerImage && newW > oldW && newH > oldH) {
        return bytea;
    }
    if (maintainAspectRatio) {
        float oldRatio = (float) oldW / (float) oldH;
        float newRatio = (float) newW / (float) newH;
        if (oldRatio < newRatio) {
            newW = (int) (newH * oldRatio);
        } else if (oldRatio > newRatio) {
            newH = (int) (newW / oldRatio);
        }
    }
    BufferedImage dimg = new BufferedImage(newW, newH, rImage.getType());
    Graphics2D g = dimg.createGraphics();
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g.drawImage(rImage, 0, 0, newW, newH, 0, 0, oldW, oldH, null);
    g.dispose();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    String mimeType = MimeTypeUtil.getInstance().getMimeTypeName(bytea);
    if (mimeType.contains("jpeg")) {
        mimeType = "jpeg";
    } else if (mimeType.contains("png")) {
        mimeType = "png";
    } else if (mimeType.contains("gif")) {
        mimeType = "gif";
    } else if (mimeType.contains("bmp")) {
        mimeType = "bmp";
    } else {
        return bytea;
    }
    ImageIO.write(dimg, mimeType, baos);
    byte[] bytesOut = baos.toByteArray();
    return bytesOut;
}