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:gg.msn.ui.panel.MainPanel.java

@Override
public void paint(Graphics g) {
    super.paint(g);
    try {/*from   w ww.  j a  v  a2 s. c o m*/
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        //render per utenti Facebook
        if (ChatClientView.protocol.equals(ChatClientView.FACEBOOK_PROTOCOL)) {
            final FacebookUser user = (FacebookUser) value;
            int textY = (getHeight() / 2) + (g2.getFont().getSize() / 2);

            //name string
            g2.setFont(list.getFont());
            g2.drawString(user.name, WHITE_SPACE + IMAGE_LATE + 3, textY);

            //status string
            //                g2.setFont(list.getFont());
            //                g2.drawString(user.status, WHITE_SPACE + IMAGE_LATE + 3, textY);

            //icon
            ImageIcon icon = user.portrait;
            //log.debug("icon [" + icon + "]");
            //ImageIcon scaledIcon = new ImageIcon(icon.getImage().getScaledInstance(24, 24, Image.SCALE_AREA_AVERAGING));
            g2.drawImage(icon.getImage(), WHITE_SPACE, getHeight() / 2 - (IMAGE_LATE / 2), IMAGE_LATE,
                    IMAGE_LATE, null);
            //                 g2.setColor(Color.WHITE);
            //                g2.drawRoundRect(WHITE_SPACE, getHeight() / 2 - (IMAGE_LATE / 2), IMAGE_LATE, IMAGE_LATE, ARC_SIZE, ARC_SIZE);
            //                g2.drawRoundRect(WHITE_SPACE, getHeight() / 2 - ((IMAGE_LATE+1) / 2), IMAGE_LATE+1, IMAGE_LATE+1, ARC_SIZE, ARC_SIZE);
            //                g2.drawRoundRect(WHITE_SPACE, getHeight() / 2 - ((IMAGE_LATE+2) / 2), IMAGE_LATE+2, IMAGE_LATE+2, ARC_SIZE, ARC_SIZE);
            //render per utenti Client
            //                log.debug("user status [" + user.status + "]");
            //                log.debug("user online status [" + user.onlineStatus + "]");
            if (StringUtils.equals(user.status, FacebookUser.STATUS_ONLINE)) {
                g2.setColor(Color.GREEN);
                g2.fillOval(getWidth() - STATUS_ICON_OFFSET, (getHeight() / 2) - (STATUS_ICON_WIDTH / 2),
                        STATUS_ICON_WIDTH, STATUS_ICON_WIDTH);
            } else {
                g2.setColor(Color.GRAY);
                g2.fillOval(getWidth() - STATUS_ICON_OFFSET, (getHeight() / 2) - (STATUS_ICON_WIDTH / 2),
                        STATUS_ICON_WIDTH, STATUS_ICON_WIDTH);
            }

        } else {
            g2.setFont(list.getFont());
            int textY = (getHeight() / 2) + (g2.getFont().getSize() / 2);

            Client client = (Client) value;
            //                setText((client).getNick());
            ImageIcon icon = null;
            try {
                new ImageIcon(client.getImage());
            } catch (Exception e) {
                //                    log.debug("immgine non presente");
                icon = ThemeManager.getTheme().get(ThemeManager.USER_ICON);
            }
            g2.drawString(client.getNick(), WHITE_SPACE + IMAGE_LATE + 3, textY);
            //log.debug("icon [" + icon + "]");
            //ImageIcon scaledIcon = new ImageIcon(icon.getImage().getScaledInstance(24, 24, Image.SCALE_AREA_AVERAGING));
            g2.drawImage(icon.getImage(), WHITE_SPACE, getHeight() / 2 - (IMAGE_LATE / 2), IMAGE_LATE,
                    IMAGE_LATE, null);

            //                setFont(list.getFont());
            //                setIcon(scaledIcon);
        }
    } catch (Exception e) {
        log.warn(e);
    }

}

From source file:com.funambol.foundation.util.MediaUtils.java

/**
 * Creates the thumbnail.//from w  w w  . j a  v a  2 s  . c om
 *
 * @param imageFile the image file
 * @param thumbFile the empty thumbnail file
 * @param thumbX the width of the thumbnail
 * @param thumbY the height of the thumbnail
 * @param imageName the image file name with extension
 * @param tolerance the percentage of tolerance before creating a thumbnail
 * @return true is the thumbnail has been created, false otherwise
 * @throws IOException if an error occurs
 */
