Skip to content
Snippets Groups Projects
Commit 27bc77fc authored by timmolter's avatar timmolter
Browse files

added jpeg saving

parent 8d5d8ae7
No related branches found
No related tags found
No related merge requests found
...@@ -17,13 +17,22 @@ package com.xeiam.xchart; ...@@ -17,13 +17,22 @@ package com.xeiam.xchart;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.Iterator;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.FileImageOutputStream;
/** /**
* A helper class with static methods for saving Charts as bitmaps
*
* @author timmolter * @author timmolter
*/ */
public class BitmapEncoder { public class BitmapEncoder {
...@@ -36,22 +45,51 @@ public class BitmapEncoder { ...@@ -36,22 +45,51 @@ public class BitmapEncoder {
} }
/** /**
* Saves a chart as a PNG file * Save a Chart as a PNG file
* *
* @param chart * @param chart
* @param pFileName * @param fileName
* @throws IOException * @throws IOException
*/ */
public static void savePNG(Chart chart, String pFileName) throws IOException { public static void savePNG(Chart chart, String fileName) throws IOException {
BufferedImage lBufferedImage = new BufferedImage(chart.width, chart.height, BufferedImage.TYPE_INT_RGB); BufferedImage bufferedImage = new BufferedImage(chart.width, chart.height, BufferedImage.TYPE_INT_RGB);
Graphics2D lGraphics2D = lBufferedImage.createGraphics(); Graphics2D lGraphics2D = bufferedImage.createGraphics();
chart.paint(lGraphics2D); chart.paint(lGraphics2D);
// Save chart as PNG // Save chart as PNG
OutputStream out = new FileOutputStream(pFileName); OutputStream out = new FileOutputStream(fileName);
ImageIO.write(lBufferedImage, "png", out); ImageIO.write(bufferedImage, "png", out);
out.close(); out.close();
} }
/**
* Save a Chart as a JPEG file
*
* @param chart
* @param fileName
* @param quality - // a float between 0 and 1 (1 = maximum quality)
* @throws FileNotFoundException
* @throws IOException
*/
public static void saveJPG(Chart chart, String fileName, float quality) throws FileNotFoundException, IOException {
BufferedImage bufferedImage = new BufferedImage(chart.width, chart.height, BufferedImage.TYPE_INT_RGB);
Graphics2D lGraphics2D = bufferedImage.createGraphics();
chart.paint(lGraphics2D);
Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpeg");
ImageWriter writer = iter.next();
// instantiate an ImageWriteParam object with default compression options
ImageWriteParam iwp = writer.getDefaultWriteParam();
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwp.setCompressionQuality(quality);
File file = new File(fileName);
FileImageOutputStream output = new FileImageOutputStream(file);
writer.setOutput(output);
IIOImage image = new IIOImage(bufferedImage, null, null);
writer.write(null, image, iwp);
writer.dispose();
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment