Skip to content
Browse files

Fix Image Exporter

  • Loading branch information...
1 parent c6fe4f9 commit f7ce5663a0922d9fbe31362a081598466d838062 @wpivotto committed Nov 7, 2011
Showing with 33 additions and 37 deletions.
  1. +33 −37 src/main/java/br/com/caelum/vraptor/jasperreports/formats/Image.java
View
70 src/main/java/br/com/caelum/vraptor/jasperreports/formats/Image.java
@@ -23,11 +23,11 @@
@Component
public class Image extends AbstractExporter {
-
+
private Float zoom = 1f;
private Integer page = 0;
private String format = "png";
-
+
public String getContentType() {
return "image/" + format;
}
@@ -43,68 +43,64 @@ public JRExporter setup() {
throw new RuntimeException(e);
}
}
-
- public Image zoom(Float zoom){
+
+ public Image zoom(Float zoom) {
this.zoom = zoom;
return this;
}
-
- public Image page(Integer page){
+
+ public Image page(Integer page) {
this.page = page;
return this;
}
-
- public Image format(String format){
- this.format = format;
+
+ public Image png() {
+ this.format = "png";
return this;
}
-
- public Image png(){
- return format("png");
- }
-
- public Image jpeg(){
- return format("jpeg");
+
+ public Image jpeg() {
+ this.format = "jpeg";
+ return this;
}
-
+
public boolean supportsBatchMode() {
return false;
}
-
+
public byte[] toByteArray(List<JasperPrint> print) {
-
+
ByteArrayOutputStream output = new ByteArrayOutputStream();
-
+
try {
-
+
JRExporter exporter = setup();
exporter.setParameters(parameters);
exporter.setParameter(JRExporterParameter.JASPER_PRINT, print.get(0));
-
+
BufferedImage image = getPageImage(print.get(0));
- exporter.setParameter(JRGraphics2DExporterParameter.GRAPHICS_2D, image.getGraphics());
- exporter.setParameter(JRGraphics2DExporterParameter.ZOOM_RATIO, zoom);
- exporter.setParameter(JRExporterParameter.PAGE_INDEX, page);
- exporter.exportReport();
-
- ImageIO.write(image, "png", output);
- return output.toByteArray();
-
+ exporter.setParameter(JRGraphics2DExporterParameter.GRAPHICS_2D, image.getGraphics());
+ exporter.setParameter(JRGraphics2DExporterParameter.ZOOM_RATIO, zoom);
+ exporter.setParameter(JRExporterParameter.PAGE_INDEX, page);
+ exporter.exportReport();
+
+ ImageIO.write(image, format, output);
+ return output.toByteArray();
+
} catch (JRException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException("Unable to convert image to stream", e);
- }
- finally {
+ } finally {
Closeables.closeQuietly(output);
parameters.clear();
}
}
-
- private BufferedImage getPageImage(JasperPrint print){
+
+ private BufferedImage getPageImage(JasperPrint print) {
int width = (int) (print.getPageWidth() * zoom);
- int height = (int) (print.getPageHeight() * zoom);
- return new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
+ int height = (int) (print.getPageHeight() * zoom);
+ return new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
}
-
+
}

0 comments on commit f7ce566

Please sign in to comment.
Something went wrong with that request. Please try again.