Example usage for java.awt Graphics dispose

List of usage examples for java.awt Graphics dispose

Introduction

In this page you can find the example usage for java.awt Graphics 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:WaterMark.java

public static String execute(String src, String dest, String text, Color color, Font font) throws Exception {
    BufferedImage srcImage = ImageIO.read(new File(src));

    int width = srcImage.getWidth(null);
    int height = srcImage.getHeight(null);
    BufferedImage destImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics g = destImage.getGraphics();

    g.drawImage(srcImage, 0, 0, width, height, null);
    g.setColor(color);/*  ww w.  j  a va  2s  .c o  m*/
    g.setFont(font);
    g.fillRect(0, 0, 50, 50);
    g.drawString(text, width / 5, height - 10);
    g.dispose();

    ImageIO.write(destImage, DEFAULT_FORMAT, new File("dest.jpg"));
    return dest;
}

From source file:com.openkm.util.ImageUtils.java

/**
 * cloneImage/*from  w ww .  ja v a2s . co m*/
 */
public static BufferedImage clone(BufferedImage source) {
    BufferedImage img = new BufferedImage(source.getWidth(), source.getHeight(), BufferedImage.TYPE_INT_RGB);
    Graphics g = img.getGraphics();
    g.drawImage(source, 0, 0, null);
    g.dispose();
    return img;
}

From source file:com.jaeksoft.searchlib.util.ImageUtils.java

public static BufferedImage toBufferedImage(Image image) throws InterruptedException {

    if (image instanceof BufferedImage)
        return (BufferedImage) image;

    image = new ImageIcon(image).getImage();
    int type = hasAlpha(image) ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB;
    BufferedImage bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
    Graphics g = bimage.createGraphics();
    g.drawImage(image, 0, 0, null);/*from  w  w  w  .  j  a  v  a  2s. c  o m*/
    g.dispose();
    return bimage;
}

From source file:org.b3log.symphony.model.Character.java

/**
 * Creates an image with the specified content (a character).
 *
 * @param content the specified content// w ww  . j a  va  2 s  .  co  m
 * @return image
 */
public static BufferedImage createImage(final String content) {
    final BufferedImage ret = new BufferedImage(500, 500, Transparency.TRANSLUCENT);
    final Graphics g = ret.getGraphics();
    g.setClip(0, 0, 50, 50);
    g.fillRect(0, 0, 50, 50);
    g.setFont(new Font(null, Font.PLAIN, 40));
    g.setColor(Color.BLACK);
    g.drawString(content, 5, 40);
    g.dispose();

    return ret;
}

From source file:org.polymap.core.data.image.ImageTransparencyProcessor.java

public static BufferedImage transparency(Image image, final Color markerColor) throws IOException {
    long start = System.currentTimeMillis();

    RGBImageFilter filter = new RGBImageFilter() {
        // the color we are looking for... Alpha bits are set to opaque
        public int markerRGB = markerColor.getRGB() | 0xFF000000;

        byte threshold = 25;

        double range = ((double) 0xFF) / (3 * threshold);

        public final int filterRGB(int x, int y, int rgb) {
            Color probe = new Color(rgb);
            //log.info( "probe=" + probe + ", marker=" + markerColor );

            // delta values
            int dRed = markerColor.getRed() - probe.getRed();
            int dGreen = markerColor.getGreen() - probe.getGreen();
            int dBlue = markerColor.getBlue() - probe.getBlue();
            //log.info( "    dRed=" + dRed + ", dGreen=" + dGreen );

            if (dRed >= 0 && dRed < threshold && dGreen >= 0 && dGreen < threshold && dBlue >= 0
                    && dBlue < threshold) {
                int alpha = (int) Math.round(range * (dRed + dGreen + dBlue));
                //log.info( "    -> alpha=" + alpha );

                return ((alpha << 24) | 0x00FFFFFF) & rgb;
            } else {
                // nothing to do
                return rgb;
            }//from w ww . j  a v a 2s  .c  o  m
        }
    };

    //        BufferedImage bimage = null;
    //        if (image instanceof BufferedImage) {
    //            bimage = (BufferedImage)image;
    //        }
    //        else {
    //            bimage = new BufferedImage(
    //                    image.getHeight( null ), image.getWidth( null ), BufferedImage.TYPE_INT_ARGB );
    //            Graphics g = bimage.getGraphics();
    //            g.drawImage( image, 0, 0, null );
    //            g.dispose();
    //        }

    ImageProducer ip = new FilteredImageSource(image.getSource(), filter);
    Image result = Toolkit.getDefaultToolkit().createImage(ip);

    BufferedImage bresult = new BufferedImage(image.getHeight(null), image.getWidth(null),
            BufferedImage.TYPE_INT_ARGB);
    Graphics g = bresult.getGraphics();
    g.drawImage(result, 0, 0, null);
    g.dispose();

    //        // XXX this can surely be done any more clever
    //        int width = bimage.getWidth();
    //        int height = bimage.getHeight();
    //        for (int x=bimage.getMinX(); x<width; x++) {
    //            for (int y=bimage.getMinY(); y<height; y++) {
    //                int filtered = filter.filterRGB( x, y, bimage.getRGB( x, y ) );
    //                result.setRGB( x, y, filtered );
    //            }
    //        }

    log.debug("Transparency done. (" + (System.currentTimeMillis() - start) + "ms)");
    return bresult;
}

From source file:com.mirth.connect.server.util.DICOMUtil.java

