Example usage for org.jfree.chart.encoders SunPNGEncoderAdapter SunPNGEncoderAdapter

List of usage examples for org.jfree.chart.encoders SunPNGEncoderAdapter SunPNGEncoderAdapter

Introduction

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

Prototype

SunPNGEncoderAdapter

Source Link

Usage

From source file:bzstats.chart.AbstractChartFactory.java

/**
 * Utility function to save an image as a PNG file.
 *
 * @param image//from   w  w  w.  j  a  v a  2 s .c  o  m
 *            The image to save.
 * @param filename
 *            The filename to save the image as.
 * @throws BZStatsException
 *             If the file could not be written.
 */
public static final void savePNGImage(BufferedImage image, String filename) throws BzStatsException {

    File outputfile = new File(filename);

    // overwrite
    if (outputfile.exists()) {
        if (outputfile.canWrite()) {
            outputfile.delete();
        } else {
            throw new BzStatsException("Cannot overwrite " + filename);
        }
    }

    boolean filecreated;
    try {
        filecreated = outputfile.createNewFile();
    } catch (IOException e2) {
        throw new BzStatsException("Failed to create file " + filename);
    }

    if (filecreated) {

        OutputStream out = null;
        try {
            out = new FileOutputStream(outputfile);
        } catch (FileNotFoundException e3) {
            throw new BzStatsException("Failed to write to file " + filename);
        }

        SunPNGEncoderAdapter encoder = new SunPNGEncoderAdapter();

        try {
            encoder.encode(image, out);
        } catch (IOException e) {
            throw new BzStatsException("Failed to encode file " + filename);
        } finally {
            try {
                out.close();
            } catch (IOException e1) {
                throw new BzStatsException("Failed to close file " + filename);
            }
        }
    } else {
        throw new BzStatsException("Failed to create file " + filename);
    }

}

From source file:org.efs.openreports.actions.ReportViewerAction.java

public String execute() {
    JasperPrint jasperPrint = (JasperPrint) ActionContext.getContext().getSession().get(ORStatics.JASPERPRINT);

    report = (Report) ActionContext.getContext().getSession().get(ORStatics.REPORT);

    if (jasperPrint != null && jasperPrint.getPages() != null) {
        pageCount = jasperPrint.getPages().size();
    }//from ww w  .j  a v  a2  s . co m

    if (!"image".equals(submitType))
        return SUCCESS;

    byte[] imageData = null;

    if (jasperPrint != null) {
        try {
            BufferedImage image = (BufferedImage) JasperPrintManager.printPageToImage(jasperPrint,
                    pageIndex - 1, zoom);
            imageData = new SunPNGEncoderAdapter().encode(image);
        } catch (Exception e) {
            addActionError(e.getMessage());
            log.error(e.toString());
        }
    }

    if (imageData != null) {

        HttpServletResponse response = ServletActionContext.getResponse();

        try {
            response.setContentLength(imageData.length);
            ServletOutputStream ouputStream = response.getOutputStream();
            ouputStream.write(imageData, 0, imageData.length);
            ouputStream.flush();
            ouputStream.close();
        } catch (IOException ioe) {
            log.warn(ioe.toString());
        }
    }

    return NONE;
}