private byte[] getByteArray() { if (ba == null) { ba = BatikImageRenderer.getByteArray(document, width, ImageFormat.PNG); } return ba; }
/** * Renders a png image from the SVG document. * @param document * @param width * @param imageFormat The image file format of the output. * @return */ public static byte[] getByteArray(final Document document, final int width, final ImageFormat imageFormat) { if (imageFormat.isIn(ImageFormat.PNG, ImageFormat.JPEG) == true) { return getRasterImageByteArray(document, width, imageFormat); } if (imageFormat == ImageFormat.PDF == true) { return getPDFByteArray(document, width); } if (imageFormat == ImageFormat.SVG == true) { return getSVGByteArray(document, width); } throw new UnsupportedOperationException("Image type '" + imageFormat + "' not yet supported."); }
if (imageFormat != null) { final Document document = ganttChart.create(); content = BatikImageRenderer.getByteArray(document, ganttChart.getWidth(), imageFormat); DownloadUtils.setDownloadTarget(content, filename); } else {
@Test public void createImage() throws IOException { final GanttTask root = createTestChart(); final GanttChartStyle style = new GanttChartStyle(); final GanttChartSettings settings = new GanttChartSettings(); final GanttChart diagram = new GanttChart(root, style, settings, "test-chart"); writeFile("ganttTest.jpg", BatikImageRenderer.getByteArray(diagram.create(), 800, ImageFormat.JPEG)); writeFile("ganttTest.png", BatikImageRenderer.getByteArray(diagram.create(), 800, ImageFormat.PNG)); writeFile("ganttTest.svg", BatikImageRenderer.getByteArray(diagram.create(), 800, ImageFormat.SVG)); writeFile("ganttTest.pdf", BatikImageRenderer.getByteArray(diagram.create(), 800, ImageFormat.PDF)); }
@Test public void test() { final GanttChartStyle style = new GanttChartStyle(); style.setWidth(800); final double height = 1000; final Document doc = SVGHelper.createDocument(style.getWidth(), height); final Element root = doc.getDocumentElement(); int row = -2; drawBothLabelBars(doc, root, row += 2, date(2010, Calendar.JANUARY, 1), date(2010, Calendar.JANUARY, 31), style); drawBothLabelBars(doc, root, row += 2, date(2010, Calendar.JANUARY, 1), date(2010, Calendar.MAY, 31), style); drawBothLabelBars(doc, root, row += 2, date(2010, Calendar.JANUARY, 1), date(2010, Calendar.JULY, 31), style); drawBothLabelBars(doc, root, row += 2, date(2010, Calendar.JANUARY, 1), date(2010, Calendar.OCTOBER, 31), style); drawBothLabelBars(doc, root, row += 2, date(2010, Calendar.JANUARY, 1), date(2010, Calendar.DECEMBER, 31), style); drawBothLabelBars(doc, root, row += 2, date(2010, Calendar.JANUARY, 1), date(2012, Calendar.DECEMBER, 31), style); drawBothLabelBars(doc, root, row += 2, date(2010, Calendar.JANUARY, 1), date(2015, Calendar.DECEMBER, 31), style); drawBothLabelBars(doc, root, row += 2, date(2010, Calendar.JANUARY, 1), date(2018, Calendar.DECEMBER, 31), style); final byte[] ba = BatikImageRenderer.getByteArray(doc, style.getWidth(), ImageFormat.PNG); final File file = WorkFileHelper.getWorkFile("ganttXBarTest.png"); log.info("Writing Gantt test image to work directory: " + file.getAbsolutePath()); try { final FileOutputStream out = new FileOutputStream(file); out.write(ba); out.close(); } catch (final FileNotFoundException ex) { log.error("Exception encountered " + ex, ex); } catch (final IOException ex) { log.error("Exception encountered " + ex, ex); } }