njrfrens Posted March 2, 2009 Share Posted March 2, 2009 How can I export the chart output of .jasper result as an image?We have exporters like JRHtmlExporter,pdf,xls,xml etc. but couldn't find any exporter for image. Why is it so?if not image, atleast can I get the byte array of the result of .jasper? so that I'll convert the byte stream to image... Link to comment Share on other sites More sharing options...
kennethmch Posted March 2, 2009 Share Posted March 2, 2009 Assuming you already have the jasperprint object Code: response.setContentType("image/jpeg"); BufferedImage pageImage = new BufferedImage(jasperPrint.getPageWidth() + 1, jasperPrint.getPageHeight() + 1, BufferedImage.TYPE_INT_RGB); exporter = new JRGraphics2DExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); Map imagesMap = new HashMap(); request.getSession().setAttribute("IMAGES_MAP", imagesMap); exporter.setParameter(JRHtmlExporterParameter.IS_OUTPUT_IMAGES_TO_DIR, Boolean.FALSE); exporter.setParameter(JRHtmlExporterParameter.IMAGES_MAP, imagesMap); exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, context+"/images?image="); exporter.setParameter(JRGraphics2DExporterParameter.GRAPHICS_2D, pageImage.getGraphics()); exporter.setParameter(JRExporterParameter.PAGE_INDEX, new Integer(0)); exporter.exportReport(); output = bufferedImageToByteArray(pageImage); public static byte[] bufferedImageToByteArray(BufferedImage img) throws IOException{ ByteArrayOutputStream os = new ByteArrayOutputStream(); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os); encoder.encode(img); return os.toByteArray(); } Link to comment Share on other sites More sharing options...
njrfrens Posted March 3, 2009 Author Share Posted March 3, 2009 Thanks a lot... It worked for me Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now