In our second installment of VTK in a servlet (for the first part check
here) we will look on how to integrate VTK in a servlet.
Because there are some issues with JNI in servlets (look for instance
here or some
description involving ImageMagick) you have to decide where to load the native libraries. Because of the frequent crashes of VTK I decied to use the web application's class loader and put the code into the web application itself. Downside is of course, that you will have to restart Tomcat when you make a chnage to your code.
To achieve this I am using the following code and make all the servlet's actions (I am using Struts 1.2) inherit it:
public class vtkAction extends Action {
static {
System.loadLibrary("vtkCommonJava");
System.loadLibrary("vtkFilteringJava");
System.loadLibrary("vtkIOJava");
System.loadLibrary("vtkImagingJava");
System.loadLibrary("vtkGraphicsJava");
System.loadLibrary("vtkRenderingJava");
}
}
In the other installment we showed how to generate off screen renderings. Now we just have to put the two things together for instance this piece of code parses some web parameters and calls the rendering function:
public ActionForward execute(ActionMapping mapping,
ActionForm form, HttpServletRequest req,
HttpServletResponse res) throws Exception
{
DynaActionForm dynaForm = (DynaActionForm)form;
Double level = (Double)dynaForm.get("level");
Double window = (Double)dynaForm.get("window");
double dlevel = 127.5;
double dwindow = 255;
if (level!=null) {
dlevel = level.doubleValue();
}
if (window!=null) {
dwindow = window.doubleValue();
}
File file = ObjectStore.getInstance().getSlice(
(String)dynaForm.getString("filename"),
((Integer)dynaForm.get("orientation")).intValue(),
((Integer)dynaForm.get("slice")).intValue(),
dwindow, dlevel);
System.out.println("test x1");
//write out PNG
FileInputStream in = new FileInputStream(file);
OutputStream out = res.getOutputStream();
res.setContentType("image/png"); //Mime Type: JPEG
//copy to Output Stream
copy2(in, out);
in.close();
out.flush();
file.delete(); //delete file
return null;
}
This essentially is responsible for almost everything. The final product will soon be on our web page
http://www.digitalfishlibrary.org