private static String returnOtherImageFormat(MessageObject message, String format, boolean autoThreshold) {
    // use new method for jpegs
    if (format.equalsIgnoreCase("jpg") || format.equalsIgnoreCase("jpeg")) {
        return new String(Base64.encodeBase64Chunked(dicomToJpg(1, message, autoThreshold)));
    }//from ww  w. j av a  2  s  . c o m

    byte[] rawImage = Base64.decodeBase64(getDICOMRawData(message).getBytes());
    ByteArrayInputStream bais = new ByteArrayInputStream(rawImage);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    try {
        DICOM dicom = new DICOM(bais);
        dicom.run(message.getType());
        BufferedImage image = new BufferedImage(dicom.getWidth(), dicom.getHeight(),
                BufferedImage.TYPE_INT_RGB);
        Graphics graphics = image.createGraphics();
        graphics.drawImage(dicom.getImage(), 0, 0, null);
        graphics.dispose();
        ImageIO.write(image, format, baos);
        return new String(Base64.encodeBase64Chunked(baos.toByteArray()));
    } catch (IOException e) {
        logger.error("Error Converting DICOM image", e);
    } finally {
        IOUtils.closeQuietly(bais);
        IOUtils.closeQuietly(baos);
    }

    return StringUtils.EMPTY;
}

From source file:org.spf4j.perf.impl.chart.Charts.java

public static BufferedImage generateCountTotalChart(final String groupName, final long[][] timestamps,
        final String[] measurementNames, final String uom1, final double[][] measurements, final int width,
        final int height, final String[] measurementNames2, final String uom2, final double[][] measurements2) {
    BufferedImage count = Charts/*from w w  w  . ja v a 2 s. c  o  m*/
            .createTimeSeriesJFreeChart("Measurements for " + groupName + " generated by spf4j", timestamps,
                    measurementNames, uom1, measurements)
            .createBufferedImage(width, height / 2);
    BufferedImage total = Charts
            .createTimeSeriesJFreeChart(null, timestamps, measurementNames2, uom2, measurements2)
            .createBufferedImage(width, height / 2);
    BufferedImage combined = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    final Graphics graphics = combined.getGraphics();
    try {
        graphics.drawImage(count, 0, 0, null);
        graphics.drawImage(total, 0, height / 2, null);
    } finally {
        graphics.dispose();
    }
    return combined;
}

From source file:org.spf4j.perf.impl.chart.Charts.java

public static BufferedImage createMinMaxAvgCountImg(final String chartName, final long[] timestamps,
        final double[] min, final double[] max, final double[] total, final double[] count, final String uom,
        final int width, final int height) {

    BufferedImage bi = Charts/*w w w  . java  2  s.c o  m*/
            .createTimeSeriesJFreeChart(chartName, timestamps, new String[] { "min", "max", "avg" }, uom,
                    new double[][] { min, max, Arrays.divide(total, count) })
            .createBufferedImage(width, height - height / 3);
    BufferedImage bi2 = Charts.createTimeSeriesJFreeChart(null, timestamps, new String[] { "count" }, "count",
            new double[][] { count }).createBufferedImage(width, height / 3);
    BufferedImage combined = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    final Graphics graphics = combined.getGraphics();
    try {
        graphics.drawImage(bi, 0, 0, null);
        graphics.drawImage(bi2, 0, height - height / 3, null);
    } finally {
        graphics.dispose();
    }
    return combined;
}

From source file:com.mirth.connect.server.util.DICOMUtil.java

private static byte[] saveAsJpeg(ImagePlus imagePlug, int quality) {
    int imageType = BufferedImage.TYPE_INT_RGB;

    if (imagePlug.getProcessor().isDefaultLut()) {
        imageType = BufferedImage.TYPE_BYTE_GRAY;
    }/*  w w w.  j  av  a  2s. c o m*/

    BufferedImage bufferedImage = new BufferedImage(imagePlug.getWidth(), imagePlug.getHeight(), imageType);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    try {
        Graphics graphics = bufferedImage.createGraphics();
        graphics.drawImage(imagePlug.getImage(), 0, 0, null);
        graphics.dispose();
        ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg").next();
        writer.setOutput(ImageIO.createImageOutputStream(baos));
        ImageWriteParam param = writer.getDefaultWriteParam();
        param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        param.setCompressionQuality(quality / 100f);

        if (quality == 100) {
            param.setSourceSubsampling(1, 1, 0, 0);
        }

        IIOImage iioImage = new IIOImage(bufferedImage, null, null);
        writer.write(null, iioImage, param);
        return baos.toByteArray();
    } catch (Exception e) {
        logger.error("Error converting dcm file", e);
    } finally {
        IOUtils.closeQuietly(baos);
    }

    return null;
}

From source file:com.mirth.connect.server.util.DICOMMessageUtil.java

private static byte[] saveAsJpeg(ImagePlus imagePlug, int quality) {
    int imageType = BufferedImage.TYPE_INT_RGB;

    if (imagePlug.getProcessor().isDefaultLut()) {
        imageType = BufferedImage.TYPE_BYTE_GRAY;
    }/*  w  w w . j a  v a2  s. c  o m*/

    BufferedImage bufferedImage = new BufferedImage(imagePlug.getWidth(), imagePlug.getHeight(), imageType);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    try {
        Graphics graphics = bufferedImage.createGraphics();
        graphics.drawImage(imagePlug.getImage(), 0, 0, null);
        graphics.dispose();
        ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg").next();
        writer.setOutput(ImageIO.createImageOutputStream(baos));
        ImageWriteParam param = writer.getDefaultWriteParam();
        param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        param.setCompressionQuality(quality / 100f);

        if (quality == 100) {
            param.setSourceSubsampling(1, 1, 0, 0);
        }

        IIOImage iioImage = new IIOImage(bufferedImage, null, null);
        writer.write(null, iioImage, param);
        return baos.toByteArray();
    } catch (Exception e) {
        logger.error("Error Converting DICOM image", e);
    } finally {
        IOUtils.closeQuietly(baos);
    }

    return null;
}