/** * Write a Chart into a given stream. Does not close the target stream automatically at the end of * the operation * * @param chart * @param targetStream * @param bitmapFormat * @throws IOException */ public static void saveBitmap(Chart chart, OutputStream targetStream, BitmapFormat bitmapFormat) throws IOException { BufferedImage bufferedImage = getBufferedImage(chart); ImageIO.write(bufferedImage, bitmapFormat.toString().toLowerCase(), targetStream); }
/** * Generates a byte[] for a given chart * * @param chart * @return a byte[] for a given chart * @throws IOException */ public static byte[] getBitmapBytes(Chart chart, BitmapFormat bitmapFormat) throws IOException { BufferedImage bufferedImage = getBufferedImage(chart); byte[] imageInBytes; ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(bufferedImage, bitmapFormat.toString().toLowerCase(), baos); try { baos.flush(); imageInBytes = baos.toByteArray(); } finally { baos.close(); } return imageInBytes; }
/** * Save list of Charts into a given stream. Does not close the target stream automatically at the * end of the operation. Function assumes that all charts are the same size (width, height). * Number of charts should equal rows multiplied by cols. * * @param charts * @param rows number of rows * @param cols number of columns * @param targetStream * @param bitmapFormat * @throws IOException */ public static void saveBitmap( List<Chart> charts, Integer rows, Integer cols, OutputStream targetStream, BitmapEncoder.BitmapFormat bitmapFormat) throws IOException { List<BufferedImage> chartImages = new LinkedList<BufferedImage>(); for (Chart c : charts) chartImages.add(getBufferedImage(c)); BufferedImage bufferedImage = mergeImages(chartImages, rows, cols); ImageIO.write(bufferedImage, bitmapFormat.toString().toLowerCase(), targetStream); }
/** * Save a Chart as a JPEG file * * @param chart * @param fileName * @param quality - a float between 0 and 1 (1 = maximum quality) * @throws IOException */ public static void saveJPGWithQuality(Chart chart, String fileName, float quality) throws IOException { BufferedImage bufferedImage = getBufferedImage(chart); 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); try { writer.setOutput(output); IIOImage image = new IIOImage(bufferedImage, null, null); writer.write(null, image, iwp); writer.dispose(); } finally { output.close(); } }
/** * Write a Chart into a given stream. Does not close the target stream automatically at the end of * the operation * * @param chart * @param targetStream * @param bitmapFormat * @throws IOException */ public static void saveBitmap(Chart chart, OutputStream targetStream, BitmapFormat bitmapFormat) throws IOException { BufferedImage bufferedImage = getBufferedImage(chart); ImageIO.write(bufferedImage, bitmapFormat.toString().toLowerCase(), targetStream); }
/** * Generates a byte[] for a given chart * * @param chart * @return a byte[] for a given chart * @throws IOException */ public static byte[] getBitmapBytes(Chart chart, BitmapFormat bitmapFormat) throws IOException { BufferedImage bufferedImage = getBufferedImage(chart); byte[] imageInBytes; ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(bufferedImage, bitmapFormat.toString().toLowerCase(), baos); try { baos.flush(); imageInBytes = baos.toByteArray(); } finally { baos.close(); } return imageInBytes; }
/** * Save list of Charts into a given stream. Does not close the target stream automatically at the * end of the operation. Function assumes that all charts are the same size (width, height). * Number of charts should equal rows multiplied by cols. * * @param charts * @param rows number of rows * @param cols number of columns * @param targetStream * @param bitmapFormat * @throws IOException */ public static void saveBitmap( List<Chart> charts, Integer rows, Integer cols, OutputStream targetStream, BitmapEncoder.BitmapFormat bitmapFormat) throws IOException { List<BufferedImage> chartImages = new LinkedList<BufferedImage>(); for (Chart c : charts) chartImages.add(getBufferedImage(c)); BufferedImage bufferedImage = mergeImages(chartImages, rows, cols); ImageIO.write(bufferedImage, bitmapFormat.toString().toLowerCase(), targetStream); }
/** * Save a Chart as a JPEG file * * @param chart * @param fileName * @param quality - a float between 0 and 1 (1 = maximum quality) * @throws IOException */ public static void saveJPGWithQuality(Chart chart, String fileName, float quality) throws IOException { BufferedImage bufferedImage = getBufferedImage(chart); 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); try { writer.setOutput(output); IIOImage image = new IIOImage(bufferedImage, null, null); writer.write(null, image, iwp); writer.dispose(); } finally { output.close(); } }