Example usage for org.jfree.chart.encoders EncoderUtil writeBufferedImage

List of usage examples for org.jfree.chart.encoders EncoderUtil writeBufferedImage

Introduction

In this page you can find the example usage for org.jfree.chart.encoders EncoderUtil writeBufferedImage.

Prototype

public static void writeBufferedImage(BufferedImage image, String format, OutputStream outputStream)
        throws IOException 

Source Link

Document

Encode the image in a specific format and write it to an OutputStream.

Usage

From source file:com.pureinfo.srm.common.ImageHelper.java

public static void drawImage(String _sString, OutputStream _os) throws PureException {
    int nWidth = 200;
    int nHeight = 50;
    String sText = _sString;/*from  w  w  w. ja v a2 s .  c  o  m*/

    BufferedImage image = new BufferedImage(nWidth, nHeight, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = image.createGraphics();

    for (int i = 0; i < sText.length(); i++) {
        draw(String.valueOf(sText.charAt(i)), g2, i * nWidth / sText.length(), 0,
                (i + 1) * nWidth / sText.length(), nHeight);
    }

    g2.dispose();
    try {
        EncoderUtil.writeBufferedImage(image, ImageFormat.PNG, _os);
    } catch (Exception ex) {
        throw new PureException(PureException.UNKNOWN, "", ex);
    }
}

From source file:com.glaf.chart.util.ChartUtils.java

public static byte[] createChart(Chart chartModel, JFreeChart chart) {
    ByteArrayOutputStream baos = null;
    BufferedOutputStream bos = null;
    try {//www  .  j a v a  2 s.  com
        baos = new ByteArrayOutputStream();
        bos = new BufferedOutputStream(baos);
        java.awt.image.BufferedImage bi = chart.createBufferedImage(chartModel.getChartWidth(),
                chartModel.getChartHeight());

        if ("png".equalsIgnoreCase(chartModel.getImageType())) {
            EncoderUtil.writeBufferedImage(bi, chartModel.getImageType(), bos);
            ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
            ServletUtilities.saveChartAsPNG(chart, chartModel.getChartWidth(), chartModel.getChartHeight(),
                    info, null);
        } else if ("jpeg".equalsIgnoreCase(chartModel.getImageType())) {
            EncoderUtil.writeBufferedImage(bi, chartModel.getImageType(), bos);
            ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
            ServletUtilities.saveChartAsJPEG(chart, chartModel.getChartWidth(), chartModel.getChartHeight(),
                    info, null);
        }

        bos.flush();
        baos.flush();

        return baos.toByteArray();

    } catch (Exception ex) {
        throw new RuntimeException(ex);
    } finally {
        IOUtils.closeQuietly(baos);
        IOUtils.closeQuietly(bos);
    }
}

From source file:com.glaf.chart.util.ChartUtils.java

public static void createChart(String path, Chart chartModel, JFreeChart chart) {
    try {//from w w w .j  a  v a  2  s . co  m
        java.awt.image.BufferedImage bi = chart.createBufferedImage(chartModel.getChartWidth(),
                chartModel.getChartHeight());

        String name = chartModel.getChartName();
        if (StringUtils.isNotEmpty(chartModel.getMapping())) {
            name = chartModel.getMapping();
        }

        if ("png".equalsIgnoreCase(chartModel.getImageType())) {
            EncoderUtil.writeBufferedImage(bi, chartModel.getImageType(),
                    new FileOutputStream(path + "/" + name + ".png"));
            ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
            ServletUtilities.saveChartAsPNG(chart, chartModel.getChartWidth(), chartModel.getChartHeight(),
                    info, null);
        } else if ("jpeg".equalsIgnoreCase(chartModel.getImageType())) {
            EncoderUtil.writeBufferedImage(bi, chartModel.getImageType(),
                    new FileOutputStream(path + "/" + name + ".jpg"));
            ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
            ServletUtilities.saveChartAsJPEG(chart, chartModel.getChartWidth(), chartModel.getChartHeight(),
                    info, null);
        }

    } catch (Exception ex) {
        ex.printStackTrace();
        throw new RuntimeException(ex);
    }
}

From source file:com.pureinfo.srm.common.ImageHelper.java

public static void drawRectangle(Paint _color, Point _point, OutputStream _os) throws PureException {
    int nWidth = _point.x;
    int nHeight = _point.y;

    BufferedImage image = new BufferedImage(nWidth, nHeight, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = image.createGraphics();
    g2.setPaint(_color);/*  www. ja  v a  2s  . c o m*/
    g2.fillRect(0, 0, nWidth, nHeight);

    g2.dispose();
    try {
        EncoderUtil.writeBufferedImage(image, ImageFormat.PNG, _os);
    } catch (Exception ex) {
        throw new PureException(PureException.UNKNOWN, "", ex);
    }
}

From source file:edu.cudenver.bios.chartsvc.representation.LegendImageRepresentation.java

/**
 * Called internally by Restlet library to write the image as the HTTP
 * response.//from w ww.j a va2s .  co m
 * @param out output stream
 */
@Override
public void write(OutputStream out) throws IOException {
    // build the legend from the plot, and write it to a jpeg image
    if (plot != null) {
        LegendTitle legend = new LegendTitle(plot, new ColumnArrangement(), new ColumnArrangement());
        legend.setFrame(BlockBorder.NONE);
        //legend.setMargin(new RectangleInsets(2.0, 2.0, 2.0, 2.0));
        legend.setBackgroundPaint(Color.white);
        legend.setPosition(RectangleEdge.BOTTOM);
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = image.createGraphics();
        Rectangle2D.Double legendArea = new Rectangle2D.Double(0, 0, width, height);
        g.clip(legendArea);
        legend.arrange(g, new RectangleConstraint(width, height));
        legend.draw(g, legendArea);
        g.dispose();
        EncoderUtil.writeBufferedImage(image, ImageFormat.JPEG, out);
    }
}

From source file:table.FrequencyTablePanel.java

private void writeTableAsPNG(OutputStream out, JTable table, int width, int height) throws IOException {
    BufferedImage bufferedImage = createImage(table);
    EncoderUtil.writeBufferedImage(bufferedImage, ImageFormat.PNG, out);
}

From source file:org.kalypso.ogc.sensor.diagview.jfreechart.ExportableChart.java

/**
 * @see org.kalypso.metadoc.IExportableObject#exportObject(java.io.OutputStream, org.eclipse.core.runtime.IProgressMonitor)
 *//* w w  w  . j a v a2s . co m*/
@Override
public IStatus exportObject(final OutputStream outs, final IProgressMonitor monitor) {
    monitor.beginTask(Messages.getString("org.kalypso.ogc.sensor.diagview.jfreechart.ExportableChart.3"), //$NON-NLS-1$
            IProgressMonitor.UNKNOWN);

    try {
        final BufferedImage image = m_chart.createBufferedImage(m_width, m_height, null);
        EncoderUtil.writeBufferedImage(image, m_format, outs);
    } catch (final IOException e) {
        return StatusUtilities.statusFromThrowable(e,
                Messages.getString("org.kalypso.ogc.sensor.diagview.jfreechart.ExportableChart.4")); //$NON-NLS-1$
    } finally {
        monitor.done();
    }

    return Status.OK_STATUS;
}

From source file:org.gumtree.vis.core.internal.SWTChartComposite.java

public void paint(GC gc, int leftMargin, int topMargin, int rightMargin, int bottomMargin) {
    Transform t = new Transform(gc.getDevice());
    gc.getTransform(t);//from  w  w w .  j  ava  2 s .co  m

    float[] elements = new float[6];
    t.getElements(elements);
    Transform t2 = new Transform(gc.getDevice(), elements);

    // Apply transformation for printer resolution
    Point printerDPI = gc.getDevice().getDPI();
    Point screenDPI = getDisplay().getDPI();
    float scaleX = printerDPI.x / screenDPI.x * 1.5f;
    float scaleY = printerDPI.y / screenDPI.y * 1.5f;
    t2.scale(scaleX, scaleY);
    gc.setTransform(t2);

    SWTGraphics2D graphics2D = new SWTGraphics2D(gc);

    BufferedImage bufferedImage = getChart().createBufferedImage((int) (getBounds().width),
            (int) (getBounds().height), null);
    ImageData imageData = SWTUtils.convertToSWT(bufferedImage);

    try {
        OutputStream out = new BufferedOutputStream(
                new FileOutputStream(new File("D:/tools/quokka/resAwt.png")));
        EncoderUtil.writeBufferedImage(bufferedImage, ImageFormat.PNG, out);
    } catch (Exception e) {
        e.printStackTrace();
    }

    //      Graphics2D g2d = (Graphics2D) bufferedImage.getGraphics();

    //      getChart().draw(graphics2D,  new Rectangle((int) (leftMargin/scaleX), (int) (topMargin/scaleY), 
    //            (int) (getBounds().width/scaleX), (int) (getBounds().height/scaleY)));
    // Decrease rendering size
    //      Graphics2D g2d = (Graphics2D) graphics2D.create();
    //      GraphicsConfiguration configuration = g2d.getDeviceConfiguration();
    //      Image awtImage = configuration.createCompatibleImage(
    //            getBounds().width, getBounds().height,
    //                Transparency.TRANSLUCENT);
    //      ImageData imageData = SWTUtils.convertAWTImageToSWT(awtImage);
    //      org.eclipse.swt.graphics.Image swtImage = new org.eclipse.swt.graphics.Image(
    //            getDisplay(), imageData);

    ImageLoader imageLoader = new ImageLoader();
    imageLoader.data = new ImageData[] { imageData };
    int fileFormat = SWT.IMAGE_PNG;
    imageLoader.save("D:/tools/quokka/resSwt.png", fileFormat);
    //      double imageSizeFactor =
    //            Math.min(
    //              1,
    //              (margin.right - margin.left)
    //                * 1.0
    //                / (dpiScaleFactorX * imageWidth));
    //          imageSizeFactor =
    //            Math.min(
    //              imageSizeFactor,
    //              (margin.bottom - margin.top)
    //                * 1.0

    //        gc.drawImage(swtImage, 0, 0, swtImage.getBounds().width, swtImage.getBounds().height,
    //                leftMargin, topMargin,
    //                (int) printerDPI.x,
    //                (int) printerDPI.y);
    //       getChart().draw(graphics2D, new Rectangle((int) (x/scaleX), (int) (y/scaleY), 
    //            (int) (getBounds().width/scaleX), (int) (getBounds().height/scaleY)));
    graphics2D.dispose();

    // Restore original scaling
    //      gc.setTransform(t);
    System.out.println("chart draw finished");
}

From source file:org.gumtree.vis.mask.ChartMaskingUtilities.java

/**
 * Writes a chart to an output stream in JPEG format. This method allows
 * you to pass in a {@link ChartRenderingInfo} object, to collect
 * information about the chart dimensions/entities.  You will need this
 * info if you want to create an HTML image map.
 *
 * @param out  the output stream (<code>null</code> not permitted).
 * @param chart  the chart (<code>null</code> not permitted).
 * @param width  the image width./*from   www  .  j  a  v a 2s . c  om*/
 * @param height  the image height.
 * @param info  the chart rendering info (<code>null</code> permitted).
 * @param shapeMap 
 *
 * @throws IOException if there are any I/O errors.
 */
public static void writeChartAsJPEG(File file, JFreeChart chart, int width, int height, ChartRenderingInfo info,
        Rectangle2D imageArea, LinkedHashMap<AbstractMask, Color> maskList,
        LinkedHashMap<Shape, Color> shapeMap, LinkedHashMap<Rectangle2D, String> textContentMap)
        throws IOException {

    if (file == null) {
        throw new IllegalArgumentException("Null 'file' argument.");
    }
    if (chart == null) {
        throw new IllegalArgumentException("Null 'chart' argument.");
    }
    OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
    BufferedImage image = chart.createBufferedImage(width, height, BufferedImage.TYPE_INT_RGB, info);
    Graphics2D g2 = image.createGraphics();
    drawMasks(g2, imageArea, maskList, null, chart);
    drawShapes(g2, imageArea, shapeMap, chart);
    drawText(g2, imageArea, textContentMap, chart);
    g2.dispose();
    try {
        EncoderUtil.writeBufferedImage(image, ImageFormat.JPEG, out);
    } finally {
        out.close();
    }
}