Example usage for java.awt Graphics2D dispose

List of usage examples for java.awt Graphics2D dispose

Introduction

In this page you can find the example usage for java.awt Graphics2D dispose.

Prototype

public abstract void dispose();

Source Link

Document

Disposes of this graphics context and releases any system resources that it is using.

Usage

From source file:com.taunova.app.libview.components.ImageHelpers.java

public static Image getScaledImage(BufferedImage image, int width) {
    Dimension d = getScaledDimension(image, width);
    Image scaledImage = image.getScaledInstance(d.width, d.height, Image.SCALE_SMOOTH);

    BufferedImage resizedImage = null;

    final boolean OPTIMIZE_SCALE = true;

    if (OPTIMIZE_SCALE) {
        ResampleOp resampleOp = new ResampleOp(100, 200);
        resampleOp.setUnsharpenMask(AdvancedResizeOp.UnsharpenMask.Normal);
        resizedImage = resampleOp.filter(image, null);
    } else {/* w ww.  j av  a2s.co m*/
        resizedImage = new BufferedImage(d.width, d.height, BufferedImage.TYPE_INT_RGB);
        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(scaledImage, 0, 0, d.width, d.height, null);
        g.dispose();
    }

    return resizedImage;
}

From source file:de.mpg.imeji.logic.storage.util.ImageUtils.java

/**
 * Convenience method that returns a scaled instance of the provided {@link BufferedImage}.
 * /*from   w  w  w. j a  v  a2  s.co  m*/
 * @param img the original image to be scaled
 * @param targetWidth the desired width of the scaled instance, in pixels
 * @param targetHeight the desired height of the scaled instance, in pixels
 * @param hint one of the rendering hints that corresponds to {@code RenderingHints.KEY_INTERPOLATION} (e.g.
 *            {@code RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR},
 *            {@code RenderingHints.VALUE_INTERPOLATION_BILINEAR},
 *            {@code RenderingHints.VALUE_INTERPOLATION_BICUBIC})
 * @param higherQuality if true, this method will use a multi-step scaling technique that provides higher quality
 *            than the usual one-step technique (only useful in downscaling cases, where {@code targetWidth} or
 *            {@code targetHeight} is smaller than the original dimensions, and generally only when the
 *            {@code BILINEAR} hint is specified)
 * @return a scaled version of the original {@link BufferedImage}
 */
public static BufferedImage getScaledInstance(BufferedImage img, int targetWidth, int targetHeight, Object hint,
        boolean higherQuality) {
    int type = (img.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB
            : BufferedImage.TYPE_INT_ARGB;
    BufferedImage ret = img;
    int w, h;
    if (higherQuality) {
        // 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();
    } else {
        // Use one-step technique: scale directly from original
        // size to target size with a single drawImage() call
        w = targetWidth;
        h = targetHeight;
    }
    do {
        if (higherQuality && w > targetWidth) {
            w /= 2;
            if (w < targetWidth) {
                w = targetWidth;
            }
        }
        if (higherQuality && h > targetHeight) {
            h /= 2;
            if (h < targetHeight) {
                h = targetHeight;
            }
        }
        BufferedImage tmp = new BufferedImage(w, h, type);
        Graphics2D g2 = tmp.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);
        g2.drawImage(ret, 0, 0, w, h, null);
        g2.dispose();
        ret = tmp;
    } while (w != targetWidth || h != targetHeight);
    return ret;
}

From source file:common.utils.ImageUtils.java

/**
  * resize input image to new dinesions(only smaller) and save it to file
  * @param image input image for scaling
  * @param scale_factor factor for scaling image
 * @param rez writes resulting image to a file
 * @throws IOException//from w  ww. ja v  a2s  .c  o  m
  */
