GraphicImage is capable of presenting images that are created programatically at runtime or images stored in a database.
<div class="card">
<h5>Static Image</h5>
<p:graphicImage name="images/nature/nature4.jpg" library="demo"/>
</div>
<div class="card">
<h5>JFreeChart</h5>
<p:graphicImage value="#{graphicImageView.chart}"/>
<p:graphicImage value="#{graphicImageView.chartAsByteArray}"/>
<p:graphicImage value="#{graphicImageView.chartAsStream}"/>
</div>
<div class="card">
<h5>JFreeChart rendered as DataURI / ViewScoped support</h5>
<p:graphicImage value="#{graphicImageView.chart}" stream="false"/>
<p:graphicImage value="#{graphicImageView.chartAsByteArray}" stream="false"/>
<p:graphicImage value="#{graphicImageView.chartAsStream}" stream="false"/>
</div>
<div class="card">
<h5>JFreeChart optimized without buffering</h5>
<p:graphicImage value="#{graphicImageView.chartWithoutBuffering}"/>
<p:graphicImage value="#{graphicImageView.chartWithoutBuffering}" stream="false"/>
</div>
<div class="card">
<h5>GraphicText</h5>
<p:graphicImage value="#{graphicImageView.graphicText}"/>
</div>
@Named
@RequestScoped
public class GraphicImageView {
public StreamedContent getGraphicText() {
try {
return DefaultStreamedContent.builder()
.contentType("image/png")
.stream(() -> {
try {
BufferedImage bufferedImg = new BufferedImage(100, 25, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = bufferedImg.createGraphics();
g2.drawString("This is a text", 0, 10);
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(bufferedImg, "png", os);
return new ByteArrayInputStream(os.toByteArray());
}
catch (Exception e) {
e.printStackTrace();
return null;
}
})
.build();
}
catch (Exception e) {
e.printStackTrace();
return null;
}
}
public StreamedContent getChart() {
try {
return DefaultStreamedContent.builder()
.contentType("image/png")
.stream(() -> {
try {
JFreeChart jfreechart = ChartFactory.createPieChart("Cities", createDataset(), true, true, false);
File chartFile = new File("dynamichart");
ChartUtils.saveChartAsPNG(chartFile, jfreechart, 375, 300);
return new FileInputStream(chartFile);
}
catch (Exception e) {
e.printStackTrace();
return null;
}
})
.build();
}
catch (Exception e) {
e.printStackTrace();
return null;
}
}
public StreamedContent getChartWithoutBuffering() {
try {
return DefaultStreamedContent.builder()
.contentType("image/png")
.writer((os) -> {
try {
JFreeChart jfreechart = ChartFactory.createPieChart("Cities", createDataset(), true, true, false);
ChartUtils.writeChartAsPNG(os, jfreechart, 375, 300);
}
catch (Exception e) {
e.printStackTrace();
}
})
.build();
}
catch (Exception e) {
e.printStackTrace();
return null;
}
}
public InputStream getChartAsStream() {
return getChart().getStream().get();
}
public byte[] getChartAsByteArray() throws IOException {
InputStream is = getChartAsStream();
byte[] array = new byte[is.available()];
is.read(array);
return array;
}
private PieDataset<String> createDataset() {
DefaultPieDataset<String> dataset = new DefaultPieDataset<String>();
dataset.setValue("New York", Double.valueOf(45.0));
dataset.setValue("London", Double.valueOf(15.0));
dataset.setValue("Paris", Double.valueOf(25.2));
dataset.setValue("Berlin", Double.valueOf(14.8));
return dataset;
}
}