How to transmit dynamic images from JSP pages

Source: Internet
Author: User

To run the code in this article, you need a Tomcat or other Web server that supports JSP 1.1.

When a Web page is sent with the MIME type of image/jpeg (or other image formats), your browser treats the returned result as an image and then displays the image, as part of a page or as an image itself. To set the MIME type for your jsp page, you need to set the contentType attribute of the page:

Then you need to create a BufferedImage to draw your dynamic image:

BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);

After creating a BufferedImage, You need to obtain the graphic environment for drawing, a Graphics or Graphics2D object:

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 
align=center borderColorLight=black border=1><tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6>Graphics g = image.getGraphics();// orGraphics2d g2d = image.createGraphics();</td></tr></table>

From now on, you can draw the image content. The image environment will be painted to BufferedImage. This image is black at first, so it is a good idea to fill the image with the background color you want. Then, when you complete the image painting, you need to dispose the graphic environment:

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 
align=center borderColorLight=black border=1>
<tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6>
g.dispose();
// or
g2d.dispose();</td></tr>
</table>

Once the image is drawn, you return the image in response. You can use the custom imageencoder class in the non-standard com.sun.image.codec.jpeg package to encode the image, or if you use JDK1.4, you can use the standard ImageIO class. There is a trick when using tranquility imageencoder. You must obtain ServletOutputStream from ServletResponse, instead of using the implied JSP output variable out.

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 
align=center borderColorLight=black border=1>
<tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6>
ServletOutputStream sos = response.getOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(sos);
encoder.encode(image);
// or
ImageIO.write(image, "JPEG", out);</td></tr>
</table>

Here there is a scheme from all possibilities (such as g. dispose (); or g2d. dispose ();) selects a complete example. in this example, a random polygon is drawn using a Graphics object, and the image is drawn using JPEGImageEncoder. You can freely set the number of vertices of a polygon to get a more complex shape. In other words, there are more vertices and edges.

To run this example, place the jsp code from "" to an image. in the jsp file, place the file to a location that can be found on your web server. When Tomcat is used, it is the ROOT directory. start Tomcat and access http: // localhost: 8080/image. jsp

 <table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1><tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6> <%@ page contentType="image/jpeg"import="java.awt.*,java.awt.image.*,com.sun.image.codec.jpeg.*,java.util.*"%>  <%// Create imageint width=200, height=200;BufferedImage image = new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB);// Get drawing contextGraphics g = image.getGraphics();// Fill backgroundg.setColor(Color.white);g.fillRect(0, 0, width, height);// Create random polygonPolygon poly = new Polygon();Random random = new Random();for (int i=0; i < 5; i++) {poly.addPoint(random.nextInt(width),random.nextInt(height));}// Fill polygong.setColor(Color.cyan);g.fillPolygon(poly);// Dispose contextg.dispose();// Send back imageServletOutputStream sos = response.getOutputStream();JPEGImageEncoder encoder =JPEGCodec.createJPEGEncoder(sos);encoder.encode(image);%>

  1. High-performance, high-flexibility JSP and Servlet Performance Optimization
  2. Advantages and implementation of jstl and el jsp page development
  3. Detailed steps for applying JavaBean in JSP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.