private static boolean createThumbnail(File imageFile, File thumbFile, int thumbX, int thumbY, String imageName,
        double tolerance) throws IOException {

    FileInputStream fileis = null;
    ImageInputStream imageis = null;

    Iterator readers = null;

    try {

        readers = ImageIO.getImageReadersByFormatName(imageName.substring(imageName.lastIndexOf('.') + 1));
        if (readers == null || (!readers.hasNext())) {
            throw new IOException("File not supported");
        }

        ImageReader reader = (ImageReader) readers.next();

        fileis = new FileInputStream(imageFile);
        imageis = ImageIO.createImageInputStream(fileis);
        reader.setInput(imageis, true);

        // Determines thumbnail height, width and quality
        int thumbWidth = thumbX;
        int thumbHeight = thumbY;

        double thumbRatio = (double) thumbWidth / (double) thumbHeight;
        int imageWidth = reader.getWidth(0);
        int imageHeight = reader.getHeight(0);

        //
        // Don't create the thumbnail if the original file is smaller
        // than required size increased by % tolerance
        //
        if (imageWidth <= (thumbWidth * (1 + tolerance / 100))
                && imageHeight <= (thumbHeight * (1 + tolerance / 100))) {

            return false;
        }

        double imageRatio = (double) imageWidth / (double) imageHeight;
        if (thumbRatio < imageRatio) {
            thumbHeight = (int) (thumbWidth / imageRatio);
        } else {
            thumbWidth = (int) (thumbHeight * imageRatio);
        }

        ImageReadParam param = reader.getDefaultReadParam();
        param.setSourceSubsampling(3, 3, 0, 0);

        BufferedImage bi = reader.read(0, param);

        Image thumb = bi.getScaledInstance(thumbWidth, thumbHeight, Image.SCALE_SMOOTH);

        BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics2D = thumbImage.createGraphics();
        graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2D.drawImage(thumb, 0, 0, thumbWidth, thumbHeight, null);

        FileOutputStream fileOutputStream = new FileOutputStream(thumbFile);
        ImageIO.write(thumbImage, "jpg", fileOutputStream);

        thumb.flush();
        thumbImage.flush();
        fileOutputStream.flush();
        fileOutputStream.close();
        graphics2D.dispose();

    } finally {
        if (fileis != null) {
            fileis.close();
        }
        if (imageis != null) {
            imageis.close();
        }
    }

    return true;
}

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

/**
 * Gets a scaled icon and if it doesn't exist it creates one and scales it
 * @param icon image to be scaled/*from   w  ww.j a v  a  2s  .c om*/
 * @param iconSize the icon size (Std)
 * @param scaledIconSize the new scaled size in pixels
 * @return the scaled icon
 */
public Image getFastScale(final ImageIcon icon, final IconSize iconSize, final IconSize scaledIconSize) {
    if (icon != null) {
        int width = scaledIconSize.size();
        int height = scaledIconSize.size();

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

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

        BufferedImage thumbImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

        Graphics2D graphics2D = thumbImage.createGraphics();
        graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2D.drawImage(imgMemory, 0, 0, width, height, null);
        graphics2D.dispose();

        imgMemory = thumbImage;
        return imgMemory;

    }
    //else
    log.error("Couldn't find icon [" + iconSize + "] to scale to [" + scaledIconSize + "]");
    return null;
}

From source file:com.pronoiahealth.olhie.server.services.BookCoverImageService.java

/**
 * Resize an image//www .j  a  v  a2 s. c  o  m
 * 
 * @param originalImage
 * @param width
 * @param height
 * @param type
 * @return
 */
private BufferedImage resize(BufferedImage originalImage, int width, int height, int type) {
    BufferedImage resizedImage = new BufferedImage(width, height, type);
    Graphics2D g = resizedImage.createGraphics();
    try {
        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(originalImage, 0, 0, width, height, null);
    } finally {
        if (g != null) {
            g.dispose();
        }
    }

    return resizedImage;
}

From source file:net.rptools.maptool.util.PersistenceUtil.java

public static void saveToken(Token token, File file, boolean doWait) throws IOException {
    // Thumbnail//from w  w w  . j a v  a 2s .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.setProperty(PROP_VERSION, MapTool.getVersion());
        pakFile.save();
    } finally {
        if (pakFile != null)
            pakFile.close();
    }
}

