1 23 24 package de.progra.charting; 25 26 import javax.imageio.ImageIO ; 27 import java.awt.image.BufferedImage ; 28 import java.io.OutputStream ; 29 import java.io.IOException ; 30 import java.awt.Rectangle ; 31 import java.awt.Graphics2D ; 32 33 46 public class ChartEncoder { 47 48 53 public static void createJPEG(OutputStream os, Chart chart) throws EncodingException { 54 boolean success = true; 55 try { 56 Rectangle r = chart.getBounds(); 57 BufferedImage img = new BufferedImage ((int)r.getWidth(), 58 (int)r.getHeight(), 59 BufferedImage.TYPE_INT_RGB); 60 61 Graphics2D grafx = img.createGraphics(); 62 chart.render(grafx); 63 success = ImageIO.write(img, "jpeg", os); 64 os.flush(); 65 } catch(Throwable t) { 66 throw new EncodingException(t.getMessage(), t); 67 } 68 69 if(!success) 70 throw new EncodingException("No ImageWriter for writing JPEGs found."); 71 } 72 73 79 public static void createGIF(OutputStream os, Chart chart) throws EncodingException { 80 } 81 82 87 public static void createPNG(OutputStream os, Chart chart) throws EncodingException { 88 boolean success = true; 89 try { 90 Rectangle r = chart.getBounds(); 91 BufferedImage img = new BufferedImage ((int)r.getWidth(), 92 (int)r.getHeight(), 93 BufferedImage.TYPE_INT_RGB); 94 95 Graphics2D grafx = img.createGraphics(); 96 chart.render(grafx); 97 success = ImageIO.write(img, "png", os); 98 os.flush(); 99 } catch(Throwable t) { 100 t.printStackTrace(); 101 throw new EncodingException(t.getMessage(), t); 102 } 103 104 if(!success) 105 throw new EncodingException("No ImageWriter for writing PNGs found."); 106 } 107 108 114 public static void createEncodedImage(OutputStream os, Chart chart, String format) throws EncodingException { 115 boolean success = true; 116 try { 117 Rectangle r = chart.getBounds(); 118 BufferedImage img = new BufferedImage ((int)r.getWidth(), 119 (int)r.getHeight(), 120 BufferedImage.TYPE_INT_RGB); 121 122 Graphics2D grafx = img.createGraphics(); 123 chart.render(grafx); 124 success = ImageIO.write(img, format, os); 125 os.flush(); 126 } catch(Throwable t) { 127 throw new EncodingException(t.getMessage(), t); 128 } 129 130 if(!success) 131 throw new EncodingException("No ImageWriter for writing "+format+" images found."); 132 } 133 134 137 public static String [] getSupportedFormats() { 138 return ImageIO.getWriterFormatNames(); 139 } 140 } 141 | Popular Tags |