Re: Creating a gif file from a visad display (inside a servlet?)

  • To: Amaral <amaral@xxxxxx>
  • Subject: Re: Creating a gif file from a visad display (inside a servlet?)
  • From: Ugo Taddei <p6taug@xxxxxxxxxxx>
  • Date: Tue, 18 Mar 2003 09:24:07 +0100
Alo, Amaral,

following up on you last question on the list, it occurred to me that you might want to to this (s. below) inside a servlet.

Amaral wrote:
Please, I need help on the following problem. I would like to create a gif file from a visad display. I am using the code below but it does not work. Thanks for any help. Runnable captureImage = new Runnable() {
 public void run() {
 Image img = display.getImage();
 Graphics g =img.getGraphics();
g.drawImage(img, 0,0, null); try
  {
File filee = new File("file.gif");
OutputStream output = new BufferedOutputStream(new FileOutputStream(filee));
GIFEncoder encoder = new GIFEncoder(img);
encoder.Write(output);
  }
  catch (Exception w){ }
      }
    }
Thread t = new Thread(captureImage);
   t.start();

Two points:

1. I'd recommend you used another image format rather than gif. PNG is the best alternative for web graphics. JPG is also a choice. (Which format to use depends on the type of image.)

2. To create an image inside a servlet you can do:

// get the output stream to send the image down the line
 ServletOutputStream servOutStream = response.getOutputStream();

// set the content type so the browser will be able to interpret
// the img correctly
 response.setContentType( "image/jpeg" );


// this is the image
 BufferedImage bufImage = display.getImage(true);

// make a jpg out of it
 JPEGEncodeParam jepar = JPEGCodec.getDefaultJPEGEncodeParam(bufImage);
 jepar.setQuality( 1.0f, true);

 JPEGImageEncoder jpege = JPEGCodec.createJPEGEncoder(servOutStream) ;
 jpege.encode(bufImage, jepar);

// and send it to the browser
 servOutStream.flush();
 servOutStream.close();

You can also save the image to the disk and send a full html page back, with text and the img.

I'm currently working on a few VisAD servlet examples. These will be available "soon".

Cheers,

Ugo