From source file:com.funambol.foundation.util.MediaUtils.java

/**
 * Rotates given buffered image by given amount of degree.
 * The valid degree values are 0, 90, 180, 270.
 * If the image is a jpg, the rotation is lossless, exif data are preserved
 * and image size is almost the same.//  ww  w .  j a v  a  2s .  c om
 *
 * @param bufImage the buffered image
 * @param degree amount of degree to apply
 * @return a buffered image containing rotated image data
 * @throws PicturesException if amount of degree is invalid or if an
 *         IOException occurs
 */
private static BufferedImage rotateImage(BufferedImage bufImage, int degree)
        throws FileDataObjecyUtilsException {

    degree = degree % 360;
    int h;
    int w;

    switch (degree) {
    case 0:
    case 180:
        h = bufImage.getHeight();
        w = bufImage.getWidth();
        break;
    case 90:
    case 270:
        h = bufImage.getWidth();
        w = bufImage.getHeight();
        break;
    default:
        throw new FileDataObjecyUtilsException(
                "Error rotating image since the '" + degree + "' degree value is unsupported");
    }

    BufferedImage out = null;

    int bufImageType = bufImage.getType();
    if (BufferedImage.TYPE_BYTE_INDEXED == bufImageType || BufferedImage.TYPE_BYTE_BINARY == bufImageType) {

        IndexColorModel model = (IndexColorModel) bufImage.getColorModel();
        out = new BufferedImage(w, h, bufImage.getType(), model);

    } else if (BufferedImage.TYPE_CUSTOM == bufImageType) {

        // we don't know what type of image it can be

        // there's a bug in some VM that cause some PNG images to have 
        // type custom: this should take care of this issue

        //check if we need to have alpha channel
        boolean alpha = bufImage.getTransparency() != BufferedImage.OPAQUE;

        if (alpha) {
            // TYPE_INT_ARGB_PRE gives you smaller output images
            // than TYPE_INT_ARGB
            out = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB_PRE);
        } else {
            out = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        }

    } else {

        out = new BufferedImage(w, h, bufImage.getType());
    }

    Graphics2D g2d = out.createGraphics();

    Map renderingHints = new HashMap();

    renderingHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    renderingHints.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);

    g2d.setRenderingHints(renderingHints);
    g2d.rotate(Math.toRadians(degree));

    switch (degree) {
    case 90:
        g2d.translate(0, -w);
        break;
    case 180:
        g2d.translate(-w, -h);
        break;
    case 270:
        g2d.translate(-h, 0);
        break;
    }

    g2d.drawImage(bufImage, null, 0, 0);
    g2d.dispose();

    return out;
}

From source file:lucee.runtime.img.Image.java

