Example usage for java.awt RenderingHints VALUE_RENDER_QUALITY

List of usage examples for java.awt RenderingHints VALUE_RENDER_QUALITY

Introduction

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

Prototype

Object VALUE_RENDER_QUALITY

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

Click Source Link

Document

Rendering hint value -- rendering algorithms are chosen with a preference for output quality.

Usage

From source file:org.medici.bia.controller.manuscriptviewer.IIPImageServerController.java

/**
 * //from  w w w .j a v a  2 s  . c om
 * @param imageName
 * @param thumbnailWidth
 * @param thumbnailFormat
 * @param response
 */
private void generateThumbnailImage(String imageName, Double thumbnailWidth, Integer imageQuality,
        String thumbnailFormat, HttpServletResponse httpServletResponse) {
    File imageFile = new File(
            ApplicationPropertyManager.getApplicationProperty("iipimage.image.path") + imageName);

    ImageInputStream imageInputStream = null;
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    InputStream inputStream = null;

    try {
        if (imageFile.canRead()) {
            // Reading complete tiff information
            imageInputStream = ImageIO.createImageInputStream(imageFile);
        } else {
            logger.error("File " + imageFile.toString() + " is not present on filesystem. ");
            imageFile = new File(ApplicationPropertyManager.getApplicationProperty("iipimage.image.path")
                    + ApplicationPropertyManager.getApplicationProperty("iipimage.image.notavailable"));
            if (imageFile.canRead()) {
                // Reading complete tiff information
                imageInputStream = ImageIO.createImageInputStream(imageFile);
            } else {
                logger.error("File " + imageFile.toString() + " is not present on filesystem. ");
            }
        }

        if (imageInputStream != null) {
            Iterator<ImageReader> readers = ImageIO.getImageReaders(imageInputStream);
            if (readers.hasNext()) {
                ImageReader reader = readers.next();
                reader.setInput(imageInputStream, false, true);

                BufferedImage page = null;
                if (reader.getNumImages(true) <= 2) {
                    logger.error(imageFile + " have " + reader.getNumImages(true)
                            + " level. Trying to render thumbnail image from level 1");
                    page = reader.read(0);
                } else {
                    page = reader.read(2);
                }

                if (page != null) {
                    RenderedOp thubmnailImage = null;
                    try {
                        double resizeFactor = thumbnailWidth / page.getWidth();

                        if (resizeFactor <= 1) {
                            ParameterBlock paramBlock = new ParameterBlock();
                            paramBlock.addSource(page); // The source image
                            paramBlock.add(resizeFactor); // The xScale
                            paramBlock.add(resizeFactor); // The yScale
                            paramBlock.add(0.0); // The x translation
                            paramBlock.add(0.0); // The y translation
                            RenderingHints qualityHints = new RenderingHints(RenderingHints.KEY_RENDERING,
                                    RenderingHints.VALUE_RENDER_QUALITY);
                            thubmnailImage = JAI.create("SubsampleAverage", paramBlock, qualityHints);
                        } else if (resizeFactor > 1) {
                            thubmnailImage = ScaleDescriptor.create(page, (float) resizeFactor,
                                    (float) resizeFactor, 0.0f, 0.0f,
                                    Interpolation.getInstance(Interpolation.INTERP_BICUBIC), null);
                        }

                        if ((thumbnailFormat != null) && (thumbnailFormat.toLowerCase().equals("jpeg"))) {
                            // replaced statement to control jpeg quality
                            // ImageIO.write(thubmnailImage, "jpeg",
                            // byteArrayOutputStream);
                            JPEGEncodeParam jpgparam = new JPEGEncodeParam();
                            jpgparam.setQuality(imageQuality);
                            ImageEncoder enc = ImageCodec.createImageEncoder("jpeg", byteArrayOutputStream,
                                    jpgparam);
                            enc.encode(thubmnailImage);
                        } else {
                            logger.error("Unmanaged thumbnail format " + thumbnailFormat);
                        }
                    } catch (IOException ioException) {
                        logger.error(ioException);
                    }
                }

                // preparing image for output
                inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());

                // writing image to output
                httpServletResponse.setContentType("image/jpeg");
                IOUtils.copy(inputStream, httpServletResponse.getOutputStream());

                // Flushing request
                httpServletResponse.getOutputStream().flush();
            }
        } else {
            logger.error("File " + imageFile.toString() + " is not present on filesystem.");
        }
    } catch (IIOException iioException) {
        if (iioException.getMessage().equals("Unsupported Image Type")) {
            logger.error("Unsupported Image Type " + imageFile);
        } else {
            logger.error(iioException);
        }
    } catch (IOException ioException) {
        logger.error(ioException);
    } finally {
        try {
            if (inputStream != null) {
                inputStream.close();
            }
        } catch (IOException ioException) {
        }
        try {
            if (byteArrayOutputStream != null) {
                byteArrayOutputStream.close();
            }
        } catch (IOException ioException) {
        }
        try {
            if (imageInputStream != null) {
                imageInputStream.close();
            }
        } catch (IOException ioException) {
        }
    }
}

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