public static void saveScaledImageWidth(BufferedImage image, double scale_factor, OutputStream rez)
        throws IOException {
    //long time_resize = System.currentTimeMillis();
    if (scale_factor == 1) {
        writeImage(image, 1, rez);
    } else {
        int width = (int) (scale_factor * image.getWidth());
        int height = (int) (scale_factor * image.getHeight());
        //BufferedImage scaled_bi = new BufferedImage( image.getWidth(), image.getHeight(), image.getType() );
        int type = image.getType();
        BufferedImage scaled_bi;
        if (type == BufferedImage.TYPE_CUSTOM) {
            scaled_bi = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
        } else {
            scaled_bi = new BufferedImage(width, height, type);
        }

        Graphics2D g2 = scaled_bi.createGraphics();

        //g2.drawImage(image, at, null);
        g2.drawImage(image.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
        writeImage(scaled_bi, 1, rez);

        g2.dispose();
    }
    //time_resize = System.currentTimeMillis() - time_resize;
    //System.out.print("time_resize=" + (time_resize) + "; ");
}

From source file:br.com.diegosilva.jsfcomponents.util.Utils.java

public static byte[] resizeImage(byte[] imageData, String contentType, int width) {
    ImageIcon icon = new ImageIcon(imageData);

    double ratio = (double) width / icon.getIconWidth();
    int resizedHeight = (int) (icon.getIconHeight() * ratio);

    int imageType = "image/png".equals(contentType) ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB;
    BufferedImage bImg = new BufferedImage(width, resizedHeight, imageType);
    Graphics2D g2d = bImg.createGraphics();
    g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    g2d.drawImage(icon.getImage(), 0, 0, width, resizedHeight, null);
    g2d.dispose();

    String formatName = "";
    if ("image/png".equals(contentType))
        formatName = "png";
    else if ("image/jpeg".equals(contentType) || "image/jpg".equals(contentType))
        formatName = "jpeg";

    ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
    try {//from  w w  w .j a  v  a  2s.c o m
        ImageIO.write(bImg, formatName, baos);
        return baos.toByteArray();
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}

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();
    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:com.fengduo.bee.service.impl.file.FileServiceImpl.java

/**
 * ?// w w w.ja  va2  s . co m
 * 
 * @param source
 * @param targetW
 * @param targetH
 * @param ifScaling ?
 * @return
 */
public static BufferedImage resize(BufferedImage source, int targetW, int targetH, boolean ifScaling) {
    // targetWtargetH
    int type = source.getType();
    BufferedImage target = null;
    double sx = (double) targetW / source.getWidth();
    double sy = (double) targetH / source.getHeight();
    // targetWtargetH??,?if else???
    if (ifScaling) {
        if (sx > sy) {
            sx = sy;
            targetW = (int) (sx * source.getWidth());
        } else {
            sy = sx;
            targetH = (int) (sy * source.getHeight());
        }
    }
    if (type == BufferedImage.TYPE_CUSTOM) { // handmade
        ColorModel cm = source.getColorModel();
        WritableRaster raster = cm.createCompatibleWritableRaster(targetW, targetH);
        boolean alphaPremultiplied = cm.isAlphaPremultiplied();
        target = new BufferedImage(cm, raster, alphaPremultiplied, null);
    } else
        target = new BufferedImage(targetW, targetH, type);
    Graphics2D g = target.createGraphics();
    // smoother than exlax:
    g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));
    g.dispose();
    return target;
}

From source file:de.mprengemann.intellij.plugin.androidicons.images.ImageUtils.java

private static BufferedImage trim9PBorder(BufferedImage inputImage) {
    BufferedImage trimedImage = UIUtil.createImage(inputImage.getWidth() - 2, inputImage.getHeight() - 2,
            BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = trimedImage.createGraphics();
    g.drawImage(inputImage, 0, 0, trimedImage.getWidth(), trimedImage.getHeight(), 1, 1,
            inputImage.getWidth() - 1, inputImage.getHeight() - 1, null);
    g.dispose();
    return trimedImage;
}

From source file:io.github.moosbusch.lumpi.util.LumpiUtil.java

public static BufferedImage toBufferedImage(Visual visual) {
    BufferedImage result = createCompatibleImage(visual.getWidth(), visual.getHeight());
    Graphics2D g2 = result.createGraphics();
    visual.paint(g2);//from w w w  . j av  a2s .c  o m
    g2.dispose();

    return result;
}

From source file:net.sf.mzmine.chartbasics.graphicsexport.ChartExportUtil.java

/**
 * This method saves a chart as a PDF with given dimensions
 * //w  w  w.  ja va  2 s.  c om
 * @param chart
 * @param width
 * @param height
 * @param fileName is a full path
 */
public static void writeChartToPDF(JFreeChart chart, int width, int height, File fileName) throws Exception {
    PdfWriter writer = null;

    Document document = new Document(new Rectangle(width, height));

    try {
        writer = PdfWriter.getInstance(document, new FileOutputStream(fileName));
        document.open();
        PdfContentByte contentByte = writer.getDirectContent();
        PdfTemplate template = contentByte.createTemplate(width, height);
        Graphics2D graphics2d = template.createGraphics(width, height, new DefaultFontMapper());
        Rectangle2D rectangle2d = new Rectangle2D.Double(0, 0, width, height);

        chart.draw(graphics2d, rectangle2d);

        graphics2d.dispose();
        contentByte.addTemplate(template, 0, 0);

    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    } finally {
        document.close();
    }
}

From source file:com.actelion.research.orbit.imageAnalysis.utils.ImageUtils.java

/**
 * Rescales using predefined min,max intensities and converts to 8bit. Works for gray- and rgb color images.
 *
 * @param bi16//from   ww w.ja  va 2s. c  o m
 * @return
 */
public static BufferedImage convertTo8bit(BufferedImage bi16, int min, int max) {
    BufferedImage bi = null;
    if (bi16.getSampleModel().getNumBands() == 1) {
        bi16 = ImageUtils.scaleIntensities(bi16, new int[] { min }, new int[] { max });
        bi = new BufferedImage(bi16.getWidth(), bi16.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
    } else if (bi16.getSampleModel().getNumBands() == 3) {
        throw new IllegalArgumentException("only grayscale images supported");
    } else {
        throw new IllegalArgumentException(
                "Only images with 1 band (gray-color) or 3 bands (rgb) supported. This image has "
                        + bi16.getSampleModel().getNumBands() + " bands.");
    }
    Graphics2D g2d = bi.createGraphics();
    g2d.drawImage(bi16, 0, 0, null);
    g2d.dispose();
    return bi;
}