public void paste(Image topImage, int x, int y) throws ExpressionException {
    RenderingHints interp = new RenderingHints(RenderingHints.KEY_INTERPOLATION,
            RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    BorderExtender extender = BorderExtender.createInstance(1);
    Graphics2D g = getGraphics();
    g.addRenderingHints(new RenderingHints(JAI.KEY_BORDER_EXTENDER, extender));
    g.drawImage(topImage.image(), (new AffineTransformOp(AffineTransform.getTranslateInstance(x, y), interp)),
            0, 0);//from   w  ww . ja  v a 2  s . c  o  m

}

From source file:ImageOpByRomain.java

/**
 * <p>//from  w w w .ja v a  2  s. co  m
 * Returns a thumbnail of a source image. <code>newSize</code> defines the
 * length of the longest dimension of the thumbnail. The other dimension is
 * then computed according to the dimensions ratio of the original picture.
 * </p>
 * <p>
 * This method favors speed over quality. When the new size is less than half
 * the longest dimension of the source image,
 * {@link #createThumbnail(BufferedImage, int)} or
 * {@link #createThumbnail(BufferedImage, int, int)} should be used instead to
 * ensure the quality of the result without sacrificing too much performance.
 * </p>
 * 
 * @see #createThumbnailFast(java.awt.image.BufferedImage, int, int)
 * @see #createThumbnail(java.awt.image.BufferedImage, int)
 * @see #createThumbnail(java.awt.image.BufferedImage, int, int)
 * @param image
 *            the source image
 * @param newSize
 *            the length of the largest dimension of the thumbnail
 * @return a new compatible <code>BufferedImage</code> containing a
 *         thumbnail of <code>image</code>
 * @throws IllegalArgumentException
 *             if <code>newSize</code> is larger than the largest dimension
 *             of <code>image</code> or &lt;= 0
 */
public static BufferedImage createThumbnailFast(BufferedImage image, int newSize) {
    float ratio;
    int width = image.getWidth();
    int height = image.getHeight();

    if (width > height) {
        if (newSize >= width) {
            throw new IllegalArgumentException("newSize must be lower than" + " the image width");
        } else if (newSize <= 0) {
            throw new IllegalArgumentException("newSize must" + " be greater than 0");
        }

        ratio = (float) width / (float) height;
        width = newSize;
        height = (int) (newSize / ratio);
    } else {
        if (newSize >= height) {
            throw new IllegalArgumentException("newSize must be lower than" + " the image height");
        } else if (newSize <= 0) {
            throw new IllegalArgumentException("newSize must" + " be greater than 0");
        }

        ratio = (float) height / (float) width;
        height = newSize;
        width = (int) (newSize / ratio);
    }

    BufferedImage temp = createCompatibleImage(image, width, height);
    Graphics2D g2 = temp.createGraphics();
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g2.drawImage(image, 0, 0, temp.getWidth(), temp.getHeight(), null);
    g2.dispose();

    return temp;
}

From source file:lucee.runtime.img.Image.java

public void translate(int xtrans, int ytrans, Object interpolation) throws ExpressionException {

    RenderingHints hints = new RenderingHints(RenderingHints.KEY_INTERPOLATION, interpolation);
    if (interpolation != RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR) {
        hints.add(new RenderingHints(JAI.KEY_BORDER_EXTENDER, BorderExtender.createInstance(1)));
    }//from   ww w  .j a va 2s  .c  om

    ParameterBlock pb = new ParameterBlock();
    pb.addSource(image());
    BufferedImage img = JAI.create("translate", pb).getAsBufferedImage();
    Graphics2D graphics = img.createGraphics();
    graphics.clearRect(0, 0, img.getWidth(), img.getHeight());
    AffineTransform at = new AffineTransform();
    at.setToIdentity();
    graphics.drawImage(image(), new AffineTransformOp(at, hints), xtrans, ytrans);
    graphics.dispose();
    image(img);
}

From source file:ImageOpByRomain.java

/**
 * <p>/*from ww w . ja v a 2s.c om*/
 * Returns a thumbnail of a source image.
 * </p>
 * <p>
 * This method favors speed over quality. When the new size is less than half
 * the longest dimension of the source image,
 * {@link #createThumbnail(BufferedImage, int)} or
 * {@link #createThumbnail(BufferedImage, int, int)} should be used instead to
 * ensure the quality of the result without sacrificing too much performance.
 * </p>
 * 
 * @see #createThumbnailFast(java.awt.image.BufferedImage, int)
 * @see #createThumbnail(java.awt.image.BufferedImage, int)
 * @see #createThumbnail(java.awt.image.BufferedImage, int, int)
 * @param image
 *            the source image
 * @param newWidth
 *            the width of the thumbnail
 * @param newHeight
 *            the height of the thumbnail
 * @return a new compatible <code>BufferedImage</code> containing a
 *         thumbnail of <code>image</code>
 * @throws IllegalArgumentException
 *             if <code>newWidth</code> is larger than the width of
 *             <code>image</code> or if code>newHeight</code> is larger
 *             than the height of <code>image</code> or if one of the
 *             dimensions is &lt;= 0
 */
public static BufferedImage createThumbnailFast(BufferedImage image, int newWidth, int newHeight) {
    if (newWidth >= image.getWidth() || newHeight >= image.getHeight()) {
        throw new IllegalArgumentException(
                "newWidth and newHeight cannot" + " be greater than the image" + " dimensions");
    } else if (newWidth <= 0 || newHeight <= 0) {
        throw new IllegalArgumentException("newWidth and newHeight must" + " be greater than 0");
    }

    BufferedImage temp = createCompatibleImage(image, newWidth, newHeight);
    Graphics2D g2 = temp.createGraphics();
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g2.drawImage(image, 0, 0, temp.getWidth(), temp.getHeight(), null);
    g2.dispose();

    return temp;
}