Show metafiles with JSPs and Servlets

Skip to end of metadata
Go to start of metadata
Server-side image generation gives us the ability to draw on the fly to show information that is timely or specific to the current user. In this article, we will explore how to generate images from metafiles in JSPs and servlets.

Parsing Parameters

Our example is wmf metafile viewer written in JSP. The request to our JSP will look like this:

 
http://server.com/wmfmetafile.jsp?fname=lion.wmf&width=100&height=100
 

You'll notice above that there are three parameters:

  • fname - file name of a metafile to show
  • width and height - size of generated image to show


This is the code to parse the parameters:

  String fname = request.getParameter("fname");
  int width = Integer.parseInt(request.getParameter("width"));
  int height = Integer.parseInt(request.getParameter("height"));
 

Drawing Into Buffer

With the inputs set up, we are ready to actually draw our metafile. On the server we need create a BufferedImage.

Java

 // Create BufferedImage object with size we got from JSP's parameters
 BufferedImage bimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

 // Obtain Graphics2D object for the image and fill image with white color
 Graphics2D g = bimage.createGraphics();
 g.setPaint(Color.WHITE);
 g.fill(new Rectangle(0, 0, width, height));

 // Turn on antialiasing for this Graphics2D
 Map map = new HashMap();
 map.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
 map.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
 g.addRenderingHints(map);


 // Read our metafile
 WmfMetafile wmf = new WmfMetafile(new FileInputStream(new File(fname)));

 // Add scale transformation
 AffineTransform trans = AffineTransform.getScaleInstance(
       (double)width / wmf.getWidth(), (double)height / wmf.getHeight);
 graphics.setTransform(trans);

 // Draw metafile
 wmf.playMetafile(g);
 

Writing the Image

Now we have a BufferedImage created and drawn, the last step is to write it out to an actual image file that the web browser can see. It looks like this:

Java

 response.setContentType("image/png");
 OutputStream os = response.getOutputStream();
 ImageIO.write(bimage, "png", os);
 os.close();
 

The first two lines above set the content type to image/png, which is the appropriate MIME type for PNG images, and get the output stream from the HTTP response object. The next line does the magic; using the static write method in the java.imageio.ImageIO class, we pass in the image (bimage), the desired image format ("png"), and the output stream to write to (os). Close the output stream and we're done!

This JSP produces only the image itself. We would create a web page to link to the JSP as an image. Here's what a simple web page would look like:

 <html>
  <body>
   <h3>A simple page with metafile</h3>
    <img src="wmfmetafile.jsp?fname=lion.wmf&width=100&height=100"/>
  </body>
 </html>

 
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.