Tech-Today
How to implement an image streamer for primefaces graphicimage component
This page provide code samples for using primefaces graphic image component. In this case we are assuming that the byte data of an image is stored in an entity and that entity is access by its id. See the param id in #1.
The param id is read by the streamer and use to fetch the entity that stores the byte[] data.
1.) In the JSF page, let us define a graphicImage component like this:
<p:graphicImage id="serviceImage" cache="false"
value="#{customImageStreamer.image}"
>
<f:param name="id" value="#{myService.entity.id}"></f:param>
</p:graphicImage>
2.) Let us define a generic image streamer that we can extend.
public abstract class ImageStreamer<T extends BaseEntity> {
public StreamedContent getImage() throws IOException {
FacesContext context = FacesContext.getCurrentInstance();
if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
// So, we're rendering the HTML. Return a stub StreamedContent so
// that it will generate right URL.
return new DefaultStreamedContent();
} else {
// So, browser is requesting the image. Return a real
// StreamedContent with the image bytes.
Long id = Long.parseLong(context.getExternalContext().getRequestParameterMap().get("id"));
T obj = getPersistenceService().findById(id);
if (getImageArr(obj) != null) {
return new DefaultStreamedContent(new ByteArrayInputStream(getImageArr(obj)));
} else {
return new DefaultStreamedContent();
}
}
}
public StreamedContent getImage(T obj) throws IOException {
// So, browser is requesting the image. Return a real
// StreamedContent with the image bytes.
if (getImageArr(obj) != null) {
return new DefaultStreamedContent(new ByteArrayInputStream(getImageArr(obj)));
} else {
return new DefaultStreamedContent();
}
}
protected byte[] downloadUrl(URL toDownload) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
InputStream stream = null;
try {
byte[] chunk = new byte[4096];
int bytesRead;
stream = toDownload.openStream();
while ((bytesRead = stream.read(chunk)) > 0) {
outputStream.write(chunk, 0, bytesRead);
}
} catch (IOException e) {
return null;
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
return null;
}
}
}
return outputStream.toByteArray();
}
public abstract PersistenceService getPersistenceService();
public abstract byte[] getImageArr(T obj);
}
3.) The actual class implementation that streams the image. Note that it must be applicationScoped.
@Named
@ApplicationScoped
public class StudentImageStreamer extends ImageStreamer {
@Inject
private StudentService studentService;
@Override
public PersistenceService getPersistenceService() {
return studentService;
}
@Override
public byte[] getImageArr(Student obj) {
return obj.getImageAsByteArr();
}
}
If you have another class that needs streaming for primefaces graphic image, then just create another class that extends ImageStreamer.
References:
http://stackoverflow.com/questions/8207325/display-dynamic-image-from-database-with-pgraphicimage-and-streamedcontent
-
Encode Image And Display In Html
To encode an image file(eg. png, jpg, gif) we will need the apache commons-codec library. To avoid it in a maven project: <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.11</version>...
-
Display A Byte[] Image From A Rest Web Service
This tutorial will not delve into angular nor will we discuss what a directive is. We will not try to explain what a rest web service is. Instead we will provide a demonstration via actual code, how we can consume a rest service from angular that returns...
-
Java Generics And Multiple Parameterized Class With Inheritance
This tutorial will try to explain how to use multiple generic parameters in Java and also along the way the advantage of using this feature. Normally I use this when I have a method in a base class that returns a generic type. For example: public class...
-
Loading Locale From Post/get Parameter
This page will summarize how to load a resource bundle dynamically from a GET/POST parameter. 1.) In your mail xhtml template, make sure that you define f:view locale before the h:body tag: <f:view locale="#{languageBean.currentLocale}" />...
-
How To Render A Chart Object In A View In Mvc3
I was working on a project that requires showing a graph, fortunately there's already a built in graph helper in mvc3: http://www.asp.net/web-pages/tutorials/data/7-displaying-data-in-a-chart. Tried the tutorial in link above and was able to display...
Tech-Today