Skip to content
Snippets Groups Projects
Commit b182cee6 authored by Tim Molter's avatar Tim Molter
Browse files

added gif and bmp export, refactored BitmapEncoder

parent abdbde95
No related branches found
No related tags found
No related merge requests found
...@@ -7,3 +7,5 @@ bin/ ...@@ -7,3 +7,5 @@ bin/
.DS_Store .DS_Store
*.png *.png
*.jpg *.jpg
*.bmp
*.gif
\ No newline at end of file
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
package com.xeiam.xchart.standalone; package com.xeiam.xchart.standalone;
import com.xeiam.xchart.BitmapEncoder; import com.xeiam.xchart.BitmapEncoder;
import com.xeiam.xchart.BitmapEncoder.BitmapFormat;
import com.xeiam.xchart.Chart; import com.xeiam.xchart.Chart;
import com.xeiam.xchart.Series; import com.xeiam.xchart.Series;
import com.xeiam.xchart.SeriesMarker; import com.xeiam.xchart.SeriesMarker;
...@@ -37,9 +38,15 @@ public class Example1 { ...@@ -37,9 +38,15 @@ public class Example1 {
Series series = chart.addSeries("y(x)", null, yData); Series series = chart.addSeries("y(x)", null, yData);
series.setMarker(SeriesMarker.CIRCLE); series.setMarker(SeriesMarker.CIRCLE);
BitmapEncoder.savePNG(chart, "./Sample_Chart.png"); BitmapEncoder.saveBitmap(chart, "./Sample_Chart", BitmapFormat.PNG);
BitmapEncoder.savePNGWithDPI(chart, "./Sample_Chart_300_DPI.png", 300); BitmapEncoder.saveBitmap(chart, "./Sample_Chart", BitmapFormat.JPG);
BitmapEncoder.saveJPG(chart, "./Sample_Chart.jpg", 0.95f); BitmapEncoder.saveJPGWithQuality(chart, "./Sample_Chart_With_Quality.jpg", 0.95f);
BitmapEncoder.saveBitmap(chart, "./Sample_Chart", BitmapFormat.BMP);
BitmapEncoder.saveBitmap(chart, "./Sample_Chart", BitmapFormat.GIF);
BitmapEncoder.saveBitmapWithDPI(chart, "./Sample_Chart_300_DPI", BitmapFormat.PNG, 300);
BitmapEncoder.saveBitmapWithDPI(chart, "./Sample_Chart_300_DPI", BitmapFormat.JPG, 300);
BitmapEncoder.saveBitmapWithDPI(chart, "./Sample_Chart_300_DPI", BitmapFormat.GIF, 300);
} }
} }
...@@ -50,20 +50,24 @@ public final class BitmapEncoder { ...@@ -50,20 +50,24 @@ public final class BitmapEncoder {
} }
public enum BitmapFormat {
PNG, JPG, BMP, GIF;
}
/** /**
* Save a Chart as a PNG file * Save a Chart as an image file
* *
* @param chart * @param chart
* @param fileName * @param fileName
* @param bitmapFormat
* @throws IOException * @throws IOException
*/ */
public static void savePNG(Chart chart, String fileName) throws IOException { public static void saveBitmap(Chart chart, String fileName, BitmapFormat bitmapFormat) throws IOException {
BufferedImage bufferedImage = getBufferedImage(chart); BufferedImage bufferedImage = getBufferedImage(chart);
// Save chart as PNG OutputStream out = new FileOutputStream(fileName + "." + bitmapFormat.toString().toLowerCase());
OutputStream out = new FileOutputStream(fileName); ImageIO.write(bufferedImage, bitmapFormat.toString().toLowerCase(), out);
ImageIO.write(bufferedImage, "png", out);
out.close(); out.close();
} }
...@@ -75,7 +79,7 @@ public final class BitmapEncoder { ...@@ -75,7 +79,7 @@ public final class BitmapEncoder {
* @param DPI * @param DPI
* @throws IOException * @throws IOException
*/ */
public static void savePNGWithDPI(Chart chart, String fileName, int DPI) throws IOException { public static void saveBitmapWithDPI(Chart chart, String fileName, BitmapFormat bitmapFormat, int DPI) throws IOException {
double scaleFactor = DPI / 72.0; double scaleFactor = DPI / 72.0;
...@@ -88,27 +92,26 @@ public final class BitmapEncoder { ...@@ -88,27 +92,26 @@ public final class BitmapEncoder {
graphics2D.setTransform(at); graphics2D.setTransform(at);
chart.paint(graphics2D, chart.getWidth(), chart.getHeight()); chart.paint(graphics2D, chart.getWidth(), chart.getHeight());
Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName(bitmapFormat.toString().toLowerCase());
for (Iterator<ImageWriter> iw = ImageIO.getImageWritersByFormatName("png"); iw.hasNext();) { if (writers.hasNext()) {
ImageWriter writer = iw.next(); ImageWriter writer = writers.next();
// instantiate an ImageWriteParam object with default compression options // instantiate an ImageWriteParam object with default compression options
ImageWriteParam iwp = writer.getDefaultWriteParam(); ImageWriteParam iwp = writer.getDefaultWriteParam();
ImageTypeSpecifier typeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB); ImageTypeSpecifier typeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB);
IIOMetadata metadata = writer.getDefaultImageMetadata(typeSpecifier, iwp); IIOMetadata metadata = writer.getDefaultImageMetadata(typeSpecifier, iwp);
if (metadata.isReadOnly() || !metadata.isStandardMetadataFormatSupported()) { if (metadata.isReadOnly() || !metadata.isStandardMetadataFormatSupported()) {
continue; throw new IllegalArgumentException("It is not possible to set the DPI on a bitmap with " + bitmapFormat + " format!! Try another format.");
} }
setDPIforPNG(metadata, DPI); setDPI(metadata, DPI);
File file = new File(fileName); File file = new File(fileName + "." + bitmapFormat.toString().toLowerCase());
FileImageOutputStream output = new FileImageOutputStream(file); FileImageOutputStream output = new FileImageOutputStream(file);
writer.setOutput(output); writer.setOutput(output);
IIOImage image = new IIOImage(bufferedImage, null, metadata); IIOImage image = new IIOImage(bufferedImage, null, metadata);
writer.write(null, image, iwp); writer.write(null, image, iwp);
writer.dispose(); writer.dispose();
break;
} }
} }
...@@ -119,7 +122,7 @@ public final class BitmapEncoder { ...@@ -119,7 +122,7 @@ public final class BitmapEncoder {
* @param DPI * @param DPI
* @throws IIOInvalidTreeException * @throws IIOInvalidTreeException
*/ */
private static void setDPIforPNG(IIOMetadata metadata, int DPI) throws IIOInvalidTreeException { private static void setDPI(IIOMetadata metadata, int DPI) throws IIOInvalidTreeException {
// for PNG, it's dots per millimeter // for PNG, it's dots per millimeter
double dotsPerMilli = 1.0 * DPI / 10 / 2.54; double dotsPerMilli = 1.0 * DPI / 10 / 2.54;
...@@ -149,7 +152,7 @@ public final class BitmapEncoder { ...@@ -149,7 +152,7 @@ public final class BitmapEncoder {
* @throws FileNotFoundException * @throws FileNotFoundException
* @throws IOException * @throws IOException
*/ */
public static void saveJPG(Chart chart, String fileName, float quality) throws FileNotFoundException, IOException { public static void saveJPGWithQuality(Chart chart, String fileName, float quality) throws FileNotFoundException, IOException {
BufferedImage bufferedImage = getBufferedImage(chart); BufferedImage bufferedImage = getBufferedImage(chart);
...@@ -174,14 +177,14 @@ public final class BitmapEncoder { ...@@ -174,14 +177,14 @@ public final class BitmapEncoder {
* @return a byte[] for a given chart, PNG compressed * @return a byte[] for a given chart, PNG compressed
* @throws IOException * @throws IOException
*/ */
public static byte[] getPNGBytes(Chart chart) throws IOException { public static byte[] getBitmapBytes(Chart chart, BitmapFormat bitmapFormat) throws IOException {
BufferedImage bufferedImage = getBufferedImage(chart); BufferedImage bufferedImage = getBufferedImage(chart);
byte[] imageInBytes = null; byte[] imageInBytes = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "png", baos); ImageIO.write(bufferedImage, bitmapFormat.toString().toLowerCase(), baos);
baos.flush(); baos.flush();
imageInBytes = baos.toByteArray(); imageInBytes = baos.toByteArray();
baos.close(); baos.close();
......
...@@ -38,6 +38,8 @@ import javax.swing.JPopupMenu; ...@@ -38,6 +38,8 @@ import javax.swing.JPopupMenu;
import javax.swing.KeyStroke; import javax.swing.KeyStroke;
import javax.swing.filechooser.FileFilter; import javax.swing.filechooser.FileFilter;
import com.xeiam.xchart.BitmapEncoder.BitmapFormat;
/** /**
* A Swing JPanel that contains a Chart * A Swing JPanel that contains a Chart
* <p> * <p>
...@@ -111,22 +113,33 @@ public class XChartPanel extends JPanel { ...@@ -111,22 +113,33 @@ public class XChartPanel extends JPanel {
JFileChooser fileChooser = new JFileChooser(); JFileChooser fileChooser = new JFileChooser();
fileChooser.addChoosableFileFilter(new JPGSaveFilter()); fileChooser.addChoosableFileFilter(new JPGSaveFilter());
fileChooser.addChoosableFileFilter(new PNGSaveFilter()); FileFilter pngFileFilter = new PNGSaveFilter();
fileChooser.addChoosableFileFilter(pngFileFilter);
fileChooser.addChoosableFileFilter(new BMPSaveFilter());
fileChooser.addChoosableFileFilter(new GIFSaveFilter());
fileChooser.setAcceptAllFileFilterUsed(false); fileChooser.setAcceptAllFileFilterUsed(false);
fileChooser.setFileFilter(pngFileFilter);
if (fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) { if (fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
if (fileChooser.getSelectedFile() != null) { if (fileChooser.getSelectedFile() != null) {
File theFileToSave = fileChooser.getSelectedFile(); File theFileToSave = fileChooser.getSelectedFile();
try { try {
if (fileChooser.getFileFilter() == null) { if (fileChooser.getFileFilter() == null) {
BitmapEncoder.savePNG(chart, theFileToSave.getCanonicalPath().toString()); BitmapEncoder.saveBitmap(chart, theFileToSave.getCanonicalPath().toString(), BitmapFormat.PNG);
} }
else if (fileChooser.getFileFilter().getDescription().equals("*.jpg,*.JPG")) { else if (fileChooser.getFileFilter().getDescription().equals("*.jpg,*.JPG")) {
BitmapEncoder.saveJPG(chart, theFileToSave.getCanonicalPath().toString() + ".jpg", 1.0f); BitmapEncoder.saveJPGWithQuality(chart, theFileToSave.getCanonicalPath().toString() + ".jpg", 1.0f);
} }
else if (fileChooser.getFileFilter().getDescription().equals("*.png,*.PNG")) { else if (fileChooser.getFileFilter().getDescription().equals("*.png,*.PNG")) {
BitmapEncoder.savePNG(chart, theFileToSave.getCanonicalPath().toString() + ".png"); BitmapEncoder.saveBitmap(chart, theFileToSave.getCanonicalPath().toString(), BitmapFormat.PNG);
}
else if (fileChooser.getFileFilter().getDescription().equals("*.bmp,*.BMP")) {
BitmapEncoder.saveBitmap(chart, theFileToSave.getCanonicalPath().toString(), BitmapFormat.BMP);
}
else if (fileChooser.getFileFilter().getDescription().equals("*.gif,*.GIF")) {
BitmapEncoder.saveBitmap(chart, theFileToSave.getCanonicalPath().toString(), BitmapFormat.GIF);
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
...@@ -158,6 +171,50 @@ public class XChartPanel extends JPanel { ...@@ -158,6 +171,50 @@ public class XChartPanel extends JPanel {
} }
private class BMPSaveFilter extends FileFilter {
@Override
public boolean accept(File f) {
if (f.isDirectory()) {
return false;
}
String s = f.getName();
return s.endsWith(".bmp") || s.endsWith(".BMP");
}
@Override
public String getDescription() {
return "*.bmp,*.BMP";
}
}
private class GIFSaveFilter extends FileFilter {
@Override
public boolean accept(File f) {
if (f.isDirectory()) {
return false;
}
String s = f.getName();
return s.endsWith(".gif") || s.endsWith(".GIF");
}
@Override
public String getDescription() {
return "*.gif,*.GIF";
}
}
private class PNGSaveFilter extends FileFilter { private class PNGSaveFilter extends FileFilter {
@Override @Override
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment