Example usage for java.awt RenderingHints KEY_INTERPOLATION

List of usage examples for java.awt RenderingHints KEY_INTERPOLATION

Introduction

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

Prototype

Key KEY_INTERPOLATION

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

Click Source Link

Document

Interpolation hint key.

Usage

From source file:com.sketchy.image.ImageProcessingThread.java

public static BufferedImage rotateImage(BufferedImage image, RotateOption rotateOption) {

    if (rotateOption == RotateOption.ROTATE_NONE)
        return image;

    int degrees = 0;
    int imageWidth = image.getWidth();
    int imageHeight = image.getHeight();

    BufferedImage rotatedImage = null;
    if (rotateOption == RotateOption.ROTATE_90) {
        degrees = 90;//ww  w  .  j  a v a  2s .  co  m
        rotatedImage = new BufferedImage(imageHeight, imageWidth, BufferedImage.TYPE_INT_ARGB);
    } else if (rotateOption == RotateOption.ROTATE_180) {
        degrees = 180;
        rotatedImage = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_ARGB);
    } else if (rotateOption == RotateOption.ROTATE_270) {
        degrees = 270;
        rotatedImage = new BufferedImage(imageHeight, imageWidth, BufferedImage.TYPE_INT_ARGB);
    }

    Graphics2D g = rotatedImage.createGraphics();
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);

    g.rotate(Math.toRadians(degrees), 0, 0);

    if (degrees == 90) {
        g.drawImage(image, null, 0, -imageHeight);
    } else if (degrees == 180) {
        g.drawImage(image, null, -imageWidth, -imageHeight);
    } else if (degrees == 270) {
        g.drawImage(image, null, -imageWidth, 0);
    }
    g.dispose();
    return rotatedImage;
}

From source file:net.sf.firemox.tools.Picture.java

@Override
public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    if (cardImage != null) {
        g.drawImage(cardImage, 0, 0, getWidth(), getHeight(), this);
    }//from   w  ww  . j  a v a  2s. c  o m
}

From source file:com.fluidops.iwb.deepzoom.DZConvert.java

/**
 * Returns resized image// w w  w.j a va 2s.c  o m
 * NB - useful reference on high quality image resizing can be found here:
 *   http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html
 * @param width the required width
 * @param height the frequired height
 * @param img the image to be resized
 */
private static BufferedImage resizeImage(BufferedImage img, double width, double height) {
    int w = (int) width;
    int h = (int) height;
    BufferedImage result = new BufferedImage(w, h, getType(img));
    Graphics2D g = result.createGraphics();
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    g.drawImage(img, 0, 0, w, h, 0, 0, img.getWidth(), img.getHeight(), null);
    return result;
}

From source file:algo.PlotBar.java

private BufferedImage bufferResize(BufferedImage original, double widthFactor, double heightFactor) {

    // original image width & height
    int w, h;// w w  w.j  av  a  2s  .  co  m
    w = original.getHeight();
    h = original.getWidth();
    //        System.out.println(original.getHeight());
    //        System.out.println(original.getWidth());

    // new width & height calculated by multiplying factor
    int newWidth = new Double(original.getWidth() * widthFactor).intValue();
    int newHeight = new Double(original.getWidth() * heightFactor).intValue();

    // new resized image
    BufferedImage buffResized = new BufferedImage(newWidth, newHeight, original.getType());

    Graphics2D g = buffResized.createGraphics();

    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g.drawImage(original, 0, 0, newWidth, newHeight, 0, 0, w, h, null);
    g.dispose();

    return buffResized;
}

From source file:com.simiacryptus.util.Util.java

/**
 * Resize buffered image.//from  w  w  w. jav a 2  s  .  c  om
 *
 * @param image the image
 * @return the buffered image
 */
@Nullable
public static BufferedImage resize(@Nullable final BufferedImage image) {
    if (null == image)
        return image;
    final int width = Math.min(image.getWidth(), 800);
    if (width == image.getWidth())
        return image;
    final int height = image.getHeight() * width / image.getWidth();
    @javax.annotation.Nonnull
    final BufferedImage rerender = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    final Graphics gfx = rerender.getGraphics();
    @javax.annotation.Nonnull
    final RenderingHints hints = new RenderingHints(RenderingHints.KEY_INTERPOLATION,
            RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    ((Graphics2D) gfx).setRenderingHints(hints);
    gfx.drawImage(image, 0, 0, rerender.getWidth(), rerender.getHeight(), null);
    return rerender;
}

From source file:com.sketchy.image.ImageProcessingThread.java

public static BufferedImage flipImage(BufferedImage image, FlipOption flipOption) {

    if (flipOption == FlipOption.FLIP_NONE)
        return image;

    int imageWidth = image.getWidth();
    int imageHeight = image.getHeight();

    BufferedImage flippedImage = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_ARGB);

    Graphics2D g = flippedImage.createGraphics();
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);

    int startX = 0;
    int startY = 0;
    int endX = imageWidth;
    int endY = imageHeight;
    if ((flipOption == FlipOption.FLIP_HORIZONTAL) || (flipOption == FlipOption.FLIP_BOTH)) {
        startX = imageWidth;//w  w  w. ja va  2 s .co  m
        endX = -imageWidth;
    }
    if ((flipOption == FlipOption.FLIP_VERTICAL) || (flipOption == FlipOption.FLIP_BOTH)) {
        startY = imageHeight;
        endY = -imageHeight;
    }

    g.drawImage(image, startX, startY, endX, endY, null);
    g.dispose();
    return flippedImage;
}

From source file:com.t3.persistence.PersistenceUtil.java

public static void saveToken(Token token, File file, boolean doWait) throws IOException {
    // Thumbnail/*from  www  .  j a v  a2 s .c o m*/
    BufferedImage image = null;
    if (doWait)
        image = ImageManager.getImageAndWait(token.getImageAssetId());
    else
        image = ImageManager.getImage(token.getImageAssetId());

    Dimension sz = new Dimension(image.getWidth(), image.getHeight());
    SwingUtil.constrainTo(sz, 50);
    BufferedImage thumb = new BufferedImage(sz.width, sz.height, BufferedImage.TRANSLUCENT);
    Graphics2D g = thumb.createGraphics();
    g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g.drawImage(image, 0, 0, sz.width, sz.height, null);
    g.dispose();

    PackedFile pakFile = null;
    try {
        pakFile = new PackedFile(file);
        saveAssets(token.getAllImageAssets(), pakFile);
        pakFile.putFile(Token.FILE_THUMBNAIL, ImageUtil.imageToBytes(thumb, "png"));
        pakFile.setContent(token);
        pakFile.save();
    } finally {
        if (pakFile != null)
            pakFile.close();
    }
}

From source file:edu.ku.brc.ui.GraphicsUtils.java

/**
 * Gets a scaled icon and if it doesn't exist it creates one and scales it
 * @param icon image to be scaled//from  ww  w  . java 2 s.  c  om
 * @param iconSize the icon size (Std)
 * @param scaledIconSize the new scaled size in pixels
 * @return the scaled icon
 */
public static BufferedImage getBufferedImage(final ImageIcon icon) {
    Image imgMemory = icon.getImage();

    //make sure all pixels in the image were loaded
    imgMemory = new ImageIcon(imgMemory).getImage();

    int w = icon.getIconWidth();
    int h = icon.getIconHeight();
    BufferedImage bufferedImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);

    Graphics2D graphics2D = bufferedImage.createGraphics();
    graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    graphics2D.drawImage(imgMemory, 0, 0, w, h, 0, 0, w, h, null);
    graphics2D.dispose();

    return bufferedImage;
}

From source file:edu.ku.brc.ui.GraphicsUtils.java

/**
 * Gets a scaled icon and if it doesn't exist it creates one and scales it
 * @param icon image to be scaled/*w  w w .  ja v  a2s .co  m*/
 * @param iconSize the icon size (Std)
 * @param scaledIconSize the new scaled size in pixels
 * @return the scaled icon
 */
public static Image getScaledImage(final ImageIcon icon, final int newMaxWidth, final int newMaxHeight,
        final boolean maintainRatio) {
    if (icon != null) {
        int dstWidth = newMaxWidth;
        int dstHeight = newMaxHeight;

        int srcWidth = icon.getIconWidth();
        int srcHeight = icon.getIconHeight();

        if ((dstWidth < 0) || (dstHeight < 0)) { //image is nonstd, revert to original size
            dstWidth = icon.getIconWidth();
            dstHeight = icon.getIconHeight();
        }

        if (maintainRatio) {
            double longSideForSource = Math.max(srcWidth, srcHeight);
            double longSideForDest = Math.max(dstWidth, dstHeight);

            double multiplier = longSideForDest / longSideForSource;
            dstWidth = (int) (srcWidth * multiplier);
            dstHeight = (int) (srcHeight * multiplier);
        }

        Image imgMemory = icon.getImage();

        //make sure all pixels in the image were loaded
        imgMemory = new ImageIcon(imgMemory).getImage();

        BufferedImage thumbImage = new BufferedImage(dstWidth, dstHeight, BufferedImage.TYPE_INT_ARGB);

        Graphics2D graphics2D = thumbImage.createGraphics();
        graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2D.drawImage(imgMemory, 0, 0, dstWidth, dstHeight, 0, 0, srcWidth, srcHeight, null);
        graphics2D.dispose();
        return thumbImage;

    }
    return null;
}

From source file:com.cmart.PageControllers.SellItemImagesController.java

private static BufferedImage resizeImageWithHint(BufferedImage originalImage, int type, int width, int height) {

    BufferedImage resizedImage = new BufferedImage(width, width, type);
    Graphics2D g = resizedImage.createGraphics();
    g.drawImage(originalImage, 0, 0, width, width, null);
    g.dispose();/*from   w  w  w  .  j a va2 s  .  co  m*/
    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;
}