I send GIFs using servlet and this may differ for JPEGs.

In the GIF case:
Since I already have the image as byte array I just set the content type to
image/gif and send them to the servlet's output stream.  Without any
manipulation or involvment of graphics functions.

In the HTML you go like this:
<img src="/servlet/ImageServlet?ID=lalala">

dave

Ross Wayland wrote:

> Hi,
>
> I'm having a problem displaying jpeg images via a servlet...
>
> Context:
> The jpeg images are being passed from another java application
> in the form of a byte[]. The function of the servlet is to grab
> the byte[] and display it on a dynamically created html page.
>
> I initially tried using the jpeg encoder from www.acme.com. Their GIF
> encoder works great but I was unable to get their JPEG encoder to work
> so I turned to the jpeg encoder built into JDK 1.2 which seems to require
> transforming Image to BufferedImage.
>
> The code snippet below produces an image on the html page with the correct
> dimensions, but the image is solid black. Can anyone provide some pointers
> as to where I went astray or suggest a better way of handling the
> transformation from byte[] to BufferedImage? thanks in advance.
>
> Ross
>
> .
> .
> .
> //dataStream is a byte[] with the image data
> response.setContentType("image/jpeg");
> ServletOutputStream out = response.getOutputStream();
> BufferedImage bi = draw(dataStream);
> encode(bi,out);
> .
> .
> .
>
> public static BufferedImage draw(byte[] dataStream) {
>     BufferedImage bi =
>         new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_RGB);
>     Graphics2D g2d = bi.createGraphics();
>     Image image = Toolkit.getDefaultToolkit().createImage(dataStream);
>     g2d.drawImage(image,null,null);
>     g2d.dispose();
>     return(bi);
> }//end draw
>
> public static void encode(BufferedImage bi, OutputStream out)
>         throws IOException {
>     JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
>     JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
>     param.setQuality(1.0f,false);
>     encoder.encode(bi,param);
> }//end encode
>
> ___________________________________________________________________________
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
> of the message "signoff SERVLET-INTEREST".
>
> Archives: http://archives.java.sun.com/archives/servlet-interest.html
> Resources: http://java.sun.com/products/servlet/external-resources.html
> LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

--
David Mossakowski              [EMAIL PROTECTED]
Programmer                           212.310.7275
Instinet Corporation

"I don't sit idly by, I'm planning a big surprise"

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to