Have you ever thought about sending dynamic images from JSP pages (or Servlets? This tip tells you how to do it. To run the code here, 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:
<% @ Page contenttype = "image/JPEG"... %> 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:
Graphics G = image. getgraphics (); // Or Graphics2d g2d = image. creategraphics (); |
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:
G. Dispose (); // Or G2d. Dispose (); |
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.
Servletoutputstream SOS = response. getoutputstream (); Required imageencoder encoder = Required codec. createjpegencoder (SOS ); Encoder. encode (image ); // Or ImageIO. Write (image, "Jpeg", out ); |
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 the last "%>" into 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.
<% @ Page contenttype = "image/JPEG" Import = "Java. AWT. *, java. AWT. image .*, Com.sun.image.codec.jpeg. *, java. util .*" %> <% // Create Image Int width = 200, Height = 200; Bufferedimage image = new bufferedimage (width, Height, bufferedimage. type_int_rgb ); // Get drawing Context Graphics G = image. getgraphics (); // Fill background G. setcolor (color. White ); G. fillrect (0, 0, width, height ); // Create Random Polygon Polygon poly = new polygon (); Random random = new random (); For (INT I = 0; I <5; I ++ ){ Poly. addpoint (random. nextint (width ), Random. nextint (height )); } // Fill Polygon G. setcolor (color. Cyan ); G. fillpolygon (poly ); // Dispose Context G. Dispose (); // Send back Image Servletoutputstream SOS = response. getoutputstream (); Required imageencoder encoder = Required codec. createjpegencoder (SOS ); Encoder. encode (image ); %> |