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>GraphicText</h5>
<p:graphicImage value="#{graphicImageView.graphicText}"/>
</div>
package org.primefaces.showcase.view.multimedia;
import jakarta.enterprise.context.RequestScoped;
import jakarta.inject.Named;
import javax.imageio.ImageIO;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import io.quarkus.runtime.annotations.RegisterForReflection;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
@Named
@RequestScoped
@RegisterForReflection(serialization = true)
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;
}
}
}