/**
 * Resize an image//from   w  w w .  ja  va  2 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:util.ui.UiUtilities.java

/**
 * Scales Icons to a specific size/*from  w w  w  .jav a  2 s .c  om*/
 *
 * @param icon
 *          Icon that should be scaled
 * @param width
 *          scaled width
 * @param height
 *          scaled height
 * @return Scaled Icon
 */
public static Icon scaleIcon(Icon icon, int width, int height) {
    if (icon == null) {
        return null;
    }
    int currentWidth = icon.getIconWidth();
    int currentHeight = icon.getIconHeight();
    if ((currentWidth == width) && (currentHeight == height)) {
        return icon;
    }
    try {
        // Create Image with Icon
        BufferedImage iconImage = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(),
                BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = iconImage.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        AffineTransform z = g2.getTransform();
        g2.setTransform(z);
        icon.paintIcon(null, g2, 0, 0);
        g2.dispose();
        BufferedImage scaled = scaleDown(iconImage, width, height);
        // Return new Icon
        return new ImageIcon(scaled);
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return icon;
}

From source file:MyJava3D.java

public void setRendering(boolean rd) {
    Rendering = rd ? RenderingHints.VALUE_RENDER_QUALITY : RenderingHints.VALUE_RENDER_SPEED;
}

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

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

From source file:org.pentaho.di.core.gui.SwingDirectGC.java

public void setAntialias(boolean antiAlias) {
    if (antiAlias) {

        RenderingHints hints = new RenderingHints(RenderingHints.KEY_TEXT_ANTIALIASING,
                RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        hints.add(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
        hints.add(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
        // hints.add(new RenderingHints(RenderingHints.KEY_ALPHA_INTERPOLATION,
        // RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY));
        gc.setRenderingHints(hints);/*from w  ww .  j av  a  2  s.com*/
    }
}

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./*www.j  av a 2 s.  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:org.csstudio.swt.widgets.figures.ImageFigure.java

private void initRenderingHints() {
    transcoder.getRenderingHints().put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    transcoder.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,
            RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
    transcoder.getRenderingHints().put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    transcoder.getRenderingHints().put(RenderingHints.KEY_FRACTIONALMETRICS,
            RenderingHints.VALUE_FRACTIONALMETRICS_ON);
    transcoder.getRenderingHints().put(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
}

From source file:net.geoprism.dashboard.DashboardMap.java

private BufferedImage getLegendTitleImage(DashboardLayer layer) {

    FontMetrics fm;/*ww w.j  a  v a2 s  .c  o  m*/
    int textWidth;
    int textHeight;
    int textBoxHorizontalPadding = 4;
    int textBoxVerticalPadding = 4;
    int borderWidth = 2;
    int paddedTitleHeight;
    int paddedTitleWidth;
    int titleLeftPadding = textBoxHorizontalPadding;
    BufferedImage newLegendTitleBase;
    Graphics2D newLegendTitleBaseGraphic = null;

    try {
        // Build the Font object
        Font titleFont = new Font(layer.getName(), Font.BOLD, 14);

        // Build variables for base legend graphic construction
        try {
            newLegendTitleBase = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
            newLegendTitleBaseGraphic = newLegendTitleBase.createGraphics();

            newLegendTitleBaseGraphic.setFont(titleFont);

            fm = newLegendTitleBaseGraphic.getFontMetrics();
            textHeight = fm.getHeight();
            textWidth = fm.stringWidth(layer.getName());

            paddedTitleWidth = textWidth + (textBoxHorizontalPadding * 2) + (borderWidth * 2);

            paddedTitleHeight = textHeight + (textBoxVerticalPadding * 2) + (borderWidth * 2);
        } finally {
            // dispose of temporary graphics context
            if (newLegendTitleBaseGraphic != null) {
                newLegendTitleBaseGraphic.dispose();
            }
        }

        titleLeftPadding = ((paddedTitleWidth / 2)
                - ((textWidth + (textBoxHorizontalPadding * 2) + (borderWidth * 2)) / 2))
                + textBoxHorizontalPadding;

        newLegendTitleBase = new BufferedImage(paddedTitleWidth, paddedTitleHeight,
                BufferedImage.TYPE_INT_ARGB);
        newLegendTitleBaseGraphic = newLegendTitleBase.createGraphics();
        newLegendTitleBaseGraphic.drawImage(newLegendTitleBase, 0, 0, null);

        newLegendTitleBaseGraphic.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,
                RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
        newLegendTitleBaseGraphic.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        newLegendTitleBaseGraphic.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING,
                RenderingHints.VALUE_COLOR_RENDER_QUALITY);
        newLegendTitleBaseGraphic.setRenderingHint(RenderingHints.KEY_DITHERING,
                RenderingHints.VALUE_DITHER_ENABLE);
        newLegendTitleBaseGraphic.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
                RenderingHints.VALUE_FRACTIONALMETRICS_ON);
        newLegendTitleBaseGraphic.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        newLegendTitleBaseGraphic.setRenderingHint(RenderingHints.KEY_RENDERING,
                RenderingHints.VALUE_RENDER_QUALITY);
        newLegendTitleBaseGraphic.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
                RenderingHints.VALUE_STROKE_PURE);
        newLegendTitleBaseGraphic.setFont(titleFont);

        // draw title text
        fm = newLegendTitleBaseGraphic.getFontMetrics();
        newLegendTitleBaseGraphic.setColor(Color.WHITE);
        newLegendTitleBaseGraphic.drawString(layer.getName(), titleLeftPadding,
                fm.getAscent() + textBoxVerticalPadding);

        newLegendTitleBaseGraphic.drawImage(newLegendTitleBase, 0, 0, null);
    } finally {
        if (newLegendTitleBaseGraphic != null) {
            newLegendTitleBaseGraphic.dispose();
        }
    }

    return newLegendTitleBase;
}

From source file:com.centurylink.mdw.designer.pages.ExportHelper.java

public byte[] printImage(float scale, CanvasCommon canvas, Dimension graphsize, String format)
        throws IOException {
    int h_margin = 72, v_margin = 72;
    BufferedImage image = new BufferedImage(graphsize.width + h_margin, graphsize.height + v_margin,
            BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = image.createGraphics();
    if (scale > 0)
        g2.scale(scale, scale);//  w w w  .java2 s . c  o m
    g2.setBackground(Color.WHITE);
    g2.clearRect(0, 0, image.getWidth(), image.getHeight());
    // canvas.paint(g2);
    Color bgsave = canvas.getBackground();
    boolean edsave = canvas.editable;
    canvas.editable = false;
    canvas.setBackground(Color.white);
    g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    canvas.paintComponent(g2);
    canvas.setBackground(bgsave);
    canvas.editable = edsave;
    g2.dispose();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(image, format, baos);
    image = null;
    Runtime r = Runtime.getRuntime();
    r.gc();
    return baos.toByteArray();
}